casting in VC++ 6

Discussion in 'C++' started by hswerdfe, Aug 10, 2005.

  1. hswerdfe

    hswerdfe Guest

    I have several Classes that derive from a common Base Class
    Each of these Classes Has a Differnt Version of a Function with the Same
    Short name, but different Long Name.

    See Bellow:

    // Base Class
    class CBase
    {
    void DoStuff(CBase* other);
    }

    //Derived Class 1
    class CDerived1: public CBase
    {
    void DoStuff(CDerived1* other);
    }

    // Derived Class 2
    class CDerived1: public CBase
    {
    void DoStuff(CDerived1* other);
    }


    What I want to do is Create A new object of the Same Type that is being
    passed into my function
    then call the apropriate function
    my code currently looks like this:

    void OtherFunction(CBase* MyDerived)
    {
    CBase* myNewBase = (CBase*)
    MyDerived->GetRuntimeClass()->CreateObject();
    myNewBase->DoStuff(MyDerived);
    }

    but this always calls CBase::DoStuff(CBase* other);
    and I want it to call CDerived1::DoStuff(CDerived1* other);
    or one of the other derived functions
    the only way I can think of doing this is to use a lot of

    if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
    //cast then call
    if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
    //cast then call
    if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
    //cast then call


    is there a better way to call the Derived Classes Member function?


    thanks,

    how
    hswerdfe, Aug 10, 2005
    #1
    1. Advertising

  2. hswerdfe wrote:
    > I have several Classes that derive from a common Base Class
    > Each of these Classes Has a Differnt Version of a Function with the Same
    > Short name, but different Long Name.
    >
    > See Bellow:
    >
    > // Base Class
    > class CBase
    > {
    > void DoStuff(CBase* other);
    > }
    >
    > //Derived Class 1
    > class CDerived1: public CBase
    > {
    > void DoStuff(CDerived1* other);
    > }
    >
    > // Derived Class 2
    > class CDerived1: public CBase
    > {
    > void DoStuff(CDerived1* other);
    > }
    >
    >
    > What I want to do is Create A new object of the Same Type that is being
    > passed into my function
    > then call the apropriate function
    > my code currently looks like this:
    >
    > void OtherFunction(CBase* MyDerived)
    > {
    > CBase* myNewBase = (CBase*)
    > MyDerived->GetRuntimeClass()->CreateObject();
    > myNewBase->DoStuff(MyDerived);
    > }
    >
    > but this always calls CBase::DoStuff(CBase* other);
    > and I want it to call CDerived1::DoStuff(CDerived1* other);
    > or one of the other derived functions
    > the only way I can think of doing this is to use a lot of
    >
    > if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
    > //cast then call
    > if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
    > //cast then call
    > if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
    > //cast then call
    >
    >
    > is there a better way to call the Derived Classes Member function?


    The only way I know is to let the derived classes handle it all. It's
    called polymorphism and is created with _virtual_ functions:

    class Base {
    public
    virtual void DoStuff(Base*);
    };

    class Derived1 : Base {
    /* non-virtual */ void DoStuff(Derived1 *pd); // whatever
    void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
    Derived1* pd1 = dynamic_cast<Derived1*>(pb);
    if (pd1)
    this->DoStuff(pd1);
    }
    };

    class Derived2 : Base {
    /* non-virtual */ void DoStuff(Derived2 *pd); // whatever
    void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
    Derived2* pd2 = dynamic_cast<Derived2*>(pb);
    if (pd2)
    this->DoStuff(pd2);
    }
    };

    This is a very short reply, may be considered incomplete, but look it over
    and ask more questions if you get any. Along with it, read about virtual
    functions in your favourite C++ book.

    Oh, and we don't talk VC++ specific stuff here. If you need help with any
    of Visual C++ specific things, try 'microsoft.public.vc.language'.

    V
    Victor Bazarov, Aug 10, 2005
    #2
    1. Advertising

  3. hswerdfe

    hswerdfe Guest

    thanks, this is the conclusion I came to as well.
    all the 'DoStuff' functions are already virtual.
    problem is
    CBase, CDerived1, CDerived2....
    have all been created by my boss and he mostly considers them untouchable.

    sorry about the off topic Post
    I just reposted to 'microsoft.public.vc.language'

    p.s. if you have any other sollutions let me know on either board



    "Victor Bazarov" <> wrote in message
    news:HkrKe.28270$01.us.to.verio.net...
    > hswerdfe wrote:
    >> I have several Classes that derive from a common Base Class
    >> Each of these Classes Has a Differnt Version of a Function with the Same
    >> Short name, but different Long Name.
    >>
    >> See Bellow:
    >>
    >> // Base Class
    >> class CBase
    >> {
    >> void DoStuff(CBase* other);
    >> }
    >>
    >> //Derived Class 1
    >> class CDerived1: public CBase
    >> {
    >> void DoStuff(CDerived1* other);
    >> }
    >>
    >> // Derived Class 2
    >> class CDerived1: public CBase
    >> {
    >> void DoStuff(CDerived1* other);
    >> }
    >>
    >>
    >> What I want to do is Create A new object of the Same Type that is being
    >> passed into my function
    >> then call the apropriate function
    >> my code currently looks like this:
    >>
    >> void OtherFunction(CBase* MyDerived)
    >> {
    >> CBase* myNewBase = (CBase*)
    >> MyDerived->GetRuntimeClass()->CreateObject();
    >> myNewBase->DoStuff(MyDerived);
    >> }
    >>
    >> but this always calls CBase::DoStuff(CBase* other);
    >> and I want it to call CDerived1::DoStuff(CDerived1* other);
    >> or one of the other derived functions
    >> the only way I can think of doing this is to use a lot of
    >>
    >> if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
    >> //cast then call
    >> if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
    >> //cast then call
    >> if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
    >> //cast then call
    >>
    >>
    >> is there a better way to call the Derived Classes Member function?

    >
    > The only way I know is to let the derived classes handle it all. It's
    > called polymorphism and is created with _virtual_ functions:
    >
    > class Base {
    > public
    > virtual void DoStuff(Base*);
    > };
    >
    > class Derived1 : Base {
    > /* non-virtual */ void DoStuff(Derived1 *pd); // whatever
    > void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
    > Derived1* pd1 = dynamic_cast<Derived1*>(pb);
    > if (pd1)
    > this->DoStuff(pd1);
    > }
    > };
    >
    > class Derived2 : Base {
    > /* non-virtual */ void DoStuff(Derived2 *pd); // whatever
    > void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
    > Derived2* pd2 = dynamic_cast<Derived2*>(pb);
    > if (pd2)
    > this->DoStuff(pd2);
    > }
    > };
    >
    > This is a very short reply, may be considered incomplete, but look it over
    > and ask more questions if you get any. Along with it, read about virtual
    > functions in your favourite C++ book.
    >
    > Oh, and we don't talk VC++ specific stuff here. If you need help with any
    > of Visual C++ specific things, try 'microsoft.public.vc.language'.
    >
    > V
    hswerdfe, Aug 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Nguyen
    Replies:
    6
    Views:
    18,714
  2. Toby
    Replies:
    3
    Views:
    3,060
    Mike Treseler
    Sep 7, 2005
  3. kevin
    Replies:
    11
    Views:
    5,782
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    508
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    547
    James Kanze
    Dec 20, 2009
Loading...

Share This Page