Consider the following example when a slice of an object occurs during the dereferencing of a base pointer.
#include <stdio.h>
class Base {
public:
virtual void hello() {
printf("hello world from base\n");
}
};
class Derived : public Base{
public:
virtual void hello() {
printf("hello world from derived\n");
}
};
int main(){
Base * ptrToDerived = new Derived;
auto d = *ptrToDerived;
d.hello();
}
I want the variable to dsave the type object Derivedinstead of the type object Base, without dynamically allocating memory and without explicit casting.
I already examined this question , but the solution proposed in the answer requires dynamic allocation of memory, since it returns a pointer to a new object, not the value of a new object.
Is this possible in C ++ 11?
source
share