Redian新闻
>
How to initialize object in constructor?
avatar
How to initialize object in constructor?# Programming - 葵花宝典
r*r
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
avatar
c*t
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,

avatar
y*i
3
Yes, the rule applied to object.
Reason
avatar
r*r
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
avatar
r*r
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

avatar
y*i
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)
: {

avatar
r*r
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)

avatar
y*i
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 .

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。