Reputation: 131
Here's what I've tried:
game* Reversi::clone() const{
Reversi* ptr = this;
return ptr;
}
But I receive the following error:
error: invalid conversion from ‘const Reversi*’ to ‘Reversi*’ [-fpermissive]
Reversi* ptr = this;
Thanks in advance.
Upvotes: 2
Views: 165
Reputation: 172894
The type of this
is const Reversi*
inside the const member function. You can use a non-const member function,
game* Reversi::clone() {
Reversi* ptr = this;
return ptr;
}
or change the type of ptr
to const Reversi*
,
const game* Reversi::clone() const {
const Reversi* ptr = this;
return ptr;
}
BTW: Your code just make a copy of the pointer this
, doesn't copy the content at all. That means the returned pointer will just point to the same object. You might want,
game* Reversi::clone() const {
Reversi* ptr = new Reversi(*this); // use copy ctor here
return ptr;
}
Note when you make a copy from this
the member function could be const
. This seems more reasonable because a clone method shouldn't change the status of the original object in general.
Upvotes: 3