By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,923 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,923 IT Pros & Developers. It's quick & easy.

Masking a virtual function?

P: n/a
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::doSomething();
return true;
}
}

Error text is:
error C2555: 'Child::doSomething': overriding virtual function return
type differs and is not covariant from 'Parent::doSomething'
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
Share this Question
Share on Google+
5 Replies


P: n/a
On 28 Jan 2006 13:37:12 -0800, "Squeamizh" <sq*****@hotmail.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::doSomething();
return true;
}
}

Error text is:
error C2555: 'Child::doSomething': overriding virtual function return
type differs and is not covariant from 'Parent::doSomething'
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::doSomething() is
specified to be virtual. IOW, "virtual" propagates down the class
hierarchy.

--
Bob Hairgrove
No**********@Home.com
Jan 28 '06 #2

P: n/a
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::doSomething();
return true;
}
}

Error text is:
error C2555: 'Child::doSomething': overriding virtual function return
type differs and is not covariant from 'Parent::doSomething'
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.doSomething();

Should this invoke child.Child::doSomething() or
child.Parent::doSomething()? 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

P: n/a
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::doSomething();
return true;
}
}

Error text is:
error C2555: 'Child::doSomething': overriding virtual function return
type differs and is not covariant from 'Parent::doSomething'
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
"devirtualized" 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

P: n/a
Squeamizh wrote:
class GrandParent {
virtual void doSomething() = 0;
};

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

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

Error text is:
error C2555: 'Child::doSomething': overriding virtual function return
type differs and is not covariant from 'Parent::doSomething'

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::doSomething *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

P: n/a

"Squeamizh" <sq*****@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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::doSomething();
| return true;
| }
| }
|
| Error text is:
| error C2555: 'Child::doSomething': overriding virtual function return
| type differs and is not covariant from 'Parent::doSomething'
|
|
| 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 discussion thread is closed

Replies have been disabled for this discussion.