Reputation: 155
i want to be sure if i understand that corectly. If i have something like that:
Base* wsk = new Derived
and if i've done that with static-binding than wsk is type of Base, but Derived type object is still created? And wsk
is pointing to that Derived type object, but it cannot use methods from derived class, because wsk
is Base type? To sum up the most important question for me is that if wsk
except that it's Base type is still pointing to new object which is Derived type?
Upvotes: 1
Views: 204
Reputation: 1110
One big remark, as mentioned above, when looking at Base *
it point to type Base
. I'm not using this notation in order to simplify the explanation.
What you are asking is a different question: what is the difference between the static type and the dynamic/runtime type of an object.
Let's observe the following code:
#import <iostream>
class Base
{
public:
virtual void foo()
{
std::cout << "From Base" << std::endl;
}
};
class A : public Base
{
public:
void foo() override
{
std::cout << "From A" << std::endl;
}
void some_A_only_function()
{
std::cout << "some function from A" << std::endl;
}
};
class B : public Base
{
public:
void foo() override
{
std::cout << "From B" << std::endl;
}
void some_B_only_function()
{
std::cout << "some function from B" << std::endl;
}
};
int main()
{
Base base{};
Base * a = new A();
Base * b = new B();
base.foo();
a->foo();
// a->some_A_only_function(); this won't compile: error: no member named 'some_A_only_function' in 'Base'
b->foo();
// b->some_B_only_function(); this won't compile either!!
}
And the output is:
From Base
From A
From B
All the objects here are from a static type of Base
, thus, you cannot call any function that isn't part of Base
, since the compiler cannot know which type is put in there in runtime. If we have overridden functions, the appropriate one will be called according to the runtime type (this is called dynamic dispatch).
To sum it up, there are 3 objects here:
Base
and dynamic type Base
.Base
and dynamic type A
.Base
and dynamic type B
.The only way to use the functions of A
or B
respectively, is by using casting in order to "return" them into their original form. But it should be done with caution!
Big remark: This is actually true in any language that offers polymorphism (as far as I know). It isn't unique to C++!
Upvotes: 3
Reputation: 63152
wsk
is of type Base *
, not Base
nor Derived
.
It was initialised with a value that was (implicitly) converted from type Derived *
. It points to a Base
object, which so happens to reside within a Derived
object.
That means that static_cast<Derived *>(wsk)
will give you a value that points to a Derived
.
It may be that the representation of wsk
is the same number as the representation of static_cast<Derived *>(wsk)
, i.e. that the address of the Base
object is the same as the address of the Derived
object, but that is not guaranteed
Upvotes: 2