Skip to main content
gamedev.net
🔒 Locked

Inheriting partial methods?

Started by kaisellgren Apr 23, 2009 at 1:50 AM 4 replies 950+ views
Original Post
kaisellgren
kaisellgren
Hi, I have a small problem. I have a base class A and I have a class B which both have the same methods. The thing is, I want the method in A to be run, but I want the same method for B to be run after A to "complete" it. class A { public void do() {...} } class A : B { public void do() {... /* even more */ ...} }
phresnel
phresnel
Your code is neither valid C# nor valid C++, e.g. you derive A from A, and "do" is a reserved keyword. But let me show you both solutions (btw, posting on gamedev ;)

If this is C#:

class A {    public void do_() {        ...    }}class B : A {        new public void do_ () {             ...            base.do_ ()        }}


If this is C++:
class A {public:    void do_() {        ...    }};class B : public A {public:    void do_() {        ...        A::do_();    }};
Rewdew
Rewdew
And as an additional tip using phresnel's terms (hoho):
B instance;instance.A::do_(); // call A's methodinstance.B::do_(); // call B's method
kaisellgren
kaisellgren
Thanks!

I wonder why I do not see code tag controls in this writing form?
Zahlman
Zahlman
There is a FAQ linked at the top of every forum page. (It has an icon of an inner tube and magic wand.) You may want to read it :)
rip-off
rip-off
You could also do something like this:
class A{public:    void whatever()    {        whateverImpl();        finish();    }private:    virtual void whateverImpl() = 0;    void finish();};

Now all derived classes will always have the "finish" code executed when "whatever" is called.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.