473,795 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pure Virtual Function Hiding

A very simple example of something that acts quite odd:

class TopClass
{
public:
TopClass(){ }
virtual ~TopClass(){ }
virtual int myFunc() = 0;
};

class MiddleClass : public TopClass
{
public:
MiddleClass() : TopClass(){ }
virtual ~MiddleClass(){ }
virtual int myFunc( int x ) { return TopClass::myFun c()+x; }
};

class BottomClass : public MiddleClass
{
public:
BottomClass() : MiddleClass(){ }
virtual ~BottomClass(){ }
virtual int myFunc() { return 5; }
};

int
main( int argc, char* argv[] )
{
BottomClass c1;
c1.myFunc( 4 ); //will not compile: "'BottomClass:: myFunc' : function
does not take 1 arguments"
return 0;
}

It seems that an instantiation of BottomClass cannot "see" the version
of myFunc that takes an int, declared in MiddleClass. However, both are
public and virtual, so why not?

I'm sure there is some very subtle reason why (probably having to do
with virtual function naming and overloading), but if anyone could
shine some light on this, we'd appreciate it. Some very experienced C++
guys I know have looked at it, without much luck.

Thanks all!

Mar 6 '06 #1
6 2274
Ryan H. wrote:
A very simple example of something that acts quite odd:

class TopClass
{
public:
TopClass(){ }
virtual ~TopClass(){ }
virtual int myFunc() = 0;
};

class MiddleClass : public TopClass
{
public:
MiddleClass() : TopClass(){ }
virtual ~MiddleClass(){ }
virtual int myFunc( int x ) { return TopClass::myFun c()+x; }
};
That looks a bit fishy. TopClass::myFun c is pure virtual.
class BottomClass : public MiddleClass
{
public:
BottomClass() : MiddleClass(){ }
virtual ~BottomClass(){ }
virtual int myFunc() { return 5; }
};

int
main( int argc, char* argv[] )
{
BottomClass c1;
c1.myFunc( 4 ); //will not compile: "'BottomClass:: myFunc' : function
does not take 1 arguments"
return 0;
}
Thats right.
It seems that an instantiation of BottomClass cannot "see" the version
of myFunc that takes an int, declared in MiddleClass. However, both are
public and virtual, so why not?


Because the myFunc in BottomClass hides the definition of myFunc in
TopClass (because it's prototype is different). Add a using myFunc to
the BottomClass definition to unhide it.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 6 '06 #2
Ryan H. wrote:

I'm sure there is some very subtle reason why (probably having to do
with virtual function naming and overloading), but if anyone could
shine some light on this, we'd appreciate it.


It has nothing to do with virtual. Take out the virtual tags and you'll
have the same problem. (Take out TopClass and you'll have the same
problem, too.)

Overloading only occurs among functions defined in the same scope.
BottomClass only defines one version of myFunc, and it takes no
arguments. End of story.

To get the overloading that you seem to want, hoist MiddleClass::my Func
into BottomClass with 'using MiddleClass::my Func'.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 6 '06 #3
Ryan H. wrote:
A very simple example of something that acts quite odd:

class TopClass
{
public:
TopClass(){ }
virtual ~TopClass(){ }
virtual int myFunc() = 0;
};

class MiddleClass : public TopClass
{
public:
MiddleClass() : TopClass(){ }
virtual ~MiddleClass(){ }
virtual int myFunc( int x ) { return TopClass::myFun c()+x; }
};

class BottomClass : public MiddleClass
{
public:
BottomClass() : MiddleClass(){ }
virtual ~BottomClass(){ }
virtual int myFunc() { return 5; }
};

int
main( int argc, char* argv[] )
{
BottomClass c1;
c1.myFunc( 4 ); //will not compile: "'BottomClass:: myFunc' : function
does not take 1 arguments"
return 0;
}

It seems that an instantiation of BottomClass cannot "see" the version
of myFunc that takes an int, declared in MiddleClass. However, both are
public and virtual, so why not?

I'm sure there is some very subtle reason why (probably having to do
with virtual function naming and overloading), but if anyone could
shine some light on this, we'd appreciate it. Some very experienced C++
guys I know have looked at it, without much luck.


Yes, the name "myFunc" in the BottomClass is said to "hide" the
identical name in the MiddleClass from which it inherits. I believe the
motivation for this behavior is avoid accidentally pulling in some
inherited name that happens to be the same as the one in the local
class that is more likely to be the name which the programmer intended
to reference.

In any case, the programmer can "unhide" the name in the base class
with a using declaration. So adding:

using MiddleClass::My Func;

to BottomClass's declaration should unhide MyFunc in the MiddleClass.
Once unhidden, code in the BottomClass should be able to call it.

Greg

Mar 6 '06 #4
Sorry if there was any confusion created over the TopClass::myFun c
reference. The myFunc function in MiddleClass could just read

virtual int myFunc( int x ) { return 5; }

and it would still generate the error. The basic question I have is why
does the myFunc( int ) hide myFunc()? As a side note, adding a pure
virtual myFunc() to TopClass won't help either.

Put another way, in the classic Shapes examples, if there was a pure
virtual draw() function, and I inherited a 3DShape class with a virtual
draw( int perspective ), then my 3DBoxShape class couldn't call 3D's
draw( SIDE ), only the top-level draw().

I understand you can use using to unhide, but you shouldn't have to.
myFunc with the int is a member of MiddleClass, and therefore should
automatically be a member of BottomClass.

Mar 6 '06 #5
Greg wrote:

Once unhidden, code in the BottomClass should be able to call it.


Just a bit of a clarification: code in BottomClass (and code outside it,
talking to a BottomClass object) can call MiddleClass::my Func, provided
the name is explicitly qualified. In the original example, the
unqualified call doesn't work:

c1.myFunc( 4 ); //will not compile

But this works:

c1.MiddleClass: :myFunc( 4 ); // okay

However, adding a using declaration is, of course, almost always the
right answer.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 6 '06 #6
Ah, I see now. In my example, BottomClass was hiding MiddleClass's
myFunc. I was getting distracted worrying why it <u>appeared</u>
TopClass was somehow hiding MiddleClass. I understand now. Thanks
everyone for all your help, it was greatly appreciated!

Mar 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4369
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
6
3496
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
2340
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base pointer to point the right function with same signature in base and derived class like-- virtual void sum() { }
21
2581
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
3
3631
by: Thomas Kowalski | last post by:
Hi everyone, following situation: class A { void m(T value) =0; } class B : A {
10
4808
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
7
24545
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
10
2099
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think the reason behind this restriction... i just want to want a base class to be abstract so as to avoid object slicing into the base type from derived type, and in my case base class doesn't need to have a virtual function.
14
2758
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.