473,416 Members | 1,774 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,416 software developers and data experts.

Virtual function problem

I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class (ClassB* b; b->func(); ) the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten in the the derived class) work fine, it's just this one
function. What's the problem?
Jul 19 '05 #1
11 3723
Kostatus wrote:
I have a virtual function in a base class, which is then overwritten
You cannot portably overwrite a function. There is no guarantee that
function code exists in modifiable memory.
by a
function of the same name in a publically derived class.
Oh, you mean *overridden*. Well, that's completely different. Of course,
the function must meet requirements beyond having the same name. It must
also have the same arguments and the same return type (except for the
special case of covariant return types).
When I call the
function using a pointer to the derived class (ClassB* b; b->func(); )
If you do it that way you invoke undefined behavior and will probably
crash your program. You may not dereference a pointer that has not been
given a value.
the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten
overridden
in the the derived class) work fine, it's just this one
function. What's the problem?


The logical conclusion is that your code has a bug in it. The most
likely place for this bug to occur is line 42, though you should also
check line 37 just in case.

In other words, we have no idea without seeing the code.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2

"Kevin Goodsell" wrote in message
I have a virtual function in a base class, which is then overwritten
You cannot portably overwrite a function. There is no guarantee that
function code exists in modifiable memory.
by a
function of the same name in a publically derived class.


Oh, you mean *overridden*. Well, that's completely different. Of course,
the function must meet requirements beyond having the same name. It must
also have the same arguments and the same return type (except for the
special case of covariant return types).


I checked these, they're all identical.
When I call the
function using a pointer to the derived class (ClassB* b; b->func(); )


If you do it that way you invoke undefined behavior and will probably
crash your program. You may not dereference a pointer that has not been
given a value.


It IS given a value, there's no doubt about that. It does not crash, but
calls the wrong function.

ClassA
{
public:
virtual void func1();
virtual void func2();
};

ClassB
{
public:
void func1();
void func2();
};

void some_other_func()
{
ClassB* b;
b = new ClassB(some_arguments);
b->func1(); // calls ClassB::func1()
b->func2(); // for some strange reason calls ClassA::func2()
}
the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten


overridden


eh.. whatever...
in the the derived class) work fine, it's just this one
function. What's the problem?

In other words, we have no idea without seeing the code.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
Kostatus wrote:


I checked these, they're all identical.
I'm afraid I can't take your word for this.

It IS given a value, there's no doubt about that. It does not crash, but
calls the wrong function.
If it isn't given a correct value, crashing isn't a requirement. In
fact, calling the wrong function is a perfectly acceptable consequence.

ClassA
{
public:
virtual void func1();
virtual void func2();
};

ClassB
{
public:
void func1();
void func2();
};

