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