大家好,我创建了一个mixin类(超级设计),用于打印出元素T(某种类型T),其中元素T的名称为name()。
我想知道这是否被认为是在C ++中实现的正确方法吗?
欢迎任何意见。
template<class T>
struct name_method_printer_to_console_mixin{
void print() const{
auto& that = static_cast<T const&>(*this);
cout << "Mixin printing name which is: " << that.name() << endl;
}
};
class customer : public name_method_printer_to_console_mixin<customer>{
public:
customer(){}
customer(string const &name) : name_(name){}
string const & name() const{
return name_;
}
private:
string name_;
};
布莱尔
最佳答案
看起来有效。不确定它是否有用,但是对于超级设计的课程来说,它是同等的。
我建议转换指针并使用that-> name()而不是引用。他们做同样的事情,但是指针会更容易理解
关于c++ - C++ Mixins-这是正确的实现方式吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17068514/