Sign in to follow this  

help me with a design issue

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

first I have a class Base which has a member of class BaseMember:
class Base
{
public:
    //other stuffs
    BaseMember bm;
};



then I have a class Derived:
class Derived : public Base
{
public:
    //other stuffs
    DerivedMember dm;
};



where DerivedMember is derived from BaseMember. now I have a function that accepts an arg of type Base* and manipulates its member:
void foo(Base* b)
{
    //do something with b->bm
}



how can I make this function work on Derived type as well? i.e. passing it a Derived* object d and it will automatically use d->dm instead of b->bm ?

Share this post


Link to post
Share on other sites
Use virtual functions. Like this:
class Base
{
public:
//other stuffs
BaseMember bm;

virtual void doit()
{
// do something with bm
}
};
class Derived : public Base
{
public:
//other stuffs
DerivedMember dm;

virtual void doit()
{
// do something with dm
}
};

void foo(Base* b)
{
b->doit(); // will call Base::doit() or Derived::doit() depending on what b points to
}


Try to avoid "run-time type information" (RTTI) and dynamic_cast.

Share this post


Link to post
Share on other sites
First of all, 'BaseMember bm' will be a member of both 'Derived' and 'Base'.

Assuming that is as intended:
What you want to do is instead have a virtual function 'foo' in your 'Base' class (no parameters), and a virtual function 'foo' in your 'Derived' class.

If that was not intended, then there should be a third abstract base class for both of these classes which has neither 'bm', or 'dm', and has a pure virtual 'foo' method.
The other two derived classes would have one of the 'bm' OR 'dm' data types respectively, and a virtual 'foo' function to operate on just that data.

Share this post


Link to post
Share on other sites
You can't, without providing some method of RTTI. If you will always know the true type of the object however, and not refer to a derived object via a pointer to the base class, you could implement an overloaded version of the function which takes a derived class as a parameter.

Chances are though, you should be doing this differently. If you want something about the derived class to override something in the base class, which it seems you do, since you want this function to use a derived member instead of the base member, chances are, you want something implemented using a virtual function, which the derived class overrides. Hard to comment on it though without more information about the exact problem.

Share this post


Link to post
Share on other sites

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this