C
Ciccio
Hi all,
In one of my little programming efforts, I wrote a piece of
code that creates references to destroyed objects. The output
and the piece of code that creates it can be found below this
post.
I have two questions related to this program and its output.
1) Why is the copy constructor not called in the first part?
2) Why is, in the second part, the referenced object destroyed
before the object carrying the reference. And why is this not
the case in the first part.
2b) Is this valid c++ ???
I have tested this on various g++ compilers and also the intel
compilers. All give the same output. Valgrind only detects an
error when I try to access broken data.
Thanks for the help,
Klaas
The output and the code are given here
// OUTPUT
inside bar_fun()
construct foo: 0x7fff1bce5508
construct bar: 0x7fff1bce5508
return foo pointer: 0x7fff1bce5508
inside bar_fun()
construct foo: 0x7fff1bce5518
construct bar: 0x7fff1bce5518
construct foo: 0x7fff1bce5528
mirror bar: 0x7fff1bce5528
destruct bar: 0x7fff1bce5518
destruct foo: 0x7fff1bce5518
return foo pointer: 0x7fff1bce5518
destruct bar: 0x7fff1bce5528
destruct foo: 0x7fff1bce5528
destruct bar: 0x7fff1bce5508
destruct foo: 0x7fff1bce5508
// CODE
#include <iostream>
struct foo {
foo() { std::cout << "construct foo: " << this << std::endl;
}
~foo() { std::cout << "destruct foo: " << this << std::endl;
}
void ret_ptr() { std::cout << "return foo pointer: " << this
<< std::endl; }
char c;
};
struct bar {
bar(): f(f_) { std::cout << "construct bar: " << this <<
std::endl; }
bar(bar &b): f(f_) { std::cout << "not copy constructor bar:
" << this << std::endl; }
bar(const bar &b): f(f_) { std::cout << "copy constructor
bar: " << this << std::endl; }
bar(const bar &b, int i): f(b.f) { std::cout << "mirror bar:
" << this << std::endl; }
~bar() { std::cout << "destruct bar: " << this << std::endl;
}
foo f_;
foo &f;
};
bar bar_fun() {
std::cout << "inside bar_fun()" << std::endl;
return bar();
}
int main() {
bar b1(bar_fun());
b1.f.ret_ptr();
std::cout << std::endl;
bar b2(bar_fun(),1);
b2.f.ret_ptr();
};
In one of my little programming efforts, I wrote a piece of
code that creates references to destroyed objects. The output
and the piece of code that creates it can be found below this
post.
I have two questions related to this program and its output.
1) Why is the copy constructor not called in the first part?
2) Why is, in the second part, the referenced object destroyed
before the object carrying the reference. And why is this not
the case in the first part.
2b) Is this valid c++ ???
I have tested this on various g++ compilers and also the intel
compilers. All give the same output. Valgrind only detects an
error when I try to access broken data.
Thanks for the help,
Klaas
The output and the code are given here
// OUTPUT
inside bar_fun()
construct foo: 0x7fff1bce5508
construct bar: 0x7fff1bce5508
return foo pointer: 0x7fff1bce5508
inside bar_fun()
construct foo: 0x7fff1bce5518
construct bar: 0x7fff1bce5518
construct foo: 0x7fff1bce5528
mirror bar: 0x7fff1bce5528
destruct bar: 0x7fff1bce5518
destruct foo: 0x7fff1bce5518
return foo pointer: 0x7fff1bce5518
destruct bar: 0x7fff1bce5528
destruct foo: 0x7fff1bce5528
destruct bar: 0x7fff1bce5508
destruct foo: 0x7fff1bce5508
// CODE
#include <iostream>
struct foo {
foo() { std::cout << "construct foo: " << this << std::endl;
}
~foo() { std::cout << "destruct foo: " << this << std::endl;
}
void ret_ptr() { std::cout << "return foo pointer: " << this
<< std::endl; }
char c;
};
struct bar {
bar(): f(f_) { std::cout << "construct bar: " << this <<
std::endl; }
bar(bar &b): f(f_) { std::cout << "not copy constructor bar:
" << this << std::endl; }
bar(const bar &b): f(f_) { std::cout << "copy constructor
bar: " << this << std::endl; }
bar(const bar &b, int i): f(b.f) { std::cout << "mirror bar:
" << this << std::endl; }
~bar() { std::cout << "destruct bar: " << this << std::endl;
}
foo f_;
foo &f;
};
bar bar_fun() {
std::cout << "inside bar_fun()" << std::endl;
return bar();
}
int main() {
bar b1(bar_fun());
b1.f.ret_ptr();
std::cout << std::endl;
bar b2(bar_fun(),1);
b2.f.ret_ptr();
};