Am I correct assuming that a reference to an object will not call the object's destructor when the reference goes out of scope? The code I attached supports my assumption but I'm not certain if its correct...Any comments?
#include <iostream>
class myint
{
public:
myint():itsvalue(0) {}
myint(unsigned long val):itsvalue(val) { std::cout << "constructing->" << itsvalue << std::endl; }
~myint() { std::cout << "destroying->" << itsvalue << std::endl; }
std::ostream& print(std::ostream & out) const { return out << itsvalue; }
private:
unsigned long itsvalue;
};
std::ostream& operator <<(std::ostream & out, const myint & m)
{
return m.print(out);
}
myint& myfunc(void)
{
myint *ans = new myint(123);
return *ans;
}
void myfunc2(void)
{
myint & myr = myfunc();
std::cout << myr << std::endl;
//the reference myr doesn't call the destructor ~myint() when it goes out of scope
}
int main()
{
myfunc2();
return 0;
}