Interesting C++ Questions

Actually, this one is a little quiz for you. I don’t exactly know all the answers, but may you do šŸ˜‰ These are some questions that highlight the differences between those garbage-collected languages where almost everything is a pointer-like reference, and the gritty world of C++.
Assume you have a very simple C++ class like

class MyPoint


double x;

double y;

MyPoint(const CMyPoint& point) { … }

MyPoint& operator = (const MyPoint& param) { … }


and now you write a method that returns such a beast:

MyPoint MakeSomePoint()


MyPoint NewPoint;

return NewPoint;


Now you can call this method from somewhere else:

MyPoint foobar = MakeSomePoint();

Interestingly enough, this kind of stuff really works. The assignment in the caller is still somewhat straightforward: A new object is created on the stack and the copy constructor is called with the result of MakeSomePoint(). But the method creates the object on it’s local stack, so why is it apparently still valid? Or is this only by accident?
Further questions:

  • What kind of thing does the MakeSomePoint() method return? Is it the exact same object that was created in the method body? Or a copy thereof? Or something else? And how’s an object type passed (In contrast to pointers or references)?
  • What would be different if the method returned a reference instead?
  • At which point are the objects from MakeSomePoint()‘s stack deleted?

Happy hunting!


2 thoughts on “Interesting C++ Questions

  1. The following is happening when you call the MakeSomePoint() method:
    – Space for the return value is alllocated on the stack
    The function MakeSomePoint()gets called
    – The constructor of MyPoint is beeing called
    – The return statement calls the copy constructor to create a new point on the location it reserved for the return value on the stack
    – The MakeSomePoints() method ends

    So to answer your questions: The MakeSomePoint() method returns an object of type MyPoint (so exactly what you wrote in the code) by making a copy of the local variable in MakeSomePoint(). If the method would return a reference to an object instead, you would return a reference to an invalid object. The MakeSomePoints()‘s stack gets deleted just before the MakeSomePoints method returns.

    Ther is one mor interesting thing in your code: If you would write

    MyPoint foo;
    foo = MakeSomePoint();

    instead of your code above, then before the steps described would happen, the default constructor of MyPoint would have been called, and in the end, the assignment operator would get called.


  2. Of course I played with this again šŸ˜‰ I tried to make the return value a reference, in which case the copy constructor was called during the assignment after the method returned.
    On the surface, this method seems to work, but the destructor is called on exiting the method. Thus, the object is obviously already deleted, even if the invalid reference may still point to the old data.

    Another interesting thing though: When I use a reference type, the copy constructor is called during the assignment, as expected. However, if I return an object, the copy constructor is called during the “return” (as predicted by uli). It doesn’t seem to be called during the assignment in the main routine, though.

    I would have though that another object is created on the stack, copy-constructed with the returned object. But it seems that the assignment just “uses” the returned object. I wonder if that’s some compiler trick, or if something else is going on šŸ˜‰


Comments are closed.

Blog at

Up ↑

%d bloggers like this: