0

I have an abstract base class.

I have a bunch of member functions in it which are not private nor static nor templates, just normal public member functions.

And I noticed that I have to define any these public normal functions that *are being called in the definition of other functions even if those functions are not being run by any code in main.

And what calls more my attention is that in any case if the class is abstract, why should actually be any function forcefully defined if the class itself is not going to be instantiated any way??

This is the firs question I post here, so sorry if I am not explaining myself well or doing something wrong.

Thanks in advance for your help!

Here is a shortened version of the class I am talking about:

#ifndef PROCESS_H_INCLUDED
#define PROCESS_H_INCLUDED

#include <iostream>
#include <memory>


using std::weak_ptr;
using std::shared_ptr;

class Process;

typedef shared_ptr<Process> shProcess;
typedef weak_ptr<Process> wProcess;


class Process
{
    friend class ProcessManager;
public:

    inline void AttachChild(shProcess shChild);

    /*
    FOR EXAMPLE I CAN LEFT THE FUNCTION ABOVE WITHOUT BEING DEFINE, BUT I HAVE
    TO DEFINE THE FUNCTION BELOW OR I GET A COMPILER ERROR
    COULD IT BE BECAUSE EVEN IF THIS CLASS IS ABSTRACT I AM USING IT AS A 
    PARAMETER OF OTHER FUNCTIONS FOR POLIMORPHIC PURPOSES?
    */
    shProcess RemoveChild(void){return shProcess();}//reliases ownership of the child  

    shProcess PeekChild(void){return m_shChild;} //doesn´t release ownership of the child

};

#endif //PROCESS_H_INCLUDED
tenorsax
  • 21,123
  • 9
  • 60
  • 107
Azkron
  • 41
  • 1
  • 5

2 Answers2

1

You can get away with only declaring functions (and not providing a body) if the linker can prove they will never be called.

Virtual functions can get in the way of this, however, since the compiler has to build a vtable (some other compilation unit might inherit from the class and call the functions), and then the linker's task is much much tougher.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • But in this case, as the class is abstract its functions will never be called directly anyway, so why would I still have to define them? – Azkron Aug 04 '12 at 15:37
  • The address is needed, to put in the vtable. You can't have a function pointer to a function that isn't defined, it will cause a link error. – Ben Voigt Aug 04 '12 at 15:42
0

You can achieve the effect you want with a curious, but nonetheless standard, use of the syntax = 0, as in

class T {
    // ...
  public:
    virtual int foo() = 0;
};
thb
  • 13,796
  • 3
  • 40
  • 68