Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

Abstract classes, and virtual function question

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

Hi, I've got an abstract class called CWindow. It has a virtual method called run(), eg class CWindow { protected: virtual run() = 0; } So basically I want to make it if I dont want to use the run function I don't have to entered it into the class that is using the CWindow. eg class Main:public CWindow { public: //and no run function Main(){} }; if I try this i get some function not declared error. so what can i do so i dont get this errror? eg if i was making a program that was purely event based, so I wouldn't need the run function which is just the loop that is called inside the loop that all win32 apps have. eg translate msg, while(msg!=WM_QUIT)...

Share this post


Link to post
Share on other sites
Advertisement
All concrete classes ( ie ones you can create ) must have no pure virtual methods, so if they derive from a pure virutal class they'll need to implement the pure virutal methods before you can instantiate them.

Most times you'll just implement it as a stub if you don't need it, another way to do it is with a templated traits class, ATL uses this method quite frequetnly.

Cheers
Chris

Share this post


Link to post
Share on other sites
Well, if you were making a class that didn't use the run() function, then you wouldn't want your class to be derived from Window. This is one of the basic tenants of object oriented design.

Perhaps your Main class should *contain* a Window object rather than descending from it. Read up on some basic OOP (object oriented programming) principles and then go back to the drawing board with your design.

But, the overall answer to your question is that you shouldn't be deriving Main from Window at all. If it doesn't make sense for a run() method to be present, it should not be derived from a base class that has the run() method. This is the answer you should focus on.

The direct answer to your question is that you cannot create an instance of an object (Main) that contains a pure virtual function (CWindow::run). There is no way around this restriction. You cannot create an object that has a totally non-existant function. In other words, because it does not define run(), Main is still abstract. This is not the answer you should focus on, but it's good to know nonetheless.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!