C :具有用户定义类型的构造函数
C++: constructor with user-defined types
我正在尝试了解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()) { }
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 构造函数正在调用一个使用当前类类型的函数
- 如何修复函数样式强制转换或类型构造的预期"("?
- 具有默认模板类型的默认构造函数的类型推导
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 不命名构造函数和析构函数上的类型错误
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 创建类类型的动态分配数组,其中类不得具有默认构造函数
- 初始化自定义类型构造函数的数组
- OpenGL着色器错误C1068:类型构造函数中的数据过多
- Lambda 闭包类型构造函数
- 仅当基础类型具有模板类型构造函数时,才实现这些构造函数
- 模板类的构造函数在使用 new 关键字时调用类型构造函数
- 如何强制函数参数为相同类型,并且不允许使用类型构造函数与给定类型匹配
- 模板类型构造函数参数
- 它们是c++中的类型构造函数
- 解析C++类型构造函数