c++ Mixins——这是正确的实现方式吗?
C++ Mixins - Is this the correct way of implementing?
大家好,我已经创建了一个mixin类(超级设计),用于打印元素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_;
};
布莱尔看起来有效。我不确定它是否有用,但这是超级人造课程的标准。
我建议转换指针并使用->name()而不是引用。它们做同样的事情,但是指针更容易理解
相关文章:
- 我应该避免多重实现继承吗
- C++17 并行算法已经实现了吗?
- 我们可以使用命名空间实现封装吗?
- 编译器会秘密增加结构的对齐方式吗?
- 长度库,其实现方式类似于C++时间的计时
- 在这种情况下,有什么正确的方法可以实现锁定吗?
- 确定范围是访问虚拟功能的合法方式吗?
- 在C++中,我不能在不分离标头和 cpp 的情况下实现此类吗?
- 有什么优雅的方式吗?(类型参数包)
- 有人能告诉我这是如何在main()中实现的吗?(关于无效指针的问题)
- 可以使用 1D 数组实现 0-1 背包吗?
- 我可以在C++中创建一个匿名的动态类(接口的实现)吗?
- Qt:对于生产者-消费者模式中的消费者来说,这是正确的退出方式吗
- 常量标识符在C++中有不同的处理方式吗
- C++11中的所有STL都是用户可实现的吗
- C ++变量赋值,这是正常的方式吗
- 为什么 vector::operator[] 的实现方式与 map::operator[] 不同?
- 有清除类型的快捷方式吗
- c++ Mixins——这是正确的实现方式吗?
- 枚举是实现位标志的规范方式吗?