As far as I know, the constructor does not have a return type. But in the next code snippet, it looks like ctor really returns a const reference object (does ctor have a return type const reference aclass that is hidden?), (Or) returns a piece of memory of type const reference (Q1)? or is it something else (Q1)? Are these objects valid that are returned by ctor (Q2), are they equivalent c stack_object;(Q2)? Please share your thoughts.
#include<iostream>
using namespace std;
class c{
public:
int x = 88;
c(int i){
x=i;
}
c(){
}
~c(){
}
};
int f(){
const c & co1 = c();
c &ref = const_cast<c&>(co1);
cout<<"\n addr3 = "<<&ref<<"\n";
ref.x = 99;
}
int main(){
const c &co = c(3);
c *p = const_cast<c*>(&co);
cout<<"\n addr1 = "<<p<<"\n";
p->x = 11;
const c & co1 = c();
c *p1 = const_cast<c*>(&co1);
cout<<"\n addr2 = "<<p1<<"\n";
f();
cout<<"\n main() Done\n";
return 0;
}
o / p here:
addr1 = 0xbfc3c248
addr2 = 0xbfc3c24c
addr3 = 0xbfc3c214
main() Done
source
share