473,671 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Masking a virtual function?

The subject line probably isn't the best, but I don't know how else to
describe my question.

When I try to compile the code below, my compiler reports an error
(error text is below code sample).

class GrandParent {
virtual void doSomething() = 0;
};

class Parent : public GrandParent {
void doSomething() { std::cout << "blah"; }
};

class Child : public Parent {
bool doSomething() {
Parent::doSomet hing();
return true;
}
}

Error text is:
error C2555: 'Child::doSomet hing': overriding virtual function return
type differs and is not covariant from 'Parent::doSome thing'
I thought that since Parent's function isn't virtual, I should be able
to define an entirely unrelated function in Child that just happens to
have the same name. Is my compiler being overly picky or am I wrong?

Thanks.

Jan 28 '06 #1
5 2396
On 28 Jan 2006 13:37:12 -0800, "Squeamizh" <sq*****@hotmai l.com>
wrote:
The subject line probably isn't the best, but I don't know how else to
describe my question.

When I try to compile the code below, my compiler reports an error
(error text is below code sample).

class GrandParent {
virtual void doSomething() = 0;
};

class Parent : public GrandParent {
void doSomething() { std::cout << "blah"; }
};

class Child : public Parent {
bool doSomething() {
Parent::doSomet hing();
return true;
}
}

Error text is:
error C2555: 'Child::doSomet hing': overriding virtual function return
type differs and is not covariant from 'Parent::doSome thing'
I thought that since Parent's function isn't virtual, I should be able
to define an entirely unrelated function in Child that just happens to
have the same name. Is my compiler being overly picky or am I wrong?


Unfortunately, you are wrong. Parent's function is virtual if the base
class function is virtual, whether or not Parent::doSomet hing() is
specified to be virtual. IOW, "virtual" propagates down the class
hierarchy.

--
Bob Hairgrove
No**********@Ho me.com
Jan 28 '06 #2
Squeamizh wrote:
The subject line probably isn't the best, but I don't know how else to
describe my question.

When I try to compile the code below, my compiler reports an error
(error text is below code sample).

class GrandParent {
virtual void doSomething() = 0;
};

class Parent : public GrandParent {
void doSomething() { std::cout << "blah"; }
};

class Child : public Parent {
bool doSomething() {
Parent::doSomet hing();
return true;
}
}

Error text is:
error C2555: 'Child::doSomet hing': overriding virtual function return
type differs and is not covariant from 'Parent::doSome thing'
I thought that since Parent's function isn't virtual, I should be able
to define an entirely unrelated function in Child that just happens to
have the same name. Is my compiler being overly picky or am I wrong?

Thanks.


Imagine a situation like the following:

Child child;
child.doSomethi ng();

Should this invoke child.Child::do Something() or
child.Parent::d oSomething()? AFAIK the C++ standard inhibits the usage
you described to prevent this sort of ambiguity. Since the return type
can be discarded it is not enough to establish a function as being
different from another.

Regards,

Ben
Jan 28 '06 #3
Squeamizh wrote:
The subject line probably isn't the best, but I don't know how else to
describe my question.

When I try to compile the code below, my compiler reports an error
(error text is below code sample).

class GrandParent {
virtual void doSomething() = 0;
};

class Parent : public GrandParent {
void doSomething() { std::cout << "blah"; }
};

class Child : public Parent {
bool doSomething() {
Parent::doSomet hing();
return true;
}
}

Error text is:
error C2555: 'Child::doSomet hing': overriding virtual function return
type differs and is not covariant from 'Parent::doSome thing'
That's right.
I thought that since Parent's function isn't virtual,
It is virtual. A function that is virtual in a base class cannot be
"devirtuali zed" in a derived class.
I should be able to define an entirely unrelated function in Child that
just happens to have the same name.
Yes, if its signature (i.e. parameter list or the c-v qualifiers) differs.
You can't overload functions only by different return type. Btw: This has
nothing to do with the function being virtual.
Is my compiler being overly picky or am I wrong?


You are wrong.

Jan 28 '06 #4
Squeamizh wrote:
class GrandParent {
virtual void doSomething() = 0;
};

class Parent : public GrandParent {
void doSomething() { std::cout << "blah"; }
};

class Child : public Parent {
bool doSomething() {
Parent::doSomet hing();
return true;
}
}

