Jump to content
  • Advertisement

Archived

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

TheRealBartman

Virtual Base Class Question

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

Hey, Say you have virtual base class A, and you have a class B that is derived from A. If you add a new member to class B, then no longer can you do this: A *ClassA; ClassA = new B; Or can you? I am trying to write a rendering factory that will support both DirectX and OpenGL, but each API requires something unique to themselves. IE OpenGL has PIXELFORMATDESCRIPTOR and DirectX doesn''t, so in the OpenGL derived class you would need a PIXELFORMATDESCRIPTOR in it but in the DirectX one you wouldn''t. I hope you can understand what I am asking, if not I will explain it better. Thanks, Michael Bartman

Share this post


Link to post
Share on other sites
Advertisement
Yes, a derived class can have members that its base class doesn''t, and you''ll still be able to access the derived class from a base class pointer. You just won''t be able to access members of the derived class that don''t exist in the base class. Example:


  
class A {
public:
int var1;
};

class B : public A {
public:
int var2;
};

int main() {
A *obj = new B; // no problem here

obj->var1 = 100; // still ok

obj->var2 = 100; // CAN''T DO THIS ONE

}

Share this post


Link to post
Share on other sites
Thanks DaTroof, I had a thought something like that would happen.

How about this, can I change the parameters that the methods require from whats in the base class, and have the changes work when using a base class pointer?

Michael Bartman


[edited by - TheRealBartman on April 20, 2003 11:53:03 PM]

Share this post


Link to post
Share on other sites
I am almost 100% sure that you cannot change the parameters for virtual functions. However, you can always add new member functions and just cast the pointer to a base class to a pointer to a derived class using (assuming you are using c++):

  
dynamic_cast<derived *>(basePtr);


EDIT: added source flags to fix code.

[edited by - YoshiN on April 20, 2003 12:33:41 AM]

Share this post


Link to post
Share on other sites
So it should look like:

  
Render *MyRender = dynamic_cast<DirectXRenderer *>(MyRender);

Where Render is the base class and DirectXRenderer is the derived?

When I do that, I get this error:

C:\Projects\DOE3\DOE_Render.cpp(8) : error C2683: dynamic_cast : 'Render' is not a polymorphic type
c:\projects\doe3\doe_render.h(2) : see declaration of 'Render'


[edited by - TheRealBartman on April 21, 2003 8:10:16 PM]

Share this post


Link to post
Share on other sites

  

CRenderer *renderer = someUnknownRenderer;
CDirectXRenderer *dxRenderer = dynamic_cast<CDirectXRenderer*>(someUnknownRenderer);
//if someUnknownRenderer actually *is* a DirectXRenderer then it converts the pointer - if it''s not, dxRenderer becomes NULL.




I believe it''s something more like that. You''ll need to turn on RTTI though.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Share this post


Link to post
Share on other sites
Well what I am trying to do, is create a rendering factory for my game engine. Maybe this helps explain what I am trying to accomplish better.


Even with RTTI on, I still get the error about polymorphic type






[edited by - TheRealBartman on April 21, 2003 11:36:37 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!