void some_other_func()
{
ClassB* b;
b = new ClassB(some_arguments);
b->func1(); // calls ClassB::func1()
b->func2(); // for some strange reason calls ClassA::func2()


I'm sorry, I don't believe you. Mostly because there is no relationship
between ClassA and ClassB.

As I suggested before, post the code. The real code. A minimum,
compilable example that demonstrates the problem.

Here's the link again, you apparently didn't bother to read it last time:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

"Kevin Goodsell" wrote in message

Fixed the problem. All it needed was a re-build, I think - I can't remember
changing anything; anyway, that was strange.

I checked these, they're all identical.
I'm afraid I can't take your word for this.


Copied and pasted, also I have two eyes which seems to work perfectly.

It IS given a value, there's no doubt about that. It does not crash, but calls the wrong function.


If it isn't given a correct value, crashing isn't a requirement. In
fact, calling the wrong function is a perfectly acceptable consequence.


It's a correct value, I know for certain. Why would I lie? I'm the one
seeking help here, whats the point in lying about the problem!
ClassA
{
public:
virtual void func1();
virtual void func2();
};

ClassB
{
public:
void func1();
void func2();
};

void some_other_func()
{
ClassB* b;
b = new ClassB(some_arguments);
b->func1(); // calls ClassB::func1()
b->func2(); // for some strange reason calls ClassA::func2()


I'm sorry, I don't believe you. Mostly because there is no relationship
between ClassA and ClassB.


I forgot to put in the ": public ClassA", sorry. My real code has it, dont
worry.
As I suggested before, post the code. The real code. A minimum,
compilable example that demonstrates the problem.

Here's the link again, you apparently didn't bother to read it last time:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


Read it, so sick of people in this newsgroup trying to teach me how to post.
I didn't give you my real code because it would take me too long to get it
to a stage at which it would be compilable without the rest of the
multi-thousand line program. I thought my explanation was clear enough, I
still don't see why you wouldn't believe me - I was the one seeking help.
If you didn't know what was the problem in my case, you could just keep
quiet and let someone else help instead of saying that you can't take my
word for what I have in MY code.
Jul 19 '05 #5
Kostatus wrote:

Copied and pasted, also I have two eyes which seems to work perfectly.

I didn't see any code in your message that could have been the code you
were talking about.

It's a correct value, I know for certain. Why would I lie?
I don't know. Why are you lying about copying & pasting the code? It
could be a compulsive thing.
I'm the one
seeking help here, whats the point in lying about the problem!
People make mistakes. That's why we ask to see code.

I forgot to put in the ": public ClassA", sorry. My real code has it, dont
worry.
So you didn't copy & paste after all?

As I suggested before, post the code. The real code. A minimum,
compilable example that demonstrates the problem.

Here's the link again, you apparently didn't bother to read it last time:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Read it, so sick of people in this newsgroup trying to teach me how to post.


We're sick of teaching people how to post. It would save everyone a lot
of time if people would just learn before posting, don't you think?
I didn't give you my real code because it would take me too long
And yet we should take the time to guess at what the problem is and
explain it to you? You are the one asking for help, you should be the
one taking the time to make our job easier. (OK, 'job' is a poor term to
use there, considering we do this on a purely volunteer basis, out of
the kindness of our hearts...)
to get it
to a stage at which it would be compilable without the rest of the
multi-thousand line program. I thought my explanation was clear enough, I
still don't see why you wouldn't believe me
Because nearly every day someone posts a message saying "My code doesn't
work. I did everything right. What's the problem?" And when we finally
see the code, there's always errors. People make mistakes, and we cannot
diagnose a problem properly without the code anyway. All we can do is
guess, which is a waste of everyone's time.
- I was the one seeking help.
If you didn't know what was the problem in my case, you could just keep
quiet and let someone else help instead of saying that you can't take my
word for what I have in MY code.


Nobody knew what the problem was, nor could they have without seeing the
code. Your argument is downright foolish. If the program doesn't work,
there's a 99.9% chance there's a problem with the code, so saying "My
code is fine" is just plain ignorant. In your case, maybe you had a
compiler problem. In those rare cases, we still need to see the code.
That way we can say "code looks fine, works for me, looks like maybe
you're having a problem with your compiler."

If you want help, give us the information we need to help you. If this
sounds unreasonable to you, then please let me know so I can add you to
my killfile now and avoid wasting any more of my time. But you will have
a hard time getting help with that attitude.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6

"Kevin Goodsell" wrote in message
Copied and pasted, also I have two eyes which seems to work perfectly.

I didn't see any code in your message that could have been the code you
were talking about.


I think, assuming that you are using a head, it is easy enough to realise
that I copied and pasted the function declarations, not the code which I
sent to the group.

It's a correct value, I know for certain. Why would I lie?


I don't know. Why are you lying about copying & pasting the code? It
could be a compulsive thing.


Lying about a problem that I'm having to get help with it. So I like making
up problems and having others solve them for me? Interesting...
I'm the one
seeking help here, whats the point in lying about the problem!


People make mistakes. That's why we ask to see code.


Yes, I made a mistake of posting a porblem that I had to a
supposedly-helpful newsgroup which seems to be the only one apropriate to my
problem. Recently I've been using newsgroups to help me with a project that
I'm working on - to give me critical feedback, to help me solve problems
that I couldn't solve by myself. ALL but this newsgroup have been friendly
and helpful to me.

Yes, I have recieved some good replies in the past form comp.lang.c++, but
half of the time I was told to go away when my problem even slightly
overlapped into a different area (for example resource files). I'm sure the
people here could help me, but they told me to go away instead.

And then there is the current example, where I have been called a liar for
merely asking for some help!

I forgot to put in the ": public ClassA", sorry. My real code has it, dont worry.


So you didn't copy & paste after all?


Already explained this.

As I suggested before, post the code. The real code. A minimum,
compilable example that demonstrates the problem.

Here's the link again, you apparently didn't bother to read it last time:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Read it, so sick of people in this newsgroup trying to teach me how to post.
We're sick of teaching people how to post. It would save everyone a lot
of time if people would just learn before posting, don't you think?
My post was on-topic, and posting code was not a sane option, for reasons
that I have already explained. My description of the problem was clear
enough.
I didn't give you my real code because it would take me too long


And yet we should take the time to guess at what the problem is and
explain it to you? You are the one asking for help, you should be the
one taking the time to make our job easier. (OK, 'job' is a poor term to
use there, considering we do this on a purely volunteer basis, out of
the kindness of our hearts...)


Kindness of your hearts, please show some kindness to those asking for help!
Where was the kindness from you when you told me to go read the posting
guidelines for this newsgroup or called me a liar!
to get it
to a stage at which it would be compilable without the rest of the
multi-thousand line program. I thought my explanation was clear enough, I still don't see why you wouldn't believe me


Because nearly every day someone posts a message saying "My code doesn't
work. I did everything right. What's the problem?" And when we finally
see the code, there's always errors. People make mistakes, and we cannot
diagnose a problem properly without the code anyway. All we can do is
guess, which is a waste of everyone's time.


I explained my problem as clearly as possible, I even gave you an example
when you said that it was not clear enough. If you did not know anything
about the problem, why respond?
- I was the one seeking help.
If you didn't know what was the problem in my case, you could just keep
quiet and let someone else help instead of saying that you can't take my
word for what I have in MY code.


Nobody knew what the problem was, nor could they have without seeing the
code. Your argument is downright foolish. If the program doesn't work,
there's a 99.9% chance there's a problem with the code, so saying "My
code is fine" is just plain ignorant. In your case, maybe you had a
compiler problem. In those rare cases, we still need to see the code.
That way we can say "code looks fine, works for me, looks like maybe
you're having a problem with your compiler."


If I tell you that I copied and pasted the definition, it means that I
copied and pasted the definition. I gave you a simplified example to show
you what my code looked like. I would have given you the code but, as I
already said, it would be too hard and take too long just to get it working
by itself.
If you want help, give us the information we need to help you. If this
sounds unreasonable to you, then please let me know so I can add you to
my killfile now and avoid wasting any more of my time. But you will have
a hard time getting help with that attitude.


You sound very unreasonable to me, but dont worry about adding me to you
killfile - you're already the latest addition to mine, so you will never
have to waste your perfect manners on me.
Jul 19 '05 #7
Kostatus wrote:
I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class (ClassB* b; b->func(); ) the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten in the the derived class) work fine, it's just this one
function. What's the problem?


A picture is worth a thousand words. Please post a short, complete,
compliable code sample that exactly demonstrates the problem you're
describing. Without some code to look at, it's anyone's guess as to what
the actual problems are in your code.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #8
Jim Fischer wrote:
Kostatus wrote:
I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class (ClassB* b; b->func();
) the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten in the the derived class) work fine, it's just this one
function. What's the problem?

