类中的 C++ typedef

标签 c++ class typedef

我使用此 C++ 代码将 x 存储到变量 val。

class Hello
{
  int val;
public:
  Hello(int x) : val(x) {}
};

但是,当我看到这段代码时,我看不出 Super 是如何存储值 t 或 o 的。

template<typename T>
class K : public std::auto_ptr<T>
{
    typedef std::auto_ptr<T> Super;
public:
    K() : Super() { }
    K(T* t) : Super(t) { }
    K(K<T>& o) : Super(o) { }
};

这段代码是如何工作的?

最佳答案

可以写成

template<typename T>
class K : public std::auto_ptr<T>
{
public:
    K() : std::auto_ptr<T>() { }
    K(T* t) : std::auto_ptr<T>(t) { }
    K(K<T>& o) : std::auto_ptr<T>(o) { }
};

初始化基类只是比较冗长。如果您必须处理模板化的基类,大多数情况下 typedef 会更简洁。

关于类中的 C++ typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5186935/

相关文章:

C++:如何在协变返回类型中重用代码?

javascript - jQuery 类型类结构

python - 从父类控制子类方法

java - 我应该怎么做才能让用户在我的代码中设置自己的订单号?

C++ typedef 枚举 : Invalid conversion from int to enum

c++ - 在 LLVM 中生成函数指针

c++ - 在 C++ 中下载一个 URL

c++ - LDAP 连接 ldap_sasl_bind_s 给出断言

c++ - 如何保证 C++ 类型的位数

C++ : Make multiple constructors with the same argument types