Reputation: 427
I need some help understanding the code in a book that I'm using to learn C++.
Class Fish
{
public:
virtual Fish* Clone() = 0; //pure virtual function
};
Class Tuna: public Fish
{
public:
Tuna* Clone() //virtual clone function
{
return new Tuna(*this);
}
};
The section is actually on finding a way to get around the nonexistance virtual copy constructor in C++. However, I simply am having trouble wrapping my mind around the
return new Tuna(*this);
statement. As an aside, I understand what the following code.
int* ptr;
ptr = new int[6];
This would allocation a block of 6 int's to a pointer.
new Tuna();
would just allocate a pointer to a location that has enough space to hold a class Tuna from the first code snippet. However what does the dereference of the This pointer mean? The This pointer points to the object right? So deallocating it would unveil the contents at the objects memory location? How does this tie into
new int[6];
and
new Tuna(*this);
Upvotes: 0
Views: 149
Reputation: 11
C++ adds copy constructor to classes by default. So
new Tuna(*this)
means allocate memory of Tuna and copy the content of this to the "new" Tuna. Deallocating this will not affect the "new" Tuna except that there is ptr in the class of Tuna, for example
class Tuna
{
int* someArray;
public Tuna()
{
someArray = new int[16];
}
}
in such case you need to define the copy constructor by yourself to copy the content of the array (someArray here).
Upvotes: 0
Reputation: 924
new Tuna(*this);
This is the same as calling the copy constructor of Tuna
passing a variable by reference. You need to remember that if you don't provide a copy constructor the compiler will automatically provide one for you. Something like this; Tuna(const Tuna& var)
. So in this case dereferencing the this
pointer passes the instance that it refers to as a parameter to the copy constructor of the instance just allocated by the call to new.
You can think of new int[6]
as calling the default constructor of the int
class 6 times and allocating them in consecutive memory locations.
Upvotes: 1
Reputation: 385204
Tuna
has a copy constructor, even if it's compiled-defined.
You're dynamically allocating a new Tuna
instance, by passing a reference to the current one and copying from that.
new Tuna(*this);
// ^ ^ ^
// | | ref to current
// | | instance as
// | | argument
// | |
// | copy ctor
// | plz!
// dynamic
// allocation
The copy constructor being invoked looks like this:
Tuna(const Tuna& other);
And *this
gives you an lvalue representing the current Tuna
, which happily binds to this reference parameter.
In short, this expression does precisely what the function name implies: it clones the current object.
It's the dynamically-allocated equivalent of the following declaration:
Tuna gimmeMoreTuna(*this);
new int[6]
has pretty much nothing to do with it, frankly.
new Tuna();
would just allocate a pointer to a location that has enough space to hold a class Tuna from the first code snippet
No, it calls the default Tuna
constructor, too. That's pretty much what you're missing.
Upvotes: 3