A picture is worth a thousand words. Please post a short, complete,
compliable code sample that exactly demonstrates the problem you're
describing. Without some code to look at, it's anyone's guess as to what
the actual problems are in your code.


Feel free to disregard this post. As soon as I clicked the "post" button
the other messages in this thread suddenly appeared on my newsreader
program...

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #9
Kostatus wrote:

I think, assuming that you are using a head, it is easy enough to realise
that I copied and pasted the function declarations, not the code which I
sent to the group.
I don't make such assumptions. They are too often wrong.

Lying about a problem that I'm having to get help with it. So I like making
up problems and having others solve them for me? Interesting...
I wasn't serious about that.

Yes, I made a mistake of posting a porblem that I had to a
supposedly-helpful newsgroup which seems to be the only one apropriate to my
problem. Recently I've been using newsgroups to help me with a project that
I'm working on - to give me critical feedback, to help me solve problems
that I couldn't solve by myself. ALL but this newsgroup have been friendly
and helpful to me.
Well, for one thing I don't represent the entire group. For another, I
would have been much more friendly if you had been more reasonable. The
reasons we ask for code (the real, actual code that demonstrates the
problem) should be quite clear. Things would have gone much more
smoothly had you simply posted real code.

Yes, I have recieved some good replies in the past form comp.lang.c++, but
half of the time I was told to go away when my problem even slightly
overlapped into a different area (for example resource files).
Resource files are a good example of something that is completely and
utterly off-topic here.
I'm sure the
people here could help me, but they told me to go away instead.
A person working at a garage might be able to make a hamburger, too. But
that doesn't justify ordering a Whopper at Jiffy-Lube (sorry if the
American references are wasted on you - don't know if they would be
familiar to a New Zealander).

And then there is the current example, where I have been called a liar for
merely asking for some help!
I didn't call you a liar. I just said I needed to see the code. I think
the reasons for this are clear.

Already explained this.

Fair enough.

