Base and Derived Class

M

Michael

Hi,

Could you tell me whether the following two statement are the same?
Derived class is derived from Base class.

1. Base* ptr1;
2. Derived * ptr2;

Does it mean ptr1 is the same as ptr2?

Thanks in advance,
Michael
 
L

Luc The Perverse

Michael said:
Hi,

Could you tell me whether the following two statement are the same?
Derived class is derived from Base class.

1. Base* ptr1;
2. Derived * ptr2;

Does it mean ptr1 is the same as ptr2?

No.

The only thing you have listed are declarations. They could be set to point
the same address space, namely an address of an object of type Derived.

Then you could call any member functions of Base from the Base point and any
members of Derived from the derived pointer.

Note: Any virtual functions called from the address contained in Base* would
run from the derived classes data.
 
A

Andrey Tarasevich

Michael said:
...
Could you tell me whether the following two statement are the same?
Derived class is derived from Base class.

1. Base* ptr1;
2. Derived * ptr2;

No, they are not the same.
Does it mean ptr1 is the same as ptr2?

I don't understand what you mean here. Types of 'ptr1' and 'ptr2'? Values of
'ptr1' and 'ptr2'? Something else?
 
M

Michael

Luc said:
No.

The only thing you have listed are declarations. They could be set to point
the same address space, namely an address of an object of type Derived.

Then you could call any member functions of Base from the Base point and any
members of Derived from the derived pointer.

Note: Any virtual functions called from the address contained in Base* would
run from the derived classes data.

So a Base point could point to either Base object (complete object) or
Derived object (incomplete object). A Derived point could only point to
Derived object? Right?

Why not use Base* points to Base object and Derived points to Derived
object?
 
J

John Carson

Michael said:
So a Base point could point to either Base object (complete object) or
Derived object (incomplete object). A Derived point could only point
to Derived object? Right?

Why not use Base* points to Base object and Derived points to Derived
object?

Suppose you want to store pointers to Base objects and pointers to Derived
objects in the same container --- or, more likely, pointers to Derived1
objects and pointers to Derived2 objects. How would you do it, given that
the container will only store one type of pointer? Answer: by making the
container store base Base pointers.
 
T

Thorsten Kiefer

Michael said:
Hi,

Could you tell me whether the following two statement are the same?
Derived class is derived from Base class.

1. Base* ptr1;
2. Derived * ptr2;

Does it mean ptr1 is the same as ptr2?

Thanks in advance,
Michael

You can assign a Derived* to ptr1, but you cannot assign a Base* to ptr2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top