Error text is:
error C2555: 'Child::doSomet hing': overriding virtual function return
type differs and is not covariant from 'Parent::doSome thing'

I thought that since Parent's function isn't virtual, I should be able
to define an entirely unrelated function in Child that just happens to
have the same name. Is my compiler being overly picky or am I wrong?


Your compiler's right. I was asking about basically this same issue a
few weeks back. Basically, it turns out that "virtual" cascades
downward through an inheritance hierarchy, so Parent::doSomet hing *is*
virtual.

Now, a derived class can *hide* a base class's virtual by defining
another function of the same name but a different signature. Note that
the return value is not part of the signature. The only way you can
provide a different return value is if it is a covariant return type --
that is, if it is a pointer or reference to a type T2, where the parent
returns a pointer or reference (respectively) to a type T1, where T2
inherits from T1. In other words, for return-by-reference, you can be
"more specific" about what you're returning, since doing so simply
provides a tighter contract and doesn't break any existing guarantees.

Luke

Jan 28 '06 #5

"Squeamizh" <sq*****@hotmai l.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| The subject line probably isn't the best, but I don't know how else to
| describe my question.
|
| When I try to compile the code below, my compiler reports an error
| (error text is below code sample).
|
| class GrandParent {
| virtual void doSomething() = 0;
| };
|
| class Parent : public GrandParent {
| void doSomething() { std::cout << "blah"; }
| };
|
| class Child : public Parent {
| bool doSomething() {
| Parent::doSomet hing();
| return true;
| }
| }
|
| Error text is:
| error C2555: 'Child::doSomet hing': overriding virtual function return
| type differs and is not covariant from 'Parent::doSome thing'
|
|
| I thought that since Parent's function isn't virtual, I should be able
| to define an entirely unrelated function in Child that just happens to
| have the same name. Is my compiler being overly picky or am I wrong?

Any member function that is virtual in the base or abstract class is
virtual in the entire inheritence hierarchy.

The return type of a function is not part of its signature. Whether that
function is a non-member, a member function or a virtual member function
is irrelevent.
Jan 29 '06 #6

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

Similar topics

4
2282
by: Piotr | last post by:
Please help me, I can't solve this problem: I have domain: www.main_domain.com/index.php?userid=23&parametr1=2&parametr2=3 (and other site files like www.main_domain.com/page1.php?userid=23&otherparameter=32 www.main_domain.com/page2.php?userid=23&otherparameter=11 etc.) At this address there is an template site for many users. Personal site is identified by userid.
8
1357
by: Mike | last post by:
I have a system that will display files to numerous clients. Each folder will have unique logins so clients will not be able to see each others folders or files. I plan on them using directory browse to get to their files. My problem is this Say I have "clientfolder\xxx" and "clientfolder\yyy" and
11
4351
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.
2
3952
by: Ginchy | last post by:
I have uploaded a small 3 page web using MS Publisher 2003 and after uploading I switched on url masking to cloak the url. I am certain that it worked fine. I simply changed the colour scheme in Publisher and reuploaded the page. The links no longer work? When I switch off url masking it works as it should?
1
3100
by: NBB | last post by:
I can't figure this one out. Here's the situation, should be pretty for the pros in here. I have a ListBox that is populated with the DataTextField and DataValueField flags of the DataSet. Everything populates just fine and works great. The DataTextField displays text from a database column called Path, which is the absolute URL of a file. What I would like it to display is just the filename, essentially trimming off everything...
1
1530
by: Klaus Baumgart | last post by:
I try to send an Email by using the submit in a html Formular. The body of the Email should have XML Tags. How can I using masking to get a proper XML-File Klaus <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
3
2298
by: ara | last post by:
Hi, I have got a table which pops up when the pointer is placed over a link,as <div id="metapopup" onMouseOver="showmeta();" onMouseOut="hidemeta();"> <table border="0" cellpadding="5" cellspacing="0"> <tr> <td><a href="#">Home </a>
17
3526
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
9
2264
by: Daniel Smedegaard Buus | last post by:
Hey all :) I was wondering about the $error_types (I particularly notice the 's' suffix when reading the manual) parameter for 'set_error_handler()': Can be used to mask the triggering of the error_handler function just like the error_reporting ini setting controls which errors are shown. Without this mask set the error_handler will be called for every error regardless to the setting of the error_reporting setting.
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
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
8667
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...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4221
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
2806
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
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.