My post was on-topic, and posting code was not a sane option, for reasons
that I have already explained. My description of the problem was clear
enough.
You "reason" was that it would have taken you too long. In other words,
you weren't unable, but unwilling to take the time to do it. That's a
poor attitude to use when requesting help.

Kindness of your hearts, please show some kindness to those asking for help!
I do, frequently. I tried to help you, but you refused to show me the
problem.
Where was the kindness from you when you told me to go read the posting
guidelines for this newsgroup or called me a liar!
Pointing you to the posting guidelines was very kind of me. Those
guidelines will help you dramatically when you are asking for help here,
if you will follow them.

I explained my problem as clearly as possible, I even gave you an example
when you said that it was not clear enough. If you did not know anything
about the problem, why respond?
To find out more about the problem, for one thing.

If I tell you that I copied and pasted the definition, it means that I
copied and pasted the definition. I gave you a simplified example to show
you what my code looked like. I would have given you the code but, as I
already said, it would be too hard and take too long just to get it working
by itself.
You *didn't* show us what the code looked like, though.

You sound very unreasonable to me, but dont worry about adding me to you
killfile - you're already the latest addition to mine, so you will never
have to waste your perfect manners on me.


Well, I was willing to help. You were unwilling to let me. Nobody could
have possibly helped given only what you posted. It's like telling the
doctor you have a sore throat then refusing to open up and say "Ahhh"
for him.

I don't know why you think I've been unreasonable - clearly the group
agrees with what I said (that we need to see code) otherwise it wouldn't
be in the FAQ.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10
"Kostatus" <ko******@ihug.co.nz> wrote in message
news:bj**********@lust.ihug.co.nz

[much complaining snipped]

Your initial post said:

I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class (ClassB* b; b->func(); ) the
base-class function is called instead of the new function in the derived
class. All other similar functions (virtual in the base class and
overwritten in the the derived class) work fine, it's just this one
function. What's the problem?
How did you expect people here to answer that? The most likely problem by
far was some coding error, but it was impossible to know what coding error
without seeing the code. As it turned out, a recompilation made the problem
go away. In the absence of any information, this was an unlikely solution.
The only way to arrive at that solution would have been to see the code and
verify that it was fine.

You then posted the following code:

ClassA
{
public:
virtual void func1();
virtual void func2();
};

ClassB
{
public:
void func1();
void func2();
};

void some_other_func()
{
ClassB* b;
b = new ClassB(some_arguments);
b->func1(); // calls ClassB::func1()
b->func2(); // for some strange reason calls ClassA::func2()
}
ClassB does not inherit from ClassA in this code, so your code and the
results that you report from running it are unintelligible. Again, what
magical powers do you think that the people in this group possess to enable
them to diagnose what is really happening? You defend yourself with:
I explained my problem as clearly as possible, I even gave you an
example when you said that it was not clear enough. If you did not
know anything about the problem, why respond?

This is just drivel. You didn't explain the problem nor supply code in a way
that was of any value whatsoever for the purposes of actually diagnosing the
problem. The fact that you posted here at all means that, even *with* access
to the source code, you couldn't figure out what was wrong. Yet somehow you
think it is reasonable to expect people *without* access to the code to
figure out what was wrong. You simply have unrealistic expectations about
the ability of people to help when they don't have relevant information.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #11
"Kevin Goodsell" wrote in message

[....]
Well, for one thing I don't represent the entire group. For another, I
would have been much more friendly if you had been more reasonable. The
reasons we ask for code (the real, actual code that demonstrates the
problem) should be quite clear. Things would have gone much more
smoothly had you simply posted real code.

[....]
You "reason" was that it would have taken you too long. In other words,
you weren't unable, but unwilling to take the time to do it. That's a
poor attitude to use when requesting help.
I WOULD post some real code, but it seemed unncessesary right in the
beginning to spend my time on it, and then I have already solved the problem
when I read the post where you told me that the made-up example that I've
given you was not helpful.

[....]
Well, I was willing to help. You were unwilling to let me. Nobody could
have possibly helped given only what you posted. It's like telling the
doctor you have a sore throat then refusing to open up and say "Ahhh"
for him.

I don't know why you think I've been unreasonable - clearly the group
agrees with what I said (that we need to see code) otherwise it wouldn't
be in the FAQ.


Sorry about my stubbornness today, I'm having one of those days when
everything goes wrong today. But it was mostly caused by you saying that
you can't take my word for what's in my code. I know that you were just
trying to help, sorry about my comments.
Kostatus
Jul 19 '05 #12

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

Similar topics

18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
5
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
8
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;
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
17
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;"...
3
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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
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...

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.