Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;返回类型取决于类的方法继承_C++_Templates_Inheritance - Fatal编程技术网

C++ C++;返回类型取决于类的方法继承

C++ C++;返回类型取决于类的方法继承,c++,templates,inheritance,C++,Templates,Inheritance,假设我有一门课: class Food { public: Food * copy() const { return new Food(*this); } . . . }; 我希望它成为继承copy方法的其他类的基类。每次我都必须手动编写: class Cheese: public Food { public: Cheese * copy() const { return new Cheese(*t

假设我有一门课:

class Food
{
public:
    Food * copy() const
    {
        return new Food(*this);
    }
    .
    .
    .
};
我希望它成为继承
copy
方法的其他类的基类。每次我都必须手动编写:

class Cheese: public Food
{
public:
    Cheese * copy() const
    {
        return new Cheese(*this);
    }
    .
    .
    .
};

class Bread: public Food
{
public:
    Bread * copy() const
    {
        return new Bread(*this);
    }
    .
    .
    .
};
如何解决这个问题?我正在考虑模板,但我仍然不知道如何使用它们。。。这是我制作的程序,它不会编译:

#include <cstring>

template <class T>

class Food
{
protected:
    double weight;
public:
    T * copy() { return new T(*this); }
    Food(){ weight = 1; }
    Food(const Food& f){ weight = f.weight; }
};

class Cheese : public Food<Cheese>
{
    char * color;
public:
    Cheese(const char * c)
    {
        color = new char[strlen(c)+1];
        strcpy(color,c);
    }
    Cheese(const Cheese& c)
    {
        weight = c.weight;
        color = new char[strlen(c.color)+1];
        strcpy(color,c.color);
    }
};

main()
{
    Cheese first("yellow");
    Cheese * second = first.copy();
}
#包括
模板
等级食品
{
受保护的:
双倍重量;
公众:
T*copy(){返回新的T(*this);}
食物(){weight=1;}
食物(const Food&f){weight=f.weight;}
};
类别:公共食物
{
字符*颜色;
公众:
奶酪(const char*c)
{
颜色=新字符[strlen(c)+1];
strcpy(颜色,c);
}
奶酪(康斯特奶酪公司)
{
重量=c.重量;
颜色=新字符[strlen(c.color)+1];
strcpy(颜色,c.color);
}
};
main()
{
奶酪优先(“黄色”);
Cheese*second=first.copy();
}
以下是错误:

|10|error: no matching function for call to ‘Cheese::Cheese(Food<Cheese>&)’|
|10|note: candidates are:|
|24|note: Cheese::Cheese(const Cheese&)|
|24|note:   no known conversion for argument 1 from ‘Food<Cheese>’ to ‘const Cheese&’|
|19|note: Cheese::Cheese(const char*)|
|19|note:   no known conversion for argument 1 from ‘Food<Cheese>’ to ‘const char*’|
||In member function ‘T* Food<T>::copy() [with T = Cheese]’:|
|10|warning: control reaches end of non-void function [-Wreturn-type]|
| 10 |错误:调用“Cheese::Cheese(Food&)”没有匹配的函数|
|10 |注:候选人包括:|
|24 |注:奶酪::奶酪(const Cheese&)|
|24 |注:参数1从'Food'到'const Cheese&'没有已知的转换|
|19 |注:奶酪::奶酪(常量字符*)|
|19 |注:参数1从“Food”到“const char*”的转换未知|
||在成员函数“T*Food::copy()[with T=Cheese]”中:|
|10 |警告:控件达到非无效功能的末尾[-Wreturn类型]|
我做错了什么?实现这一点的最佳方法是什么?

做到这一点

T * copy() { return new T(*static_cast<T*>(this)); }
T*copy(){返回新的T(*static_cast(this));}

Food::copy
“this”是
Food*
,而您需要一个
T*

是否与返回新的T(*(T*)this)相同?在这个特殊的例子中,是的。但是如果有人(可能是错误地)写了,比如说,
class-Foo:public-Food{…}
,那就错了
static_cast
将拒绝在指向不相关类的指针之间进行强制转换-您将看到一个错误-但是C风格的强制转换将很乐意继续进行,生成一个无效指针,并可能在运行时崩溃。另一个问题,如果我想要另一个继承
Cheese
的类,该怎么办<代码>奶酪类:公共食品好吧,但如果我想让莫扎里拉继承奶酪
class Mozzarella:public Cheese
不起作用。除非你把
Cheese
本身作为模板。这就是为什么你在实践中不经常看到这样奇特的解决方案——它们很难推广到任意的类层次结构。是的,所以在这种情况下最好的解决方案不是使用模板,而是手动编写方法。。。谢谢