Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using Ptr of derived class to point to base class and viceversa

Reply
Thread Tools

Using Ptr of derived class to point to base class and viceversa

 
 
Bhan
Guest
Posts: n/a
 
      09-19-2005
Using Ptr of derived class to point to base class and viceversa
class base
{
....
}
class derived : public base
{
....
}

I want to know any practical scenario when this is used
a. B b;
D *p = &b;

b. B *b;
D d;
b = &d;



Can any one explain it?I know it is theoritically OK.But i am not able
why to do such things?

Thanks in Advance,
Bhanu

 
Reply With Quote
 
 
 
 
placid
Guest
Posts: n/a
 
      09-19-2005

Bhan wrote:
> Using Ptr of derived class to point to base class and viceversa
> class base
> {
> ...
> }
> class derived : public base
> {
> ...
> }
>
> I want to know any practical scenario when this is used
> a. B b;
> D *p = &b;
>
> b. B *b;
> D d;
> b = &d;
>
>
>
> Can any one explain it?I know it is theoritically OK.But i am not able
> why to do such things?


doesnt this lead to the Slicing Problem ?

>
> Thanks in Advance,
> Bhanu


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-19-2005
placid wrote:
> Bhan wrote:
>
>>Using Ptr of derived class to point to base class and viceversa
>>class base
>>{
>>...
>>}
>>class derived : public base
>>{
>>...
>>}
>>
>>I want to know any practical scenario when this is used
>>a. B b;
>> D *p = &b;


No practical use of downcasting from an object not of the derived type.

>>
>>b. B *b;
>> D d;
>> b = &d;


Upcasting: Happens all the time.

>>
>>Can any one explain it?I know it is theoritically OK.But i am not able
>>why to do such things?

>
>
> doesnt this lead to the Slicing Problem ?


Slicing happens when you copy the object. The OP is simply taking
pointers to the object.
 
Reply With Quote
 
Bhan
Guest
Posts: n/a
 
      09-19-2005
So you mean this is normally used?
>Upcasting: Happens all the time.


B b;
D *p = &b;
But why do this ?why not have a ptr to base class itself ?(below)

B b;
B *p = &b;

Can you give the use of these two?
B b;
B *p = &b;
D *p = &b;

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      09-19-2005
"Bhan" <> wrote in message
news: oups.com
> Using Ptr of derived class to point to base class and viceversa
> class base
> {
> ...
> }
> class derived : public base
> {
> ...
> }
>
> I want to know any practical scenario when this is used
> a. B b;
> D *p = &b;


You shouldn't do this because the derived class will usually have members
that the base class does not. The derived class pointer will allow you to
try to access members of the derived class than don't exist in the base
class object, with possibly disastrous consequences.

> b. B *b;
> D d;
> b = &d;


This is safe and very useful, though it rarely occurs in the context that
you have shown. It occurs in two main contexts.

1. Containers of pointers. The container (an array or vector or whatever)
can usually only store pointers of a single type. If you want it to point to
both base and derived class objects, then you store base pointers and assign
to those base pointers the addresses of both base and derived class objects.
You can then iterate over all the members in the container, calling the same
function for each. If the function is virtual, then you can get a different
function for the pointers pointing to the derived class objects as compared
to the pointers pointing to the base class objects (this is known as
run-time polymorphism). For non-virtual functions, you get the same function
in both cases, but this is OK since the function is a member of both the
base and the derived class (with public inheritance, any public function in
the base class becomes a public function in the derived class).

2. Functions that take pointer arguments. Given, say:

Base b;
Derived d;

void foo(Base *ptr)
{
ptr->MemberFunction();
}

you can call this function using the address of either a base class or a
derived class object:

foo(&b);
foo(&d);

The effect of the first is:

ptr = &b;
ptr->MemberFunction();

The effect of the second is:

ptr = &d;
ptr->MemberFunction();


--
John Carson

 
Reply With Quote
 
Bhan
Guest
Posts: n/a
 
      09-19-2005
b. B *b;
> D d;
> b = &d;

Can I summarize my understanding like this?

Whenever you have the provision to store only base ptr,store the base
ptr that points to dervied object.

When u retrieve it,use static_cast and get the req derived object.Right?

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-19-2005
Bhan wrote:
> b. B *b;
>
>> D d;
>> b = &d;

>
> Can I summarize my understanding like this?
>
> Whenever you have the provision to store only base ptr,store the base
> ptr that points to dervied object.
>
> When u retrieve it,use static_cast and get the req derived object.Right?
>


static_cast if you are sure that the pointer points to the derived
class. Use dynamic_cast if you are not sure and you have a polymorphic
base class.

john
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      09-19-2005
"Bhan" <> wrote in message
news: oups.com
> b. B *b;
>> D d;
>> b = &d;

> Can I summarize my understanding like this?
>
> Whenever you have the provision to store only base ptr,store the base
> ptr that points to dervied object.


Yes.

> When u retrieve it,use static_cast and get the req derived
> object.Right?


Most of the time you don't need any cast. You call functions that are
declared in the base class and may or may not be virtual.

If you want to call a derived class function that does not exist in the base
class --- or does exist but is not virtual --- then a cast is necessary. See
John Harrison's remarks on this.

Note that these issues only arise in situations where you don't
automatically know the type of object pointed to, as with a container of
pointers that can point to both base and derived class objects. In such
contexts, it is simplest if you can make the same function call regardless
of what the pointer points to and you achieve different behaviour by using
virtual functions. Having to figure out what is pointed to --- and making
different function calls on that basis --- is inelegant and error prone.

If you have derived objects and only derived objects and you know it, then
you just use pointers to derived objects (if you use pointers at all) and
none of these complications arise.

--
John Carson

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      09-19-2005
Bhan wrote:
> b. B *b;
> > D d;
> > b = &d;

> Can I summarize my understanding like this?
>
> Whenever you have the provision to store only base ptr,store the base
> ptr that points to dervied object.
>
> When u retrieve it,use static_cast and get the req derived object.Right?


One thing to remember of course is that a pointer to a derived class is
also a pointer to the base class. It can be used anywhere a base class
pointer can be used. An instance of the derived class is an instance of
the base class.

And while "downcasts" from a base class to a derived class are
sometimes necessary, using them is not considered a good programming
practice. A lot of downcasts in source code generally indicate a
weakness in the program's class hierarchy or implementation. After all,
if the client has a base pointer it should not need to "know" about its
derived classes. That is the whole point of having an abstraction in
the first place. On the other hand if a client does need an object of
the derived class for some reason, then the question becomes how did it
wind up with a pointer to the base class.

Greg

 
Reply With Quote
 
Bhan
Guest
Posts: n/a
 
      09-19-2005
Thats fine.
I am satisfied with this.
So,Can I summarize my understanding like this?


Whenever you have the provision to store only base ptr,store the base
ptr that points to dervied object.


When u retrieve it,use dynamic_cast or static_cast(depending on the
situation)and get the req derived object.Right?

Thanks
Bhanu

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the point of doing "typeid(1 ? *ptr : *ptr)" ? Johannes Schaub C++ 11 08-07-2011 06:22 PM
Base ptr to access the derived members Subra C++ 4 11-30-2010 05:49 AM
Regarding deleting a base ptr which is pointings to derived object. Rajya C++ 2 02-18-2010 09:16 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57