开发者

Abstract object cannot declare

I'm having a problem with abstract/virtual classes, a replication of the problem here:

#include <iostream>

class A
{
protected:
    virtual std::string getDateTime() = 0;
    virtual void Write(std::string data, bool addDate) = 0;
    virtual bool CheckFile() = 0;
    virtual bool OpenFile(std::string path) = 0;
    virtual void CloseFile() = 0;
};

class B
    : public A
{
public:
    virtual std::string ToString() { return ""; };
    virtual void Write(std::string data) { };
};

class C
    : public A
{
protected:
    std::string getDateTime()
    {
        return "TODAY";
    };

    void Write(std::string data, bool addDate)
    {
        std::cout << "BasicClassA Write" << std::endl;
    };

    bool CheckFile()
    {
        std::cout << "BasicClassA CheckFile" << std::endl;
        return true;
    };

    bool OpenFile(std::string path)
    {
        std::cout << "BasicClassA OpenFile" << std::endl;
        return true;
    };

    void CloseFile()
    {
        std::cout << "BasicClassA CloseFile" << std::endl;
    };
};

class D
    : public B,
      public C
{
public:
    BasicClassB();
    virtual ~BasicClassB();

    std::string ToString()
    {
        return "BasicClassB tostring";
    };

    void Write(std::string data)
    {
        std::cout << "BasicClassB Write" << std::endl;
    };
};

int main(int ac, char *av[])
{
    BasicClassB b;
    std::cout << b.ToString() << std::endl;
    b.Write("");
    return 0;
}

This has a compile error:

../src/main.cpp: In function ‘int main(int, char**)’:

../src/main.cpp:82: error: cannot declare variable ‘b’ to be of abstract type ‘BasicClassB’

../src/main.cpp:64: note: because the following virtual functions are pure within ‘BasicClassB’:

../src/main.cpp:13: note: virtual std::string BaseClassA::getDateTime()

../src/main.cpp:14: note: virtual void BaseClassA::Write(std::string, bool)

../src/main.cpp:15: note: virtual bool BaseClassA::CheckFile()

../src/main.cpp:16: note: virtual bool BaseClassA::OpenFile(std::string)

../src/main.cpp:17: note: virtual void BaseClassA::CloseFile()

Perhaps I'm missing the point here, but the implementation of BaseClassA (being BasicClassA) should contain these functions, and since BasicClassB is subclassed from BasicClassA as well, it should also contain these functions?

What am I missing? What should I do to make this compile?

[edit] I updated the class names as suggested by the comment

For clarification: I used pure virtual in the class A to force any of t开发者_如何学Pythonhe children to implement the functions.

It seems virtual inheritance is what I need, however, I don't seem to get the correct way on how to do this in my case...

The goal is to have several "base" classes, kind of like interfaces, forcing the children to implement the functions, but any children of those should inherit the overriden function (just like virtual inheritance)

However, using any combination of class Any : public virtual Anyother { } doesn't work out and always gives the same compile error (the one above). Perhaps I need to change more than just the virtual in the inheritance?


It doesn't work that way by default in C++ - You want a diamond inheritance pattern, but in C++ you get separate roots: So BasicClassA and BaseClassB each have their own BaseClassA (vtable and instance variables).

You probably want to use Virtual Inheritance.

For a clearer idea on non-virtual inheritance:

#include <iostream>


class A
{
    public:
        A(int x) {m_a = x;}
        virtual ~A() {}
        int m_a;
        virtual int getA() {return m_a;}
};

class B : public A
{
    public:
        B() : A(1) {}
};

class C : public A
{
    public:
        C() : A(2) {}
};

class D : public B,
          public C
{
};

void useB(B* b)
{
    std::cout << "useB:" << b->getA() << std::endl;
}

void useC(C* c)
{
    std::cout << "useC:" << c->getA() << std::endl;
}

int main()
{
    D* d = new D();
    useB(d);
    useC(d);

    return 0;
}

This produces the output:

useB:1
useC:2

This example shows virtual inheritance, and the kind of mix-in behaviour you want.

#include <iostream>


class A
{
    public:
        A(int x) {m_a = x;}
        virtual ~A() {}
        int m_a;
        virtual int getA() {return m_a;}
        virtual int virt() = 0;
};

class B : virtual public A
{
    public:
        B() : A(1) {}
};

class C : virtual public A
{
    public:
        C() : A(2) {}
        virtual int virt() {return 42;}
};

class D : public B,
          public C
{
    public:
        D() : A(3) {}
};



void useB(B* b)
{
    std::cout << "useB:" << b->getA() << std::endl;
}

void useC(C* c)
{
    std::cout << "useC:" << c->getA() << std::endl;
    std::cout << "useC-virt:" << c->virt() << std::endl;
}

int main()
{
    D* d = new D();
    useB(d);
    useC(d);

    return 0;
}

Output:

useB:3
useC:3
useC-virt:42

Note: The constructors from C and B don't get a say in setting m_a, which is controller by the D() constructor initialisation list.

EDIT: Applying virtual to your code:

#include <iostream>

class A
{
protected:
    virtual std::string getDateTime() = 0;
    virtual void Write(std::string data, bool addDate) = 0;
    virtual bool CheckFile() = 0;
    virtual bool OpenFile(std::string path) = 0;
    virtual void CloseFile() = 0;
};

class B
    : virtual public A
{
public:
    virtual std::string ToString() { return ""; };
    virtual void Write(std::string data) { };
};

class C
    : virtual public A
{
protected:
    std::string getDateTime()
    {
        return "TODAY";
    };

    void Write(std::string data, bool addDate)
    {
        std::cout << "C Write" << std::endl;
    };

    bool CheckFile()
    {
        std::cout << "C CheckFile" << std::endl;
        return true;
    };

    bool OpenFile(std::string path)
    {
        std::cout << "C OpenFile" << std::endl;
        return true;
    };

    void CloseFile()
    {
        std::cout << "C CloseFile" << std::endl;
    };
};

class D
    : public B,
      public C
{
public:
    std::string ToString()
    {
        return "D tostring";
    };

    void Write(std::string data)
    {
        std::cout << "D Write" << std::endl;
    };
};

int main(int ac, char *av[])
{
    D b;
    std::cout << b.ToString() << std::endl;
    b.Write("");
    return 0;
}


BaseClassA has 5 pure virtual functions. A class with even one pure virtual function is an "Abstract class". The purpose of pure virtual functions (in short) is to disallow creation of objects of the abstract class.

In order to instantiate BaseClassB, it needs to have definitions of all 5 functions which you declared pure virtual in BaseClassA. (In absence of these definitions, BaseClassB also becomes Abstract and hence you cannot create objects from it).


BasicClassB only derives from BaseClassA which is an abstract class since those methods :

virtual std::string getDateTime() = 0;
virtual void Write(std::string data, bool addDate) = 0;
virtual bool CheckFile() = 0;
virtual bool OpenFile(std::string path) = 0;
virtual void CloseFile() = 0;

Are pure virtual.

The error message is pretty clear: to be able to instantiate a BasicClassB you must provide an implementation for the forementioned methods.

Also, note that your definition of Write in BasicClassB:

virtual void Write(std::string data) { };

Differs from the one in BaseClassA:

virtual void Write(std::string data, bool addDate) = 0;

So this method still needs to be implemented for BasicClassB to become instantiable.


The fact that you add "=0" to your functions means that they are purely virtual, and must be implemented in child classes. Which is obviously not what you want. If you drop the "=0" from the functions that have an implementation in the base class, it should be working as intended.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