Reputation: 430
I thought i knew pointers but then as i study run time polymorphism/dynamic binding, i've seen a very different use for pointers. Here are my 3 questions, they're all regarding the single line code below:
#include <iostream>
class Base {
public:
virtual void fun (int x) { std::cout << "base" << std::endl; }
};
class Derived : public Base {
public:
void fun (int x) { std::cout << "base" << std::endl; }
};
int main() {
//--------------------------------------
//1. Pointer can only hold memory address, 'Derived()' is calling the default constructor it is not a memory address
//2. What's the point of putting this into heap?
//3. I thought pointers could only hold memory address of the same datatype/class
Base* obj = new Derived();
//--------------------------------------
obj->fun(5);
return 0;
}
Upvotes: 0
Views: 34
Reputation: 108651
Pointers can point to anything with a memory address.
A pointer to an instance of a derived class can be handled as if it were a pointer to the base class, even if the base class itself is abstract (unable to be instantiated itself). That's how polymorphism works in C++.
Why put a pointer to an object on the stack and the object itself into the heap? Many reasons, some of them good and others not so good.
Upvotes: 0
Reputation: 29022
- Pointer can only hold memory address, 'Derived()' is calling the default constructor it is not a memory address
A new
expression does multiple things. First, it allocates enough memory for the type of object. Then, it calls the constructor for that type at the allocated memory. Then, it returns a pointer to the newly created object. Pointers point to objects. new
returns a pointer to an object. So there is no problem here, obj
will now point to the newly created Derived
object.
- What's the point of putting this into heap?
It isn't strictly necessary to dynamically create an object here. You could just as easily have the following and illustrate polymorphism :
Derived foo;
Base* obj = &foo;
Maybe the author of this example didn't think of that, or maybe they wanted to illustrate the mechanics of new
at the same time. You'd have to ask them.
- I thought pointers could only hold memory address of the same datatype/class
It is true that obj
is a Base*
so it can only point to a Base
type object. But a Derived
object is also a Base
object, so obj
can easily point to a Derived
object. That is what public
inheritance achieves. The : public Base
part of class Derived : public Base
means Derived
is also a Base
.
Upvotes: 1
Reputation: 63124
- Pointer can only hold memory address, 'Derived()' is calling the default constructor it is not a memory address
The expression here is not Derived()
(which would indeed construct a temporary object), but new Derived()
, which is new
's specific syntax to allocate and construct an object with dynamic lifetime and return a pointer to it. Note that the corresponding delete
to end the object's lifetime is missing, but see the next point.
- What's the point of putting this into heap?
None. In particular, the lifetime of an object does not affect the use of pointers with it, nor are pointers required for polymorphism (references work just as well). In any case, dynamic allocation should be done with smart pointers, not naked new
s.
- I thought pointers could only hold memory address of the same datatype/class
That's still true: obj
is pointing at the Base
subobject of the Derived
object. C++ provides an implicit pointer-adjusting conversion from Derived *
to Base *
(and Derived &
to Base &
as well) to facilitate the use of polymorphism.
Upvotes: 2