473,468 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what virtual functions?!

When compiling this code with
g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4

######################################
#include <vector>

class b {
public:
virtual void f() = 0;
};

class d : public b {
std::vector<int> v;
public:
void f() {}
};
#####################################

I get a warning:

program.cpp:8: warning: `class d' has virtual functions but non-virtual
destructor

Am I very confused about the presence of virtual functions in class d, or is
the compiler?
Jul 23 '05 #1
5 4075
"alex goldman" <he***@spamm.er> wrote in message
news:19****************@yahoo.com...
When compiling this code with
g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4

######################################
#include <vector>

class b {
public:
virtual void f() = 0;
};

class d : public b {
std::vector<int> v;
public:
void f() {}
This line means the same thing as

virtual void f() {}

Once a function is virtual it stays virtual. Hence the keyword is optional.

};
#####################################

I get a warning:

program.cpp:8: warning: `class d' has virtual functions but non-virtual
destructor

Am I very confused about the presence of virtual functions in class d, or
is
the compiler?


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #2

"alex goldman" <he***@spamm.er> wrote in message
news:19****************@yahoo.com...
When compiling this code with
g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4

######################################
#include <vector>

class b {
public:
virtual void f() = 0;
};

class d : public b {
std::vector<int> v;
public:
void f() {}
};
#####################################

I get a warning:

program.cpp:8: warning: `class d' has virtual functions but non-virtual
destructor
The compiler is clearly telling you that the presence of a pure-virtual
member function in class b does *not* automatically generate a virtual
d~tor. And indeed, it doesn't - thats bad news.

in fact, you should have seen the following warnings with the
-ansi -pedantic -Wall options:

`class b' has virtual functions but non-virtual destructor
`class d' has virtual functions but non-virtual destructor

Am I very confused about the presence of virtual functions in class d, or is the compiler?


This has nothing to do with the virtual function as per say. The basic rule
in inheritence is that a virtual d~tor should replace the default
non-virtual d~tor in a base class you plan to derive from.

So class B should look like...

class B
{
public:
B() { }
virtual ~B() { }
virtual void f() = 0;
};

class D : public B
{
std::vector<int> vn;
public:
D() : vn() { }
~D() { } // is also virtual since base d~tor is virtual
void f() {} // is also virtual...
};

int main()
{
D d;

system("PAUSE");
return EXIT_SUCCESS;
}

No warning anymore...

Jul 23 '05 #3
Peter Julian wrote:
class D : public B
{
std::vector<int> vn;
public:
D() : vn() { }
Isn't the above line redundant?
~D() { } // is also virtual since base d~tor is virtual
void f() {} // is also virtual...
};


Jul 23 '05 #4
alex goldman wrote:
Peter Julian wrote:
class D : public B
{
std::vector<int> vn;
public:
D() : vn() { }


Isn't the above line redundant?


It is superfluous.
~D() { } // is also virtual since base d~tor is virtual
void f() {} // is also virtual...
};

Jul 23 '05 #5

"alex goldman" <he***@spamm.er> wrote in message
news:11****************@yahoo.com...
Peter Julian wrote:
class D : public B
{
std::vector<int> vn;
public:
D() : vn() { }


Isn't the above line redundant?


You're right: ts not required, but quite handy. vn(40, 0) would have
initialize 40 int elements to 0.

Jul 23 '05 #6

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
5
by: Ryan Faulkner | last post by:
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...
4
by: PengYu.UT | last post by:
Hi, It seems that the line with "//no error" and "//error" are the same. However, the second one give an error. I'm using g++-3.3. The error message was: g++-3.3 -g -c -o main.o main.cc...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
20
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
17
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
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:
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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: 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 ...

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.