#include <iostream>
#include <string>
// minimap example-class to test
// - constructor variable shadowing
// - assigning to const-refs -- see https://stackoverflow.com/questions/27423364/binding-const-of-temporary-no-compiler-warning
// the device-under-test:
class TestClass {
public:
std::string a;
std::string b;
const std::string &c;
// removing the "reference" whould remove the temporary-problem
const std::string &d;
TestClass(std::string a, std::string b, const std::string &c,
const std::string &d)
: a(a), b(b), c(c), d(d) {
// pitfall: changing the variable "a" from the constructor-call, not
// the actual member variable
a = "A";
// using pointer-indirection the lookup-semantics are different and the
// member variable is found instead
this->b = "B";
// "c" is a const-ref, cannot be changed at all... if it is assigned some
// temporary value it is mangled up...
}
};
int main() {
// a stack-resident variable, where we can safely pass a const-ref
std::string c("c");
// creating the actual test-oject.
//
// NOTE: the fourth variable "d" is a
// temporary, whose reference is not valid... what I don't get in the
// moment: why does no compiler warn me?
TestClass dut("a", "b", c, "d");
// and printing what we got:
std::cout << "beginning output:\n\n";
std::cout << "dut.a: '" << dut.a << "'\n";
std::cout << "dut.b: '" << dut.b << "'\n";
std::cout << "dut.c: '" << dut.c << "'\n";
// this will abort the program (gcc-4.9.1) or be an empty string
// (clang-3.5) -- don't know whats going on here... I mean I know why
// it should segfault (using reference of temporary), but why does no
// compiler warn here?
//std::cout << "dut.d: '" << dut.d << "'\n";
std::cout << "\nthats it!\n";
return 0;
}