what virtual functions?!

A

alex goldman

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

Cy Edmunds

alex goldman said:
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.
 
P

Peter Julian

alex goldman said:
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...
 
P

Peter Julian

alex goldman said:
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.
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top