want to leave virtual functions undefined

K

kdotk

As an ex-C programmer moving to C++, I imagined this to be an FAQ, but
it isn't:

I have a class whose member functions are implemented in multiple
compilation
unit's (.o). Now, while linking my main program with these .o's, I can
exclude from
the link line all those .o's whose member functions are not called by
the main
program.

Except for virtual member functions, even if my main program never
calls them, I
have to include the corresponding .o's, or else, I get unsatisified
symbols for them
which I imagine are coming from the VTABLE.

What's the C++ way to fix this (multiple inheritance ?)

The C way is to just stub them out!

class Atom {
public:
const char *foo();
virtual const char *bar();
};

class HAtom : public Atom {
public:
const char *bar();
};

Atom::foo is defined in atom1.o. HAtom::bar and Atom::bar are defined
in atom2.o.
But I am forced to link in atom2.o even if my main never calls bar().
If bar() wasn't
virtual, I can get away.

Sincerely,
Kapil
 
A

Alf P. Steinbach

* kdotk:
I have a class whose member functions are implemented in multiple
compilation unit's (.o). Now, while linking my main program with these
.o's, I can exclude from the link line all those .o's whose member
functions are not called by the main program.

Except for virtual member functions, even if my main program never
calls them, I have to include the corresponding .o's, or else, I get
unsatisified symbols for them which I imagine are coming from the VTABLE.

What's the C++ way to fix this (multiple inheritance ?)

Basically it's Quality Of Implementation issue: your compiler & linker
should have some option to remove unused definitions.

If you have declared a virtual function in a class, you must also
implement it for that class, except if it's pure virtual (in which case
you cannot instantiate the class, so that's not what you want).
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top