【问题标题】:Implement java like interface in C++在 C++ 中实现类似 java 的接口
【发布时间】:2015-12-20 23:00:17
【问题描述】:

我想创建一个Animal 接口。 Cat 类实现了它。一只动物可以吃另一只动物eat(Animal a)Animal.die() 会杀死动物,Animal.isDead() 返回,如果动物死了,或者没有。

如果我想编译它,我会得到一些错误:

templates may not be 'virtual'
invalid use of incomplete type 'class A'
expected class-name before '{' token

我已经搜索了很多,如何解决这个错误。但他们都没有解决它。我不是C++ 专家。我只有几年的 JAVA 经验。

代码:

#include <iostream>

using namespace std;

//interface
class Animal {

public:
    template<class A extends Animal>
    virtual void eat(A* a) = 0;
    virtual void die() = 0;
    virtual bool isDead() = 0;

};

// Cat class
class Cat: Animal {

private:
    bool dead = false;

public:
    Cat();
    Cat(const Cat& orig);
    virtual ~Cat();

    template<class A extends Animal>
    void eat(A* a);
    void die();
    bool isDead();

};

// Implement cat
Cat::Cat() {
}

Cat::Cat(const Cat& orig) {
}

Cat::~Cat() {
}


template<class A extends Animal>
void Cat::eat(A* a) {
    a->die();
}

void Cat::die() {
    dead = true;
}

bool Cat::isDead() {
    return dead;
}

int main(int argc, char** argv) {

    Cat* cat = new Cat();
    Cat* cat2 = new Cat();

    cat->eat(cat2);

    cout << (cat2->isDead()? "Cat2 is dead" : "Cat2 is not dead") << endl;

    return 0;
}

【问题讨论】:

  • 请查阅 C++ 基础书籍。
  • 看起来你在猜。你不能那样学习C++。我建议通过一本好书有条不紊地工作:stackoverflow.com/questions/388242/…
  • 虽然在思考 C++ 的同时尝试编写 Java 是一个坏主意,但在思考 Java 的同时尝试编写 C++ 也是一个坏主意。许多设计良好的 C++ 程序与 Java 典型的几乎纯 OO 方法相去甚远。
  • Implement java like interface in C++ 嗯...没有。
  • 我不想用 JAVA 风格编写 C++ 代码。我只是想弄清楚,如何在 C++ 中解决同样的问题

标签: c++ class templates interface


【解决方案1】:

您的问题与泛型有关。它们与模板不同,即使它们的语法有些相似。

替换:

template<class A extends Animal>
virtual void eat(A* a);

与:

virtual void eat(Animal*);

然后你的代码编译。 Java 基本上执行上述操作,但是当您调用 eat 时,它会存储 A 的运行时类型,并且在某些情况下会为您将其转换回该类型。在 C++ 中,您有责任自己进行转换。

您可以使用模板 tomfoolery 复制 Java 对泛型所做的大部分或全部操作,但这很少值得费心。

【讨论】:

    【解决方案2】:

    首先,您必须指定公共继承语句才能以多态方式使用您的Animal 接口。

    class Cat: public Animal
    

    其次,C++ 模板声明与 Java 略有不同

    template <class T>
    class ClassName
    {
        void ClassMethod(T* pObject);
    };
    

    在方法定义中

    template <class T>
    void ClassName<T>::ClassMethod(T* pObject)
    {
    }
    

    所以你的代码必须这样显示

    template <class A>
    class Animal {
    
    public:
        virtual void eat(A* a) = 0;
        virtual void die() = 0;
        virtual bool isDead() = 0;
    
    };
    
    // Cat class
    template <class A>
    class Cat: public Animal<A> {
    
    private:
        bool dead = false;
    
    public:
        Cat();
        Cat(const Cat<A>& orig);
        virtual ~Cat();
        void eat(A* a);
        void die();
        bool isDead();
    
    };
    
    // Implement cat
    template <class A>
    Cat<A>::Cat() {
    }
    
    template <class A>
    Cat<A>::Cat(const Cat<A>& orig) {
    }
    
    template <class A>
    Cat<A>::~Cat() {
    }
    
    
    template<class A>
    void Cat<A>::eat(A* a) {
        a->die();
    }
    
    template <class A>
    void Cat<A>::die() {
        dead = true;
    }
    
    template <class A>
    bool Cat<A>::isDead() {
        return dead;
    }
    
    int main(int argc, char** argv) {
    
        Cat<Cat>* cat = new Cat<Cat>();
        Cat<Cat>* cat2 = new Cat<Cat>();
    
        cat->eat(cat2);
    
        cout << (cat2->isDead()? "Cat2 is dead" : "Cat2 is not dead") << endl;
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多