由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这个类的default constructor怎么写
相关主题
菜鸟请教smart pointer为啥gcc找不到类的构造函数?
c++ 一问一个c++小问题
请问以下代码有什么错误?问个 std::vector 的基本问题
operator() overloading 一问string operator +
why do we still use dynamic allocation?const in c++
question overloading ++ error一个C++ operator new的重载问题
C++ operator = overloading用copy & swap有啥优点Is the order of initialization a, b, c or c, b, a?
c++ iterator 弱问question about Design Patterns
相关话题的讨论汇总
话题: default话题: const话题: 定义话题: vector
进入Programming版参与讨论
1 (共1页)
o**********a
发帖数: 330
1
class A {
public:
A(const int, const vector&);
A(const A&);
virtual A& operator=(const A&);
//...
protected:
int i;
vector s;
};
A::A(const int a,const vector& b):i(a),s(b){}
//我这么写没有定义defualt constructor,请问如何定义这个constructor
//另外,一般写类的时候,是不是最好都定义default contructor
X****r
发帖数: 3557
2
这个别人怎么知道你想要它default behavior是什么。
并不是一定要有default constructor的。

【在 o**********a 的大作中提到】
: class A {
: public:
: A(const int, const vector&);
: A(const A&);
: virtual A& operator=(const A&);
: //...
: protected:
: int i;
: vector s;
: };

k******r
发帖数: 2300
3
default constructor 并不是绝对需要,但一般都要。判断一个类是否需要default
constructor 就是你定义类的实例到底能不能有一个default value。 一般是最好有一
个default constructor, 但是如果你发现你定义类的实例不可能在compile time 确
定一个default value, 就不定义default constructor。

【在 o**********a 的大作中提到】
: class A {
: public:
: A(const int, const vector&);
: A(const A&);
: virtual A& operator=(const A&);
: //...
: protected:
: int i;
: vector s;
: };

1 (共1页)
进入Programming版参与讨论
相关主题
question about Design Patternswhy do we still use dynamic allocation?
why copy assignment operator returns non-const type?question overloading ++ error
How to initialize object in constructor?C++ operator = overloading用copy & swap有啥优点
C++问题,confusing...c++ iterator 弱问
菜鸟请教smart pointer为啥gcc找不到类的构造函数?
c++ 一问一个c++小问题
请问以下代码有什么错误?问个 std::vector 的基本问题
operator() overloading 一问string operator +
相关话题的讨论汇总
话题: default话题: const话题: 定义话题: vector