473,419 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Help me understand Overriding a virtual function

I read that the return type has to be exactly same for a virtual
function to be overriden. While testing something, I discovered
something I cannot understand.
I cannot understand why the code below compiles.

I see that it won't compile if I have "virtual char & vtest() { }" in
Base class
and have "virtual const char & vtest() { }" in Derived class.

But it compiles if I have "virtual const char & vtest() { }" in Base
class and have "virtual char & vtest() { }" in derived class.
Why? What's going on?

Similarly, operator*() functions compiles as written, but always
returns const int & even I assign derived object to Base reference.
Please have a look at comments on function fun() to see what I mean.

Can anyone kindly explain why it compiles yet won't do dynamic
binding?
Thanks always (especially to you, Victor B.)

#include <iostream>
using std::cout;
using std::endl;

class CPtr {
public:
CPtr(int * a):ptr(a) { };
virtual const int & operator*() const
{
return *ptr;
}
virtual const char & vtest() { }
protected:
int * ptr;
};

class Ptr: public CPtr {
public:
Ptr(int * a):CPtr(a) { };
virtual int & operator*() const
{
return *ptr;
}
virtual char & ttest() { }
};

void fun(CPtr & p)
{
// I was hoping this would work since a Ptr object is passed and
// dynamically it would bind to virtual int & operator*() const
// But it binds to virtual const int & operator*() const at compile
time
// Why?

// cout << ++(*p) << endl;
}

int main()
{
int i = 10;
Ptr pt(&i);
cout << ++(*pt) << endl;
fun(pt);

return 0;
}
Jul 22 '05 #1
3 2164

"CoolPint" <co******@yahoo.co.uk> wrote in message
news:15*************************@posting.google.co m...
I read that the return type has to be exactly same for a virtual
function to be overriden. While testing something, I discovered
something I cannot understand.
I cannot understand why the code below compiles.

I see that it won't compile if I have "virtual char & vtest() { }" in
Base class
and have "virtual const char & vtest() { }" in Derived class.

Because if it did you could violate const correctness.
But it compiles if I have "virtual const char & vtest() { }" in Base
class and have "virtual char & vtest() { }" in derived class.
Why? What's going on?
Because with this you can't:
A CPtr will return you a cont reference whether or not it is actually a Ptr.

Similarly, operator*() functions compiles as written, but always
returns const int & even I assign derived object to Base reference.
Please have a look at comments on function fun() to see what I mean.

Can anyone kindly explain why it compiles yet won't do dynamic
binding?
Thanks always (especially to you, Victor B.)

#include <iostream>
using std::cout;
using std::endl;

class CPtr {
public:
CPtr(int * a):ptr(a) { };
This is the start of your misconceptions:
If CPtr is a pointer to a const int then why doesn't it take one ine the
constructor?
Your CPtr cannot actually point to a constant integer!
If you change to CPtr(const int*) the problems that you will then have with
Ptr(int*) should show you
why your design is flawed and why C++ works the way it does.
virtual const int & operator*() const
{
return *ptr;
}
virtual const char & vtest() { }
returns what?
protected:
int * ptr;
};

class Ptr: public CPtr {
inheritance is not appropriate here.
You are saying "Every Ptr IS-A CPtr"
or "every pointer is a pointer to constant except that you can change it!"
This is obviously nonsensical.
public:
Ptr(int * a):CPtr(a) { };
If you had CPtr(const int*) you would not now be able to initialize it with
a.
virtual int & operator*() const
{
return *ptr;
}
virtual char & ttest() { }
};

void fun(CPtr & p)
{
// I was hoping this would work since a Ptr object is passed and
// dynamically it would bind to virtual int & operator*() const
// But it binds to virtual const int & operator*() const at compile
time
No it doesn't - it binds to virtual int& operator*() (look at the asembler)
BUT the the return type is treated as const int& because that is what it is
in CPtr.

C++ uses dynamic function calls but static type checking.
Or to put it another way virtual function calls select functions at runtime
NOT types.
// Why?

// cout << ++(*p) << endl;
}

int main()
{
int i = 10;
Ptr pt(&i);
cout << ++(*pt) << endl;
fun(pt);

return 0;
}

Jul 22 '05 #2
> No it doesn't - it binds to virtual int& operator*() (look at the asembler)
BUT the the return type is treated as const int& because that is what it is
in CPtr.

C++ uses dynamic function calls but static type checking.
Or to put it another way virtual function calls select functions at runtime
NOT types.


Does that mean return types of virtual functions do not take part in
the resolving of dynamic binding?

Thank you for your comments. The coding was made just to illustrate
the points I am trying to ask in simple way without considering the
design. What I really want to find out is the feature of the langauge
and how it works and you kind of answered above there but it's not
clear to me yet. Could you kindly elaborate a bit more?

Books say the return type has to be exactly same when a virtual
function is overriden but why doesn't compiler complain in the
following case:

Base member's return type : virtual const T &
Derived member's return type : virtual T &

They look different types to me but compiler doesn't complain. But
compiler complains when the situation is reversed though.

Does this mean I have successfully overridden the function for dynamic
binding to work? If then, why calling the member function through base
reference would return the base class member's return type?

Thank you for your patience.
Jul 22 '05 #3

"CoolPint" <co******@yahoo.co.uk> wrote in message news:15**************************@posting.google.c om...
Does that mean return types of virtual functions do not take part in
the resolving of dynamic binding?
Correct. The important issue is name and parameter list. However,
the return types of overriding functions are required to be same or one
of the legal related types.
Books say the return type has to be exactly same when a virtual
function is overriden but why doesn't compiler complain in the
following case:


Those books are wrong. The rule is relaxed in the case when:
1. Both returns are pointers or references to classes
2. The type of the base class function return is an unambiguous accessible base class
of the return of the derived class function
3. The CV-qualification is the same for the pointers, and the same or less qualfifed for
the pointed/referred to derived object

Jul 22 '05 #4

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

Similar topics

5
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
11
by: Achintya | last post by:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& #include<iostream> using namespace std; class A { int i; virtual void f(){ cout<<endl<<"i= "<<i; } public: A(int j) { i =...
1
by: paulsmith5 | last post by:
Hi, I've never developed in CSharp before and I'm trying to adapt an existing user control written in CSharp. The user control inherits from the DataGridColumn. I'd like for one of my controls...
3
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo {...
13
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a...
3
by: Rick | last post by:
One of the rules I have recommended for our new coding standard is as follows: "When overriding a base class virtual function, all overloads of that base class virtual function should also be...
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.