473,473 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Virtual Function Problems

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
Jul 19 '05 #1
5 2905
"Ryan Faulkner" <ya******@hotmail.com> wrote...
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
Jul 19 '05 #2

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vi************@corp.supernews.com...
"Ryan Faulkner" <ya******@hotmail.com> wrote...
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
Jul 19 '05 #3
"Ryan Faulkner" <ya******@hotmail.com> wrote...

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vi************@corp.supernews.com...
"Ryan Faulkner" <ya******@hotmail.com> wrote...
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
Jul 19 '05 #4

"Ryan Faulkner" <ya******@hotmail.com> wrote in message
news:bg**********@news1.mountaincable.net...

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vi************@corp.supernews.com...
"Ryan Faulkner" <ya******@hotmail.com> wrote...
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
Jul 19 '05 #5

"Ryan Faulkner" <ya******@hotmail.com> wrote in message
news:bg**********@news1.mountaincable.net...
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
Jul 19 '05 #6

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

Similar topics

3
by: CoolPint | last post by:
I read that the return type has to be exactly same for a virtual function to be overriden. While testing something, I discovered something I cannot understand. I cannot understand why the code...
62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
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: 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
13
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
11
by: Chris Thomasson | last post by:
Consider an an object that that can has 7 or 8 functions. If you create an abstract base class for the "interface" of the object, well, that means 7 or 8 pure virtual functions right? Well, IMHO,...
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;"...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.