Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ironfroggy

derived classes with identical methods

This topic is 6329 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

say i have class A. and i have classes B and C inherited from A. and i have a pointer P that could point to either a B or a C type object. And i have method D(int E); in both B and C. I then do this: P->D(1); will this call the method of the B or C type object, whichever it is pointing too at the time?

Share this post


Link to post
Share on other sites
Advertisement
If D(int) is a virtual function, it will do B''s D func. if P points do a B, and C''s D func if P points to a C.

The following should work

  
class A
{
virtual int D(int e);
};

class B:public A
{
virtual int D(int e){ //do B stuff

}
};

class C:public A
{
virtual int D(int e){ //do C stuff

}
};

Share this post


Link to post
Share on other sites
i never said anything about virtual functions, im getting confused!

ok..
i have class ZBase
and the class ZBitmap : class ZBase
and class ZeroGL : class ZBase
and ZBitmaps and ZeroGLs both have a function of
FLAGS BltBitmapTo(ZBitmap *aFromPtr, UINT aX, UINT aY);
i start all my argument variables with a
and then i have
ZBase *Ptr;
can I use Ptr->BltBitmapTo(...) reguardless of if Ptr points to a ZBitmap or a ZeroGL?

Share this post


Link to post
Share on other sites
(using the first example)

If function D() in class A *is not* declared as virtual, calling D() from a pointer to B or C will call class A''s D().

If function D() in class A *is* declared as virtual, calling D() from a pointer to B or C will call the D() of B or C, respectively.

That''s what Big B was saying.

.travois.

Share this post


Link to post
Share on other sites
Also note that you don''t need to declare the method in B nor C as virtual, only in the base class The rule goes: "If you override a method, declare the overridden method as virtual". You should never ever override non-virtual method.

Cheers, Altair


"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." - Albert Einstein

Share this post


Link to post
Share on other sites
ok ok.. my confusion was in what the virtual part did, i couldnt remember. just looked it up, understand now.. except! Do I have to have the class the others are derived from have the virtual function? what if class B and C have each a D() and they are derived from class A which has no D()? will it work?

Share this post


Link to post
Share on other sites
Naturally it won''t work, because you try to access D() through base class (A) interface. The idea is, that you can pass any class which is derived from A as argument whatfor no assumptations can be made about the interface of derived classes _except_ that it has all methods that A do have. If you have declared some methods in A as virtual, some methods may also have new implementations in derived classes. If you have declared method in A as _pure_ virtual, derived class also has to implement the method.

Cheers, Altair


"Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." - Albert Einstein

Share this post


Link to post
Share on other sites
  
#include <iostream.h>

class A
{
public:
virtual void D() = 0;
};

class B : public A
{
public:
void D() { cout << "B :: D()" << endl; }
};

class C : public A
{
public:
void D() { cout << "C :: D()" << endl; }
};

void main()
{
A *P = new B;
A *P2 = new C;

P->D();
P2->D();

delete P;
delete P2;
}

// should print "B :: D()", "C :: D()", right?



-----------------
The Goblin
-----------------
"Oh, God..."
"Yes?" <- My Response

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!