Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++'s pure virtual function implementation and header files

I'm having some trouble implementing pure virtual functions inherited from some abstract class, when the classes in question are divided into *.h and *.cpp files. The compiler (g++) tells me that the derived class cannot be instantiated because of the existence of pure functions.

/** interface.h**/
namespace ns
{
    class Interface {
        public:
            virtual void method()=0;
    }
}

/** interface.cpp**/
namespace ns
{
    //Interface::method()() //not implemented here
}

/** derived.h **/
namespace ns
{
    class Derived : public Interface {
        //note - see below
    }
}

/** derived.cpp **/
namespace ns
{
    void Derived::Interface::method() { /*doSomething*/ }
}

/** main.cpp **/
using namespace ns;
int main()
{
    Interface* instance = new Derived; //compiler error
}

Does this mean that I have to declare the method() twice - in the Interface's *.h and in the derived.h too? Is there no other way around?

like image 504
Neo Avatar asked Jan 13 '11 00:01

Neo


People also ask

Can a pure virtual function have an implementation?

A pure virtual function (or abstract function) in C++ is a virtual function for which we can have implementation, But we must override that function in the derived class, otherwise the derived class will also become abstract class (For more info about where we provide implementation for such functions refer to this ...

What is the implementation of making a function pure virtual function?

To create a pure virtual function, rather than define a body for the function, we simply assign the function the value 0.

Which class provides implementation of pure virtual function?

A class that contains a pure virtual function is known as an abstract class. In the above example, the class Shape is an abstract class. We cannot create objects of an abstract class.

What is pure virtual function explain with example?

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax. For example: class Base {


2 Answers

You have to declare your method in the subclass.

// interface.hpp
class Interface {
public:
    virtual void method()=0;
}

// derived.hpp
class Derived : public Interface {
public:
    void method();
}

// derived.cpp
void
Derived::method()
{
    // do something
}
like image 94
robert Avatar answered Oct 07 '22 06:10

robert


You forgot to declare Derived::method().

You tried to define it at least, but wrote Derived::Interface::method() rather than Derived::method(), but you did not even attempt to declare it. Therefore it doesn't exist.

Therefore, Derived has no method(), therefore the pure virtual function method() from Interface was not overridden... and therefore, Derived is also pure virtual and cannot be instantiated.

Also, public void method()=0; is not valid C++; it looks more like Java. Pure virtual member functions have to actually be virtual, but you did not write virtual. And access specifiers are followed by a colon:

public:
    virtual void method() = 0;
like image 41
Lightness Races in Orbit Avatar answered Oct 07 '22 06:10

Lightness Races in Orbit