# [c++] wondering about virtual functions

This topic is 3854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

given you have a class QMainWindow BaseView derives from it. TraceView derives from BaseView in TraceView I overwrite a protected virtual function which is not overwritten in BaseView. The original version is in QMainWindow. If I now call BaseView::showEvent() from TraceView::showEvent() the compiler does not complain about baseview not implementing the function - so I first thought that he might call QMainWindow::showEvent() instead. In the stroustrup he states that using the scope operator on virtual functions denies the use of the virtual mechanism, which leads me to the conclusion that in the above case simply nothing will happen ... but why does the compiler not complain? any clues? €: I tried it out with a simple example and it seems that the QMainWindow version will be called ... i think it's simply because BaseView "has all the functions" of QMainWindow ... - thanks anyways

##### Share on other sites
When using public inheritance your saying that your derived class is built upon another class and contains all the members and methods of that class, although admittedly only public and protected members / methods will be accessible to it.

Scope operator does override (excuse pun) the virtual mechanism, but it depends on how you use it.

In your case though as you found out; The compiler is ok with it, as your calling a protected member of it's base class and therefore the derived class has the method anyway.

##### Share on other sites
Using the scope operator alone (::function) works as you expected (doesn't compile).

test.cpp:28: error: ‘::prot_f’ has not been declared

Perhaps the (base::function) syntax is meant specifically for resolving multiple inheritance issues? Is it a form of casting?

#include <iostream>class base{public:	base() {}	virtual ~base() {}	void operator()() { prot_f(); }protected:	virtual void prot_f() { std::cout << "base::prot_f\n"; }};class derive1: public base{public:	derive1(): base() {}	virtual ~derive1() {}};class derive2: public derive1{public:	derive2(): derive1() {}	virtual ~derive2() {}protected:	void prot_f()	{	std::cout << "derive2::prot_f\n";		::prot_f(); //ERROR		derive1::prot_f();	}};int main(){	base b; b();	derive1 d1; d1();	derive2 d2; d2();	return 0;}

##### Share on other sites
Quote:
 Original post by hydrooIf I now call BaseView::showEvent() from TraceView::showEvent() the compiler does not complain about baseview not implementing the function - so I first thought that he might call QMainWindow::showEvent() instead.

It does, as your tests show.

Quote:
 In the stroustrup he states that using the scope operator on virtual functions denies the use of the virtual mechanism

It does. But all that means is that writing BaseView::showEvent() will not try to find a virtual override for the function (which would end up calling the TraceView::showEvent() function again, leading to infinite recursion - so we're very glad the language doesn't work that way!). It says "look for the showEvent() function in the BaseView class and use that". And...

Quote:
 i think it's simply because BaseView "has all the functions" of QMainWindow ... - thanks anyways

It does, hence the observed behaviour. :)

@m3mb3rsh1p: ::prot_f() tries to look up a global function named 'prot_f'. You get an error because there is no such function. Using scope resolution to get at base class functions can be used to resolve multiple inheritance issues, but it's often used instead to "use the base version of the function and then do a little extra work specific to the derived class" - although this is often bad design. I wouldn't call it "a form of casting", since if you actually did cast to the base class and then called the function normally, it would still exhibit polymorphism.

##### Share on other sites
Sorry, I broke the first rule of answering questions :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015445
×