Virtual Function Problems

R

Ryan Faulkner

Hi,

Im having a few problems with virtual functions (Im using the Visual C++
environment by the way). I have a base class with three virtual functions
and a derived class with a single new virtual function plus redefinitions of
the three inherited virtual functions.

Following is a simplified code fragment to illustrate my code and my
problem:

class cBase {

public:
virtual void f1() {code}
virtual void f2() {code}
virtual void f3() {code}
};

class cDerived : public cBase {

public:

void f1() {code}
void f2() {code}
void f3() {code}
virtual void f4() {code}

void func(); // some class member function
};

void cDerived::func() {

// If func() is called by a cDerived object then the cDerived definitions if
f1,f2 and f3
// should be invoked
f1();
f2();
f3();

// The screwed up thing here is that I have only one definition of f4() it's
simply not
// being called
f4(); // Does nothing when invoked with cDerived object -- must use
cDerived::f4();

}

If I say:

cDerived g_der;

g_der.func(); // cBase::f1(), cBase::f2(), cBase::f3() are invoked in
func() ... f4() is
// not invoked at all!

If I say:

cDerived *gp_der = new cDerived;

//Everything initialized properly

gp_der->func(); // compiles but cause program to crash because of f4()
gp_der->f4(); // compiles but causes crash .. Ive debugged my code to
determine
// these errors
//////////////////////////////

Now I should mention a few things. I am calling all the functions from a
cDerived object directly (Ex. cDerived g_der; g_der.func();), I am not
using a pointer to the object. Furthermore, when I changed my cDerived
object into a pointer and assigned it memory my program crashed when I
called: g_der->f4();. I am getting no errors, everything compiles properly
the functions are simply not being invoked or the program crashes as I just
mentioned. I even tried using the "this" pointer to invoke the virtual
functions within func() and nothing happened. I should mention that when I
call the virtual functions from my cDerived object directly the functions
are invoked properly. In other words,

cDerived g_der;

g_der.f1(); // calls cDerived::f1() .. good
g_der.f2(); // calls cDerived::f2() .. good
g_der.f3(); // calls cDerived::f3() .. good
g_der.f4(); // calls cDerived::f4() .. good


I am also referencing a book called the C++ Primer by Stanley Lippman and I
cant find any rules that I may be breaking, I read about vitual functions
thoroughly. I must be doing something wrong, if anyone can shed any light
on this I would appreciate it so much.

Ryan
 
V

Victor Bazarov

Ryan Faulkner said:
Im having a few problems with virtual functions [...]

Following is a simplified code fragment [...]

Post _real_ code. Simplify it as much as necessary to still
keep it compilable and producing the described error, then
post.

Victor
 
R

Ryan Faulkner

Victor Bazarov said:
Ryan Faulkner said:
Im having a few problems with virtual functions [...]

Following is a simplified code fragment [...]

Post _real_ code. Simplify it as much as necessary to still
keep it compilable and producing the described error, then
post.

Victor

The code is complete and should compile other than the fact that I put the
word "code" inside function definitions that are not relevant to the error.
Did you check out the code?

Ryan
 
V

Victor Bazarov

Ryan Faulkner said:
Victor Bazarov said:
Ryan Faulkner said:
Im having a few problems with virtual functions [...]

Following is a simplified code fragment [...]

Post _real_ code. Simplify it as much as necessary to still
keep it compilable and producing the described error, then
post.

Victor

The code is complete and should compile other than the fact that I put the
word "code" inside function definitions that are not relevant to the error.
Did you check out the code?

If I have to change ONE SYMBOL after copying and pasting your code
into the text editor before I run a compiler, your code is NOT
_complete_ or _compilable_.

Now, this is complete and compilable, and it works as expected
(and I say this because I tested it):
---------------------------------------------------------
class B {
public:
virtual void f1() {}
virtual void f2() {}
virtual void f3() {}
};
#include <iostream>
class D : public B {
public:
void f1() { std::cout << "D::f1\n"; }
void f2() { std::cout << "D::f2\n"; }
void f3() { std::cout << "D::f3\n"; }
virtual void f4() { std::cout << "D::f4\n"; }
void func();
};

void D::func() {
f1();
f2();
f3();
f4();
}

int main() {
D d;
d.func();
}
---------------------------------------------------------

Therefore one only can conclude that the crash you describe only
happens because of "code" you omitted. That's why I said, post
the complete code.

Victor
 
J

John Harrison

Ryan Faulkner said:
Victor Bazarov said:
Ryan Faulkner said:
Im having a few problems with virtual functions [...]

Following is a simplified code fragment [...]

Post _real_ code. Simplify it as much as necessary to still
keep it compilable and producing the described error, then
post.

Victor

The code is complete and should compile other than the fact that I put the
word "code" inside function definitions that are not relevant to the error.
Did you check out the code?

Ryan

Well there is no main function for a start. There are lots of statements
outside of any function, and the code appears to be interspersed with text.

People are lazy, there just like to cut and paste the code you've posted and
compile it.

Also posters make mistakes. In many, many posts containing code, the newbie
leaves out the vital code, because they thought it was irrelevant. Even
though they are a newbie, and the have a problem they can't solve, they
still think they know what is releveant and what is not.

Post complete, compilable code, its not that hard and you will get an answer
in minutes.

john
 
J

John Harrison

Ryan Faulkner said:
My apologies I didnt realize you actually wanted to compile it. Since I
wasnt getting errors compiling in the first place I was just trying to
communicate the concepts involved with my code (the code itself was very
extensive) and trying to find out if I had the wrong idea somewhere about
how virtual functions were used. My fault.

I don't think you have the wrong idea about virtual functions. You have a
bug in your code. That bug is somewhere in the code you haven't posted.

john
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top