473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ 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 4090
"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
4429
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 one vtbl ptr BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes . class A = 4 bytes class B :public A = 4 bytes class...
5
2926
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 virtual function plus redefinitions of the three inherited virtual functions. Following is a simplified code fragment to illustrate my code and my problem:
4
1541
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 main.cc: In function `int main(int, char**)':
37
4181
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
2417
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
14719
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 vfptr i.e the pointer to vtable internally. What confuses me is if vtable is an array of pointer to virtual functions then as per the properties of the array all the entries inside the array must be same.i.e all the pointers should be of similar...
3
2071
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 destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
23
4616
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
3551
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;" instead? I think declaring a function as "=0" is the same
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.