473,668 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual function parameter variance


Hello all,

Quoting from page 24 of "The Boost Graph Library; User Guide and Reference
Manual":

"It turns out that by the contravariance subtyping rule, the parameter type
in the derived classes member function must be either the same type or a
base class of the type as the parameter in the base class."

Now please consider this code:

#include <iostream>

struct base_1 {};
struct derived_1: base_1 {};

struct base_2
{
virtual void foo(derived_1 *p) {std::cout << "base_2::foo()\ n";}
};

struct derived_2: base_2
{
virtual void foo(base_1 *p) {std::cout << "derived_2::foo ()\n";}
};

int main()
{
base_2 *ptr = new derived_2;
ptr->foo(new derived_1);
}

This outputs base_2::foo(). Why?

The quoted passage implies derived_2::foo( ) should override base_2::foo().
Clearly, it does not. Furthermore, I cannot find anything in the Standard
that indicates it should. So, I am clearly misinterpreting the quoted
passage. Can anybody explain to me what was meant in that passage?

Thank you,
Dave
Jul 22 '05 #1
3 5240
On Sat, 7 Aug 2004 23:38:14 -0700, "Dave" <be***********@ yahoo.com>
wrote:

Hello all,

Quoting from page 24 of "The Boost Graph Library; User Guide and Reference
Manual":

"It turns out that by the contravariance subtyping rule, the parameter type
in the derived classes member function must be either the same type or a
base class of the type as the parameter in the base class."

Now please consider this code:

#include <iostream>

struct base_1 {};
struct derived_1: base_1 {};

struct base_2
{
virtual void foo(derived_1 *p) {std::cout << "base_2::foo()\ n";}
};

struct derived_2: base_2
{
virtual void foo(base_1 *p) {std::cout << "derived_2::foo ()\n";}
};

int main()
{
base_2 *ptr = new derived_2;
ptr->foo(new derived_1);
}

This outputs base_2::foo(). Why?

The quoted passage implies derived_2::foo( ) should override base_2::foo().
Clearly, it does not. Furthermore, I cannot find anything in the Standard
that indicates it should. So, I am clearly misinterpreting the quoted
passage. Can anybody explain to me what was meant in that passage?

Thank you,
Dave


Unfortunately, it doesn't work the way you propose in C++.

Here is an interesting discussion about co- and contravariance WRT
C++. It is a little dated (1995), so maybe there is something new in
the works??

http://tinyurl.com/54f3d

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #2
On Sun, 08 Aug 2004 12:29:21 +0200, Bob Hairgrove
<wouldnt_you_li ke@to_know.com> wrote:
On Sat, 7 Aug 2004 23:38:14 -0700, "Dave" <be***********@ yahoo.com>
wrote:

Hello all,

Quoting from page 24 of "The Boost Graph Library; User Guide and Reference
Manual":

"It turns out that by the contravariance subtyping rule, the parameter type
in the derived classes member function must be either the same type or a
base class of the type as the parameter in the base class."

[snip]

Sorry, it wasn't your proposal ... I got out my copy of the BGL and
looked up the quoted passage. It is confusing to me, too.

In context of what follows (namely, replacing inheritance and virtual
functions with non-member template functions to ensure type safety) I
interpret the quoted passage as a kind of "wishful thinking". The
example given BEFORE the passage illustrates something entirely
different, namely an attempt to use covariant parameter types (which
don't exist in C++ and hence the resulting error message).

But contravariant parameter types, although conceptually feasible,
also don't exist in C++ (yet??). That's the only sense I can make of
it. Note that the second example (ColorPoint2) uses a Point* as
parameter, which is the same type as the base class function "equal()"
takes. It wouldn't work if the derived class took a ColorPoint2* as an
argument (that would be contravariance) because the derived class
equal() would then hide the base class function.

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #3
On Sun, 08 Aug 2004 13:24:45 +0200, Bob Hairgrove
<wouldnt_you_li ke@to_know.com> wrote:

[snip]
But contravariant parameter types, although conceptually feasible,
also don't exist in C++ (yet??). That's the only sense I can make of
it. Note that the second example (ColorPoint2) uses a Point* as
parameter, which is the same type as the base class function "equal()"
takes. It wouldn't work if the derived class took a ColorPoint2* as an
argument (that would be contravariance) because the derived class
equal() would then hide the base class function.


Oops ... taking a ColorPoint2* in ColorPoint2::eq ual() would yet again
be covariance!

Contravariance would mean that Point::equal() would have to take a
more derived type pointer, and ColorPoint2::eq ual() a Point*.
Nevertheless, ColorPoint2::eq ual() would still hide Point::equal() if
the argument types aren't exactly the same.

(Slippery stuff, this contravariance ... no wonder it hasn't yet made
it into the language...)

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #4

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

Similar topics

1
665
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual member function?
6
7583
by: Vajira | last post by:
Hello, Can you tell me why compilar does not recognize base class's virtual function in the following code? Is there is any limitation in C++, related to overloading virtual function of a base class in the derived class ? Compilar printed following message > g++ main2.cpp main2.cpp: In function `int main()': main2.cpp:21: no matching function for call to `B::Display(int)'
8
17755
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
2
2510
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
12
2102
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
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
2
3689
by: Markus Dehmann | last post by:
I have an abstract base class called Data. It has a pure virtual function virtual void write(std::ostream& out) =0; which writes the internal data to a stream. Now the problem is that this is not appropriate for some implementations of the class. Some implementations have an internal representation that they should rather write into several separate files. So for those, something like this would be more appropriate:
2
2406
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
10
1459
by: =?Utf-8?B?Y2FybG0=?= | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
0
8462
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
8381
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
8893
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
8583
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
7401
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...
1
6209
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
4205
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
2791
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
2023
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.