Dear C++ programmers,
I have 2 classes
A and B
with virtual functions
virtual long A::Save();
virtual long B::Save(bool b = true);
Now, if I have a Pointer A * which points to a B Object:
B b;
A * p = & b;
p->Save();
Unfortunately A::Save() is called but I wanted B::Save() to be called.
In this case, the default parameter of B::Save() is senseless - but the
compiler gives no error.
???
Greetings
Ernst 10 8362
Ernst Murnleitner wrote: Dear C++ programmers,
I have 2 classes A and B with virtual functions virtual long A::Save(); virtual long B::Save(bool b = true);
Now, if I have a Pointer A * which points to a B Object:
B b; A * p = & b; p->Save();
Unfortunately A::Save() is called but I wanted B::Save() to be called. In this case, the default parameter of B::Save() is senseless - but the compiler gives no error.
This is exactly what the language specifies. For the "B::Save" method
to be called you need to have a "B * p;" i.e. a pointer to a B object.
However, you could theoretically do this:
class B
{
.....
virtual long Save() { Save( true ); }
virtual long Save(bool b);
};
Now you get what you expect. Here we have overloaded "Save()" and made
it do what you expect p->Save() to do.
G
"Ernst Murnleitner" <mu*@awite.de> wrote... Dear C++ programmers,
I have 2 classes A and B with virtual functions virtual long A::Save(); virtual long B::Save(bool b = true);
B::Save is not an overrider for A::Save. They have different
types. Default argument values are not part of the function
type. Now, if I have a Pointer A * which points to a B Object:
B b; A * p = & b; p->Save();
Unfortunately A::Save() is called but I wanted B::Save() to be called.
You have to make B::Save to be the overrider of A::Save. In
order to accomplish that you need to make them the same type.
Either give A::Save the argument or remove the argument from
B::Save.
In this case, the default parameter of B::Save() is senseless - but the compiler gives no error.
This should help you:
class A {
public:
virtual long Save();
};
class B {
long Save(bool b); // not virtual and private
public:
virtual long Save() { this->Save(true); }
};
int main() {
B b;
A *p = &b;
p->Save(); // will call B::Save(), which in turn will call
// B::Save(bool)
}
HTH
Victor
"Ernst Murnleitner" <mu*@awite.de> wrote in message news:be************@ID-130107.news.dfncis.de... Dear C++ programmers,
I have 2 classes A and B with virtual functions virtual long A::Save(); virtual long B::Save(bool b = true);
Now, if I have a Pointer A * which points to a B Object:
B b; A * p = & b; p->Save();
Unfortunately A::Save() is called but I wanted B::Save() to be called. In this case, the default parameter of B::Save() is senseless - but the compiler gives no error.
B::Save(bool) doesn't override A::Save. There's no way that this is going
to happen. You must match the parameter list if you want things to override.
Further, a function in the derived class hides the base class definitions so
B::Save(bool b = true) is possibly not senseless.
The compiler issues no error because there is no error.
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m... "Ernst Murnleitner" <mu*@awite.de> wrote in message
news:be************@ID-130107.news.dfncis.de... Dear C++ programmers,
I have 2 classes A and B with virtual functions virtual long A::Save(); virtual long B::Save(bool b = true);
Now, if I have a Pointer A * which points to a B Object:
B b; A * p = & b; p->Save();
Unfortunately A::Save() is called but I wanted B::Save() to be called. In this case, the default parameter of B::Save() is senseless - but the compiler gives no error.
B::Save(bool) doesn't override A::Save. There's no way that this is
going to happen. You must match the parameter list if you want things to
override. Further, a function in the derived class hides the base class definitions
so B::Save(bool b = true) is possibly not senseless.
The compiler issues no error because there is no error.
Please correct me in my understanding of hiding and overriding of member
functions.
A function gets overridden when a derived class defines a function with the
same name, return type and
parameters as a base class function.
A function gets hidden when a derived class defines a function with the same
name but different return type or
parameters.
Now my question is that in the OP's code snippet doesn't B::Save hide
A::Save ?
If that is the case then why does the compiler not issue any error message?
-SK
SK wrote: Now my question is that in the OP's code snippet doesn't B::Save hide A::Save ? If that is the case then why does the compiler not issue any error message?
Because p is a pointer to A. And A has a Save() method.
For
A* p = &b;
p->Save();
it is completely unimportant that there exists a B class. The B class
is never investigated, if it contains a member function Save().
--
Karl Heinz Buchegger kb******@gascad.at
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
SK wrote: Now my question is that in the OP's code snippet doesn't B::Save hide A::Save ? If that is the case then why does the compiler not issue any error
message?
Because p is a pointer to A. And A has a Save() method.
For A* p = &b; p->Save();
it is completely unimportant that there exists a B class. The B class is never investigated, if it contains a member function Save().
Please correct me again if I am wrong.
Do you mean to say that p is statically a pointer to A(even if it's
pointing to B) and B has no matching definition for Save() so the function
name becomes
visible.
Also
B b;
b.Save () //error
Thanks again.
-SK
SK wrote: Because p is a pointer to A. And A has a Save() method.
For A* p = &b; p->Save();
it is completely unimportant that there exists a B class. The B class is never investigated, if it contains a member function Save(). Please correct me again if I am wrong. Do you mean to say that p is statically a pointer to A(even if it's pointing to B)
When the compiler tries to compile
p->Save();
it looks up the data type of p. And that's A*
Thus class A is searched for a member function Save().
The fact that during *runtime*, p in reality points to a B object
is unimportant for the compiler. It cannot know this in general.
and B has no matching definition for Save() so the function name becomes visible.
No. B is never looked up to figure out which function to call.
In
p->Save();
there is no logic path that would lead to a class B. In that
statement there is only class A (which is the data type of *p)
mentioned.
Also B b; b.Save () //error
This is different. Here the compiler knows that b is of type B.
Thus the function lookup starts in the B class.
--
Karl Heinz Buchegger kb******@gascad.at
SK wrote:
No. B is never looked up to figure out which function to call. In p->Save(); there is no logic path that would lead to a class B. In that statement there is only class A (which is the data type of *p) mentioned.
I agree with you completely that at compile time p's type is determined as A*. But since Save () is a virtual function the function call gets resolved to VtablePtr->Save, the actual function call is then evaluated at run time. I hope I am right so far :-).
Yes.
At run time(in the OP) B object's Vtable is being referred to at run time. A* p = &b; p->Save () (I am not very sure after this :-( ) In this Vtable the function pointer being referred to is A's Save method.
Yes. If the signature of the method in B would have been the same as the
method in A, the function pointer would have referred to B's Save method
instead.
--
"Codito ergo sum"
Roel Schroeven
"Roel Schroeven" <j4*******@sneakemail.com> wrote in message
news:oq******************@afrodite.telenet-ops.be... SK wrote:
No. B is never looked up to figure out which function to call. In p->Save(); there is no logic path that would lead to a class B. In that statement there is only class A (which is the data type of *p) mentioned.
I agree with you completely that at compile time p's type is determined
as A*. But since Save () is a virtual function the function call gets resolved
to VtablePtr->Save, the actual function call is then evaluated at run
time. I hope I am right so far :-).
Yes.
At run time(in the OP) B object's Vtable is being referred to at run
time. A* p = &b; p->Save () (I am not very sure after this :-( ) In this Vtable the function pointer being referred to is A's Save
method. Yes. If the signature of the method in B would have been the same as the method in A, the function pointer would have referred to B's Save method instead.
Thanks Karl and Roel for your inputs.
I know VTables are implementation specific.
Still if you could suggest any useful links/resources on VTables, that would
be great.
Thanks,
-SK
SK wrote: No. B is never looked up to figure out which function to call. In p->Save(); there is no logic path that would lead to a class B. In that statement there is only class A (which is the data type of *p) mentioned.
I agree with you completely that at compile time p's type is determined as A*. But since Save () is a virtual function the function call gets resolved to VtablePtr->Save, the actual function call is then evaluated at run time. I hope I am right so far :-).
Right. But we were discussing why this is not a compile-time error.
Thus we don't have to deal with runtime issues. At run time(in the OP) B object's Vtable is being referred to at run time. A* p = &b; p->Save () (I am not very sure after this :-( ) In this Vtable the function pointer being referred to is A's Save method.
Again: Right. P.S : I hope I am not asking too many and too silly questions :-).
There are no silly questions. :-)
--
Karl Heinz Buchegger kb******@gascad.at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: zipher |
last post by:
After searching through comp.lang.python and the web regarding
metaclasses, I could not find an example for customing classes using
metaclass parameters.
I want to be able to create a class at...
|
by: Sonia |
last post by:
Hi, Please look at the following code. This is a simple program for a game
of craps - very basic.
I have declared the gameStatus as global variable. I was wondering if there
is a better way of...
|
by: jeffc |
last post by:
When compiling the following program, I get an error on the line specified:
The base class "B" cannot be initialized because it does not have a default
constructor.
If I remove the "virtual"...
|
by: cody |
last post by:
Why doesn't C# allow default parameters for methods?
An argument against I hear often is that the default parameters would have
to be hardbaken into the assembly, but why? The Jit can take care of...
|
by: Francisco Amaro |
last post by:
Hi all,
Have question about inheriting a class that has parameters in the
constructor such as :
Public MustInherit Class MyParentClass
Public mystring As String
Public Sub New(ByVal...
|
by: ingoweiss |
last post by:
Hi,
I am having trouble passing parameters of a Javascript subclass
constructor through to it's superclass constructor.
I am trying all sorts of things, including the below, but nothing...
|
by: cody |
last post by:
I got a similar idea a couple of months ago, but now this one will require
no change to the clr, is relatively easy to implement and would be a great
addition to C# 3.0 :)
so here we go..
To...
|
by: ali |
last post by:
Hi,
I was reading on inheritance and constructors on a text, and the
author was saying that its a good practice to include a default
constructor even if we have overloaded constructors. I don't...
|
by: jamesclose |
last post by:
My problem is this (apologies if this is a little long ... hang in there):
I can define a function in VB.NET with optional parameters that wraps a SQL procedure:
Sub Test(Optional ByVal Arg1...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |