2013-06-12 50 views
1

大家好我创建了一个mixin类(超级设计),用于打印出名为()的方法的元素T(某些类型为T)。C++ Mixins - 这是实现的正确方法吗?

我想知道这是否被认为是在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_; 
}; 

布莱尔

回答

0

看起来有效。不确定它是否有用,但这是超级设计的课程。

我会建议投射指针并使用 - > name()代替引用。他们做同样的事情,但指针会更容易理解

相关问题