4

I'm having trouble understanding what's really happening with the code in a book I'm using to learn C++. Here's the code:

class Base
{
    public:
        Base() {};
        virtual ~Base() {};

        virtual Base* Clone() {return new Base(*this);}
};

class Derived
{
    public:
        Derived() {};
        virtual ~Derived() {};

        virtual Base* Clone() {return new Derived(*this);}
};

So in this Clone() function I understand that the function returns a pointer to a Base class object. What I don't understand is what's happening within that function. When I've previously used new as in int *pInt = new int, I was under the impression that new essentially allocates enough memory on the free store for an integer, then returns that address, applying the address to the pointer pInt. With that same logic I'm trying to understand the new Derived(*this) portion of the code. So, I think it is allocating enough memory on the free store for a Derived class object, and returning the address, which is then returned by the function Clone().

Why, though, does it pass *this through the constructor, if that is a constructor? I understand *this means its passing the address of whatever object is being cloned, but I don't understand the syntax of class_name(address_of_an_object) in the context of the new function.

Could someone please explain what's happening in that portion?

Thanks in advance.

1 Answer 1

10

The misunderstanding is here:

*this means its passing the address of whatever object is being cloned

In reality, this is the address of the object that is being cloned, but *this (note the asterisk) is the result of dereferencing that address. So *this is of type Derived &, it's a reference to the object being cloned, not its address.

Therefore, calling new Derived(*this) means that after dynamically allocating space (which is what new does), the new space is initialised by the copy constructor Derived(const Derived &), which in this case hasn't actually been user-defined, so the (compiler-generated) default version of the copy constructor is used.


To clarify the semantics of new: If C is a class, then

new C;

allocates enough space for an object of type C and then calls the constructor of C to initialise that space. This is part of the semantics of new: It always calls the constructor to initialise the newly allocated space.

When you call

new C(a,b,c);

with some arguments a, b and c, then new will call a constructor of C that takes these three arguments. If no such constructor has been defined, you'll get a compiler error.

Now in the special case where you call

new C(a);

with an argument a that is itself of type C&, new will, as always, call the appropriate constructor. The appropriate constructor is either C(C &) (if defined), or C(const C&) (copy-constructor auto-defined by the compiler).

4
  • Thanks for the reply. I guess I don't understand how exactly I'm "calling the copy constructor" with 'new'. If 'Derived(*this)' is merely a reference of the object being cloned, how is 'new' calling a copy constructor? I thought 'new' was just about allocating memory and returning an address, is that not the case?
    – EindacorDS
    Feb 6, 2013 at 1:55
  • 3
    new always invokes a constructor. new Derived() invokes default constructor, new Derived(1,2) invokes the constructor (int,int) - that does not exist - and new Derived(*this) is invoking the implicit copy constructor. Everytime you have a constructor of the type ClassName(const ClassName&) it is a copy constructor, if you pass an object of ClassName to construct another object of ClassName it will be copied. Feb 6, 2013 at 2:02
  • @user1997615 What André said. I've also added a clarification to the answer.
    – jogojapan
    Feb 6, 2013 at 2:04
  • 1
    Thank you both for the clarification, this lesson makes much more sense now. Really appreciate the help.
    – EindacorDS
    Feb 6, 2013 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.