Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Multi inheritance and pure virtual


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 monkeyboi   Members   -  Reputation: 188

Like
0Likes
Like

Posted 27 January 2013 - 10:43 AM

Hi I have some question about multi inheritance see the code snippet below


class Window{
public:
	virtual void drawBorder() = 0;
	virtual void prompt() =0;
};

class BorderedWindow:public virtual Window{
public:
	void drawBorder(){}
};

class WindowWithMenu : public virtual Window{
public:
	void prompt(){}
};

class CustomWindow : public BorderedWindow, public WindowWithMenu{
public:
	void draw(){drawBorder(); prompt();}
};

 

My questions are

  1. why does derived class not have to implement all pure virtual functions declared in base class? Because it is virtual inheritance?
  2. Is :public virtual Window different from :virtual public Window?


Sponsor:

#2 wintertime   Members   -  Reputation: 1882

Like
1Likes
Like

Posted 27 January 2013 - 11:03 AM

1. That has nothing to do with virtual inheritance. Its to leave the possibility open that programmers make a class hierarchy with more than 2 steps where they continuously add common functionality first and later more specialized. If you try to make a BorderedWindow or WindowWithMenu you will see the compiler guards against this because its an abstract class like Window.
2. Should be the same if it both compiles.

Btw., you should always add a virtual destructor to any baseclass with a virtual method to prevent errors when you only have a pointer or reference to a baseclass which you initialized with an object of the derived class.

Edited by wintertime, 27 January 2013 - 11:05 AM.


#3 SiCrane   Moderators   -  Reputation: 9675

Like
0Likes
Like

Posted 27 January 2013 - 11:23 AM

In this case BorderedWindow::drawBorder() dominates WindowWithMenu::drawBorder() in CustomWindow and likewise WindowWithMenu::prompt() dominates BorderedWindow::prompt(). Since the dominant member functions are all non-pure virtual the CustomWindow is non-abstract.

Btw., you should always add a virtual destructor to any baseclass with a virtual method to prevent errors when you only have a pointer or reference to a baseclass which you initialized with an object of the derived class.

Note that this isn't necessary in C++11 anymore, as it makes the destructor implicitly virtual if a class has any virtual functions.

#4 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 27 January 2013 - 12:42 PM

In this case BorderedWindow::drawBorder() dominates WindowWithMenu::drawBorder() in CustomWindow and likewise WindowWithMenu::prompt() dominates BorderedWindow::prompt(). Since the dominant member functions are all non-pure virtual the CustomWindow is non-abstract.

Btw., you should always add a virtual destructor to any baseclass with a virtual method to prevent errors when you only have a pointer or reference to a baseclass which you initialized with an object of the derived class.

Note that this isn't necessary in C++11 anymore, as it makes the destructor implicitly virtual if a class has any virtual functions.
I can't find anything to back that up, and Visual Studio 2012 does not do it.

At the very least, it seems like a dangerous assumption, and could actually be wrong.

#5 monkeyboi   Members   -  Reputation: 188

Like
0Likes
Like

Posted 27 January 2013 - 05:21 PM

1. That has nothing to do with virtual inheritance. Its to leave the possibility open that programmers make a class hierarchy with more than 2 steps where they continuously add common functionality first and later more specialized. If you try to make a BorderedWindow or WindowWithMenu you will see the compiler guards against this because its an abstract class like Window.
2. Should be the same if it both compiles.

Btw., you should always add a virtual destructor to any baseclass with a virtual method to prevent errors when you only have a pointer or reference to a baseclass which you initialized with an object of the derived class.

ok so for pure virtual functions, you dont necessarily have to implement the pure function in the NEXT level derived class. As long as in the hierarchy, derived class(that could be instantiated) has implemented all pure virtual functions from his "higher level base classes" no matter how many levels it derives?

 

That code was just for demostrating the problem. Thanks for heads up tho. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS