Search code examples
c++new-operatorstatic-binding

static binding with new operator c++


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?


Solution

  • 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