Virtual function

D

dragoncoder

What are your feeling about the code ?

#include <iostream>
#include <ostream>

using namespace std;

class A {
public:
A() { f(); }
virtual void f() { g(); }
void g() { cout << "A" << endl; }
};

class B: public A {
public:
B() { f(); }
virtual void f() { g(); }
void g() { cout << "B" << endl; }
};

int main() {
A a;
B b;
return 0;
}

Thanks
 
P

Phlip

dragoncoder said:
What are your feeling about the code ?

I feel your nebulous question, and the trick nature of the code, implies
it's for an exam or something. One more suspicious than me might accuse you
of replacing the real question with something less likely to trigger our
anti-homework response.

What are _your_ feelings about the code?
 
A

Alan Johnson

dragoncoder said:
What are your feeling about the code ?

#include <iostream>
#include <ostream>

using namespace std;

class A {
public:
A() { f(); }

The function call in this constructor may not do what you expect. But
then, that is probably the point of this post.
virtual void f() { g(); }
void g() { cout << "A" << endl; }
};

class B: public A {
public:
B() { f(); }
virtual void f() { g(); }
void g() { cout << "B" << endl; }
};

int main() {
A a;
B b;
return 0;
}

Thanks

During class A's constructor, whatever object is being constructed is of
type A (even if it later winds up being type B). Therefore you can't
expect virtual dispatching to work.
 
A

Alf P. Steinbach

* dragoncoder:
What are your feeling about the code ?

#include <iostream>
#include <ostream>

using namespace std;

class A {
public:
A() { f(); }
virtual void f() { g(); }
void g() { cout << "A" << endl; }
};

class B: public A {
public:
B() { f(); }
virtual void f() { g(); }
void g() { cout << "B" << endl; }
};

int main() {
A a;
B b;
return 0;
}

I don't know how to express this feeling in English, but I once was
cajoled into a role as a "C++ expert" at a seminar, when I didn't know
much C++ and the only compiler available to me was Turbo C++ 1.0 or
whatever the version was. I did know a good deal about how to interface
C++ and assembly code, but broke into a sweat (literally!) when one of
the participants asked me for help with simple textual /line input/,
which I'd never needed and so knew nothing about. Worse, an example I'd
prepared relied on the misconception that a C++ constructor could call a
derived class' virtual function virtually (because Turbo C++ did that),
and even though this was long before C++ was standardized the ARM said
differently, and one participant pointed this out. In spite of this
most of them were happy. And I got a silver knife as thank-you.

Now you know perhaps how I feel about that code.

But perhaps this was instead about the program's behavior, that it
somehow was unexpected. In that case I suggest visiting the FAQ. E.g.
the item "When my base class's constructor calls a virtual function on
its this object, why doesn't my derived class's override of that virtual
function get invoked", currently at <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5>.
 
D

dragoncoder

Phlip said:
I feel your nebulous question, and the trick nature of the code, implies
it's for an exam or something. One more suspicious than me might accuse you
of replacing the real question with something less likely to trigger our
anti-homework response.

What are _your_ feelings about the code?

Well, its not a homework, you may ofcourse call it one if you like.
Someone asked me this question just for fun and based on my experience
I told him the program is not standard compliant.

I see here the code is trying to call a virtual function for class B
while its constructor is still executing, which I feel is wrong and
makes the program ill-formed. I was not sure though whether its an
undefined behaviour or something else thats why I did not post my
feelings with the code but I wanted to know what the language has to
say about this.

The reason for asking your _feeling_ about the code was just because I
did not want to write the story behind it and waste people's time
reading, which makes no difference. If it is so rude, I apologise.
Could you please now share your views on this. Or at least just a line
saying what I feel is write or not?

Thanks
 
P

Phlip

dragoncoder said:
Well, its not a homework, you may ofcourse call it one if you like.
Someone asked me this question just for fun and based on my experience
I told him the program is not standard compliant.

Ah, that explains the peculiar but otherwise clean code with the open-ended
question. No problem!

Next time add a word or too about the code's origin. Yes, we also get "my
friend showed me this" as a cover for homework, yet all we require is a hint
towards what kind of answer will be best for you. ;-)
I see here the code is trying to call a virtual function for class B
while its constructor is still executing, which I feel is wrong and
makes the program ill-formed. I was not sure though whether its an
undefined behaviour or something else thats why I did not post my
feelings with the code but I wanted to know what the language has to
say about this.

It's just well-formed and well-defined as calling A::f().

To make it ill-defined, can we make A::f() pure virtual? And how might we
make A::f() pure virtual yet safe to accidentally call?

Another issue: B::g() hides A::g(). That made me think a teacher was laying
a false trail. Students unfamiliar with the rule about calling virtuals from
a constructor might stop at that problem and report it.
 
P

Phlip

Tomás said:
What you want hear is:

A::f();

That is advice on style, not language-law.

The advice is good when it makes the code resilient to refactors. If you
move f() out of A's constructor, you might still not want B::f(), so the A::
will remind you, and will preserve behavior if you forget.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top