Virtual function problem

K

Kostatus

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?
 
K

Kevin Goodsell

Kostatus said:
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-lite/how-to-post.html#faq-5.8

-Kevin
 
K

Kostatus

in message
You cannot portably overwrite a function. There is no guarantee that
function code exists in modifiable memory.


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.
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()
}
overridden

eh.. whatever...
 
K

Kevin Goodsell

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-lite/how-to-post.html#faq-5.8

-Kevin
 
K

Kostatus

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'm afraid I can't take your word for this.

Copied and pasted, also I have two eyes which seems to work perfectly.
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!
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-lite/how-to-post.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.
 
K

Kevin Goodsell

Kostatus said:
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?
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
 
K

Kostatus

"Kevin Goodsell" wrote in message
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.
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...
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!
So you didn't copy & paste after all?

Already explained this.
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.
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!
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?
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.
 
J

Jim Fischer

Kostatus 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?

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.
 
J

Jim Fischer

Jim said:
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...
 
K

Kevin Goodsell

Kostatus said:
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
 
J

John Carson

[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.
 
K

Kostatus

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
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top