c++ - 重写构造函数 C++

标签 c++ inheritance constructor

我有抽象类A

class A{
 public:
  A(dim) : dim_(dim);
 private:
  int dim_;
}

和B级

class B : public A{
 public:
  B(int dim);
}

我需要为类 B 创建构造函数,它仅在 dim > 1 时有效,否则抛出断言。

在这种情况下

B::B(int dim) : A(dim){
  assert(dim > 1);
}

它有效,但我认为这不是好的解决方案,因为创建和删除了类 A 的实例。

比起我为 A 类制作初始化方法:

class A{
 public:
  void init(int dim){
    dim_ = dim;
  }
  A(int dim){
    init(dim);
  }
 private:
  int dim_;
}

并更改 B 类的构造函数:

class B : public A {
 public:
  B(int dim){
    assert(dim > 1);
    init(dim);
  }
}

但它不起作用。我的问题是否有任何可能的解决方案?

最佳答案

我想你可以写一个小的 myint 类来确保你传递的 int 总是大于 1:

struct myint
{
    int data; 
    myint(int i) : data(i) { assert(data > 1); }
};

现在在你的类里面使用它:

class B : public A{
 public:
  B(myint dim) //this can still take int, due to implicit conversion! 
   : A(dim.data) { }
}

请注意,您仍然可以构造 B 传递 int,因为它将隐式转换为 myint 并且在转换发生时(隐式) ,它将测试 assert,如果成功,只有这样你才能将 dim.data 传递给基类 A。如果 assert 失败,您的程序将在进入基类构造函数之前中止(也不会在派生类中初始化任何内容)。


您甚至可以将其概括为:

//Summary : gint<N> makes sure that data > N
template<int N>
struct gint  //call it greater int
{
    int data; 
    gint(int i) : data(i) { assert(data > N); } //Use N here!
};

现在在你的类里面使用它:

class B : public A{
 public:
  B(gint<1> dim) //the template argument 1 makes sure that dim.data > 1
   : A(dim.data) { }
}

如果你需要另一个类,例如:

class Xyz : public A{
 public:
  B(gint<10> dim)  //gint<10> makes sure that dim.data > 10
   : A(dim.data) { }
}

很酷,不是吗?

关于c++ - 重写构造函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176199/

相关文章:

c++ - 现在有了内联变量,extern const仍然有用吗?

python - "We' 是不可变的,所以使用 __new__ 而不是 __init__"

c++ - 为什么声明复制构造函数不会删除复制赋值运算符,反之亦然?

C# - 只读属性和字段不能在派生类构造函数中设置

c# - 将 IEnumerable 转换为自定义 ObservableCollection 类?

c++ - 在使用带有 https 的 url 时,我遇到了 Casablanca C++ 的问题

c++ - 从 yml 文件初始化相机参数 opencv C++

c++ - (C++) 命令行剪刀石头布游戏的 If - Else if 语句

java - 从 JVM 的角度来看,从原始数组继承是不可能的吗?

c++ - 析构函数不在控制台上打印行