C++:具有用户定义类型的构造函数

标签 c++ oop class methods constructor

我正在尝试理解 C++ 中的类并开发一些我在 Python 中看到的类似类。这是代码:

#include <iostream>
#include <cmath>
using namespace std;

/*============================================================================*/
/* Define types
/*============================================================================*/
class none_type;
class bool_type;
class int_type;
struct identifier;

/*============================================================================*/
/* Define none type
/*============================================================================*/
class none_type {
  public:
    none_type()                  { /* constructor */ };
    ~none_type()                 { /* destructor */ };
}; /* none_type */

/*============================================================================*/
/* Define bool type
/*============================================================================*/
class bool_type {
  private:
    bool base;
  public:
    bool_type()                  { base = false; };
    ~bool_type()                 { /* destructor */ };
    bool_type(bool init)         { base = bool(init); };
    bool_type(int init)          { base = bool(init); };
    bool_type(long init)         { base = bool(init); };
    bool_type(float init)        { base = bool(init); };
    bool_type(double init)       { base = bool(init); };
    bool_type(bool_type init)    { base = bool(init.base); };
    bool_type(int_type init)     { base = bool(init.base); };
    int get()                    { cout << base << endl; };
}; /* bool_type */

/*============================================================================*/
/* Define int type
/*============================================================================*/
class int_type {
  private:
    long base;
  public:
    int_type()                   { base = 0; };
    ~int_type()                  { /* destructor */ };
    int_type(bool init)          { base = long(init); };
    int_type(int init)           { base = long(init); };
    int_type(long init)          { base = long(init); };
    int_type(float init)         { base = long(init); };
    int_type(double init)        { base = long(init); };
    int_type(bool_type init)     { base = long(init.base); };
    int_type(int_type init)      { base = long(init.base); };
    int get()                    { cout << base << endl; };
}; /* int_type */

当我尝试编译它时,g++ 告诉我所有使用我自己的类型的构造函数都是无效的。你能解释一下哪里出了问题吗?我已经定义了类原型(prototype),我还应该做什么?提前致谢!

最佳答案

这个构造函数:

bool_type(int_type init)     { base = bool(init.base); };

无效,因为此时 int_type 不完整。

您必须将此构造函数实现移出类定义,直到 int_type 完成:

class bool_type {
  bool_type(int_type init);
};
class int_type {};
inline bool_type::bool_type(int_type init)     { base = bool(init.base); };

另一个问题是你的构造函数伪装成复制构造函数:

bool_type(bool_type init)    { base = bool(init.base); };

这里你有无限递归 - 因为 init 参数是一个拷贝 - 所以必须调用这个构造函数来制作这个拷贝,但是这个构造函数的下一次调用有它自己的 init 必须复制的参数等到无穷大或堆栈限制...

拷贝构造函数的正确定义如下:

bool_type(const bool_type& init)    { base = bool(init.base); };

必须使用 Const 引用,但是在这种情况下您可以依赖编译器 - 它会为您生成复制构造函数 - 所以只需将其删除即可。

关于C++:具有用户定义类型的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13105523/

相关文章:

c++ - C++中模板的原理是什么?

c++ - 使用 OpenCV 在交叉点画线

c++ - 在 C++ 中,说一个变量具有右值引用类型有意义吗?

JavaScript:尝试使用由于某种原因无法运行的类函数

c++ - 围绕椭圆移动球体

C#引用分配未按预期工作

c# - 如何验证类中 List<> 属性中的数据?

python - python中继承多个类无需多次初始化

c++ - 从其他类调用此方法会创建违规阅读

c++ - 类中的析构函数发生内存泄漏,该类的嵌套类为私有(private)