由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How to initialize object in constructor?
相关主题
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?one question about initializaiton list
operator() overloading 一问一道 memset in C++的题
c++问题,请高人指点迷津,c++ faq lite的一个例子问一个constructor的问题 (转载)
c++ 一问C++ question
一个 default constructor 的问题What're the three types of memory allocated for C++ variables?
question about c++ constructor抠字眼:assignment and initialize in C++
问个C++的operator conversation function问题大家谈谈看??
Is the order of initialization a, b, c or c, b, a?c++ initialize struct
相关话题的讨论汇总
话题: pointcoll话题: point话题: thepointer话题: object
进入Programming版参与讨论
1 (共1页)
r*****r
发帖数: 397
1
One rule for constructor:use initialization instead of assignment. Is this
also true for object?For example,I have a simple class
Point{
public:
...
private:
float x;
float y;
}
Suppose I have another class and want to use a point as a private member,
PointColl{
public:
//default constructor
PointColl();
PointColl(const PointColl & thePoint);
...
private:
int number;
Point* thePointer;
}
For the default constructor, can I do like this
PointColl::PointColl():number(0)
{
thePointer = n
c*****t
发帖数: 1879
2
Use the one declaring the initiation parameters before { }

【在 r*****r 的大作中提到】
: One rule for constructor:use initialization instead of assignment. Is this
: also true for object?For example,I have a simple class
: Point{
: public:
: ...
: private:
: float x;
: float y;
: }
: Suppose I have another class and want to use a point as a private member,

y****i
发帖数: 156
3
Yes, the rule applied to object.
Reason
r*****r
发帖数: 397
4
Thank you.And I understand the rule, just not sure about the syntax.for
example,in default constructor,
PointColl::PointColl():
number(0),
thePointer(NULL)
{
}
thePointer(NULL) is equal to " thePointer = new Point();"?I mean, this will
create a new pointer.
For another constructor,I can use directory
PointColl::PointColl(int num,const Point& p):
number(num)
thePointer(p);
{
}
instead of
PointColl::PointColl(int num,const Point& p):number(num)
{
thePointer = new Point(p);
}
Thanks, again
r*****r
发帖数: 397
5
I mean, I tried for both case in the constructor
PointColl::PointColl(int num,const Point& p):
number(num)
thePointer(p);
{
}
and get an error "cannot convert 'Point' to 'Point*' in initialization
but if i use
PointColl::PointColl(int num,const Point& p):number(num)
{
thePointer = new Point(p);
}
it works fine.
why?

.

【在 y****i 的大作中提到】
: Yes, the rule applied to object.
: Reason

y****i
发帖数: 156
6

I mean, I tried for both case in the constructor
PointColl::PointColl(int num,const Point& p):
number(num)
thePointer(p);
{
}
and get an error "cannot convert 'Point' to 'Point*' in initialization
//---------------------------------
thePointer is a pointer(Point*) , however p is an object(Point)
You cannot assign a object to a pointe. If you really want to, you have to
cast it like thePoint = (Point*) p; // dangerous
Are you clear?
but if i use
PointColl::PointColl(int num,const Point& p):numbe

【在 r*****r 的大作中提到】
: I mean, I tried for both case in the constructor
: PointColl::PointColl(int num,const Point& p):
: number(num)
: thePointer(p);
: {
: }
: and get an error "cannot convert 'Point' to 'Point*' in initialization
: but if i use
: PointColl::PointColl(int num,const Point& p):number(num)
: {

r*****r
发帖数: 397
7
Yeah, very clear. So basically, for pointers, better use new to create instead
of initialization.Right?Thank you .

【在 y****i 的大作中提到】
:
: I mean, I tried for both case in the constructor
: PointColl::PointColl(int num,const Point& p):
: number(num)
: thePointer(p);
: {
: }
: and get an error "cannot convert 'Point' to 'Point*' in initialization
: //---------------------------------
: thePointer is a pointer(Point*) , however p is an object(Point)

y****i
发帖数: 156
8
Yes.
For object, it is better to init in init list
for builtin types or pointer, you can use both ways.
However, let p = new A() int constructor is much clear that in the init-list.

for pointers, better use new to create
instead: of initialization.Right?Thank you .

【在 r*****r 的大作中提到】
: Yeah, very clear. So basically, for pointers, better use new to create instead
: of initialization.Right?Thank you .

1 (共1页)
进入Programming版参与讨论
相关主题
c++ initialize struct一个 default constructor 的问题
pthread_create inside a constructorquestion about c++ constructor
问个copy constructor的问题问个C++的operator conversation function问题
请问以下代码有什么错误?Is the order of initialization a, b, c or c, b, a?
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?one question about initializaiton list
operator() overloading 一问一道 memset in C++的题
c++问题,请高人指点迷津,c++ faq lite的一个例子问一个constructor的问题 (转载)
c++ 一问C++ question
相关话题的讨论汇总
话题: pointcoll话题: point话题: thepointer话题: object