What does the standard say about this

X

Xenos

I just spend about an hour tracking down a link error with GCC. I had
defined a virtual member function, but never referenced it. With this
declaration, the linker would complain that the virtual table for the class
was missing. I know that it is acceptable by the standard to declare a
non-virtual member but not define it, if it is never accessed. Is the same
true for virtual members?

Thanks,

DrX
 
R

Rob Williscroft

Xenos wrote in in
comp.lang.c++:
I just spend about an hour tracking down a link error with GCC. I had
defined a virtual member function, but never referenced it. With this
declaration, the linker would complain that the virtual table for the
class was missing. I know that it is acceptable by the standard to
declare a non-virtual member but not define it, if it is never
accessed. Is the same true for virtual members?

No, but you can declare the member pure-virtual, in which case
(as long as it isn't the destructor) you don't need a defenition.

However it makes the class *abstract*, i.e. you may not create
instances of it (even if you do provide a defenition).

struct X
{
virtual int f() = 0; /* = 0 means: pure */
};

struct Y : X
{
virtual int f();
};

int Y::f()
{
return 0;
}

int main()
{
Y y;
return y.f();
}

Rob.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top