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

C++: constructor with user-defined types

本文关键字:类型 构造函数 定义 用户      更新时间:2023-10-16

我正在尝试了解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++告诉我所有使用自己类型的构造函数都是无效的。你能解释一下怎么了吗?我已经定义了类原型,我还应该做什么?预先感谢!

此构造函数:

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引用必须使用,但是在此过程中,您可以依靠编译器 - 它将为您生成复制构造函数 - 因此,只需删除它即可。

g 已经告诉你,怎么了:

错误:无效的构造函数;您可能是指'boo_type(const boo_type&amp;)'

而不是bool_type (bool_type),您必须使用bool_type (const bool_type&)。原因是,如果您按值通过对象,编译器使用复制构造函数将其放在堆栈上。因此,为了将bool_type传递到复制构造函数bool_type(bool_type),它必须使用复制构造函数本身。那是不可能的。

int_type(int_type)

也是如此

在构造函数'bool_type :: bool_type(int_type)': 错误:" init"类型不完整

在这一点上,G 不知道int_type的外观。由于它不知道int_type具有base成员,因此无法使用它。只需声明构造函数:

bool_type(int_type init);

并在 int_tpye的声明之后定义它:

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

当您拥有较大的对象时,请建议通过引用使用它们,因为通过值传递意味着复制堆栈上的对象。这要贵得多(对于大物体而言),而不是传递对这个大物体的引用。对于小物体,这没关系。

和最后一个错误:

在构造函数'int_type :: int_type(bool_type)': 错误:'bool boo_type :: base'是私人

您已将bool_type中的成员base声明为private:。这意味着,仅允许bool_type访问此成员。为了获得base,您必须使用访问方法get()

int_type(bool_type init)     { base = long(init.get()); }

类似,您必须定义:

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

最后,查看C -FAQ或C ,然后遵循书籍列表。C 常见问题解答也很不错。

编辑:我错过了,您的get()方法根本不是访问者。它们应定义为:

class bool_type {
public:
    bool get() const { return base; }
...
};

int_type::get()

相同
int get() const { return base; }

很难提供"学习C "的具体建议,但是以下是可以在"正常" C 中设计的课程:

class int_type;
class none_type { };
class bool_type
{
    bool base;
public:
    bool_type() : base() { }
    explicit bool_type(bool init) : base(init) { }
    explicit bool_type(int_type const &);
    void print() const { std::cout << base << std::endl; }
    bool get() const { return base; }
};
class int_type
{
    int base;
public:
    int_type() : base() { }
    explicit int_type(int init) : base(init) { }
    explicit int_type(bool_type const & init) : base(init.get() ? 1 : 0) { }
    void print() const { std::cout << base << std::endl; }
    int get() const { return base; }
};
inline bool_type::bool_type(int_type const & init) : base(init.get()) { }