GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2005, 18:48
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

References to base class to access derived class only members


Hi!

Can a base class reference be used to access derived class-specific members? Note that the members are not part of the base class, only part of the derived class.

Thanks, Michelle
  #2  
Old 01-Dec-2005, 20:15
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 922
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: References to base class to access derived class only members


If anyone had caught my previous post, sorry. That was wrong.
The answer is :
NO.

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 03-Dec-2005, 20:07
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 922
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: References to base class to access derived class only members


Quote:
Originally Posted by mirizar
Can a base class reference be used to access derived class-specific members? Note that the members are not part of the base class, only part of the derived class.

OOPS!!.
It seems that it is possible by static_cast.
My first post holds good!!!


Here is how we can do it:
Code:
static_cast<Derived*>(&reference_variable)->DerivedMember(arguments);

So, consider a base class like this:
CPP / C++ / C Code:
class base
{   
public:
    void printbase()
    {
        cout<<"Hello there from base...";
    }
};
A derived class:
CPP / C++ / C Code:
class derived:public base
{
public:
    void printderived()
    {
        cout<<"Hello there from derived..";
    }
};

In the main function, we can call the printderived function like this:
CPP / C++ / C Code:
    base *b = new base();

    static_cast<derived*>(b)->printderived();
Will print "Hello there from derived.."

But doing such thing is often due to a design flaw.
It is not recommended, and often leads to logical error.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 03-Dec-2005, 20:40
mirizar mirizar is offline
New Member
 
Join Date: Jun 2004
Posts: 20
mirizar is on a distinguished road

Re: References to base class to access derived class only members


Paramesh:

That makes sense! Thanks a lot for the input. I agree that this is not the best way to do things.

Thanks, Michelle
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fluid + class derived from Fl_Window iga FLTK Forum 11 31-May-2005 03:02
GIMcontacts - a gim fltk fluid venture cable_guy_67 FLTK Forum 0 14-Feb-2005 14:18
calling abstract base class method calls draw instead achoo FLTK Forum 1 19-Dec-2004 09:38
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 CPP / C++ Forum 2 19-Dec-2004 06:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.