H
Haquejiel
Hello. I have a question that is simultaneously simple and
complicated.
I have a custom class I wrote, and I want to return new instances of
it by reference (for odd reasons). I have the deconstructor of this
class outputting to stdout when it is called.
In MSVC++, returning a reference to a new instance of this object
seems to be legitimate - if I assign it to a local reference, it gets
deconstructed when it goes out of scope - but does it get deleted?
Here's an example of what I've been testing against:
Object& someFunction()
{
Object *newObject = new Object();
return *(newObject);
}
int main()
{
Object outer = someFunction();
cout << "Before" << endl;
{
Object inner = someFunction();
}
cout << "After" << endl;
return 0;
}
The result of this appears to be as such (pretending the deconstructor
outputs the object name):
Before
inner deconstructed.
After
outer deconstructed.
If I do not assign someFunction() to a value, a deconstructor is never
apparently called. Is this a memory leak?
The deconstructor is being called for inner, but is the memory getting
cleaned up?
These are pretty specific questions, but I'm having trouble finding
them answered explicitly. Could anyone help me out here?
Thankee
- (e-mail address removed)
complicated.
I have a custom class I wrote, and I want to return new instances of
it by reference (for odd reasons). I have the deconstructor of this
class outputting to stdout when it is called.
In MSVC++, returning a reference to a new instance of this object
seems to be legitimate - if I assign it to a local reference, it gets
deconstructed when it goes out of scope - but does it get deleted?
Here's an example of what I've been testing against:
Object& someFunction()
{
Object *newObject = new Object();
return *(newObject);
}
int main()
{
Object outer = someFunction();
cout << "Before" << endl;
{
Object inner = someFunction();
}
cout << "After" << endl;
return 0;
}
The result of this appears to be as such (pretending the deconstructor
outputs the object name):
Before
inner deconstructed.
After
outer deconstructed.
If I do not assign someFunction() to a value, a deconstructor is never
apparently called. Is this a memory leak?
The deconstructor is being called for inner, but is the memory getting
cleaned up?
These are pretty specific questions, but I'm having trouble finding
them answered explicitly. Could anyone help me out here?
Thankee
- (e-mail address removed)