I have some difficulties using this
, it seems to slice my derived class. This example will help me to explain my problem.
class A
{
A() {
OtherClass(*this);
}
virtual doSomething() = 0;
}
class B : public A
{
B() : A() {}
doSomething() override {
std::cout << "Hi!" << std::endl;
}
}
class OtherClass()
{
OtherClass(A &a) {
a.doSomething();
}
}
After some investigation it seems like that using *this
slice the class B
. And then OtherClass
will call a pure virtual method on A
. I'm wrong? Should I make the OtherClass(*this)
, after the Initialization of the B
class?