关于某事的两个问题 [英] Two questions about...something

查看:56
本文介绍了关于某事的两个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在正在读一本STL书,而且我已经拿到了以下语法:


class C {

private:

int value;

public:

C(int initValue ):value(initValue){}

}


我可以看到它的作用,但是......为什么?为什么用这种方式写成

,而不是:


class C {

private:

int value;

public:

C(int initValue){

value = initValue;

}

}


第一个片段看起来像是一些继承东西,但

显然它正在初始化私有成员。那么为什么

是这个使用的语法,并且它与第二个片段的质量有什么不同?


- -

Fred H


void FredH ::联系人(){

TextToSpeach.say(" frode at age dee dee dot en oh");

}


I''m reading a STL book right now, and there I''ve
picked up the folowing syntax:

class C {
private:
int value;
public:
C(int initValue) : value(initValue) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it''s initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}

推荐答案

" Fred H" < SE **** @ nospam.com>写了...
"Fred H" <se****@nospam.com> wrote...

我正在读一本STL书,而且我已经找到了下面的语法:
C类{
私有:
int value;
public:
C(int initValue):value(initValue){}
}
私人:
int value;
公开:
C (int initValue){
value = initValue;
}


第一个片段看起来像是一些继承东西,但很明显它是''初始化私人成员。那么为什么
这个语法被使用了,它与第二个片段的质量有什么不同呢?

I''m reading a STL book right now, and there I''ve
picked up the folowing syntax:

class C {
private:
int value;
public:
C(int initValue) : value(initValue) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it''s initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?




这包括在常问问题。请在发布前阅读常见问题解答。


我认为你太早开始阅读STL书籍。你需要

a C ++书和足够的时间,所以你不要问问题

那样。


Victor



This is covered in the FAQ. Please read FAQ before posting.

I think you started reading the STL book too early. You need
a C++ book and enough time with it so you don''t ask questions
like that.

Victor


Fred H写道:

第一个片段看起来像是一些继承物,但
显然它是'初始化私人成员。那么为什么
使用这种语法,并且它与第二个片段的质量有什么不同呢?

The first snippet looks like some inherit-stuff, but
obviously it''s initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?




1)

第一个剪辑是初始化,第二个是

赋值。在这里使用int它并没有太多的价值

的区别。但请考虑:


C级{

私人:

SomeExpensiveClass值;

public:

C(SomeExpensiveClass initValue):value(initValue){}

}


在第一次剪切时,initValue用于构造

值。

在第二个剪切值中默认构造。然后

将initValue分配给值。这通常非常低效。


2)

C级{

私人:

const int value;

public:

C(int initValue):value(initValue){}

}


Const成员只能初始化,不能设置。所以

的第二个片段不适用于上面的代码。


3)

使用初始化代替赋值被视为好​​风格。所以更喜欢写你的构造函数,比如

先破解。


hth


Christoph



1)
The first snipped is initialization, the second one
assignment. Here with the int it doesnt make much
difference. But consider:

class C {
private:
SomeExpensiveClass value;
public:
C(SomeExpensiveClass initValue) : value(initValue) { }
}

In the first snipped the initValue is used to construct the
value.
In the second snipped value is default constructed. Then
initValue is assigned to value. This is most often very
inefficient.

2)
class C {
private:
const int value;
public:
C(int initValue) : value(initValue) { }
}

Const members can only be initialized, not set. So the
second snippet doesnt work with the above code.

3)
Using initialization instead of assignment is considered
good style. So prefer to write your constructors like the
first snipped.

hth

Christoph




" Fred H" < SE **** @ nospam.com>在留言中写道

news:op ************** @ news.mimer.no ...

"Fred H" <se****@nospam.com> wrote in message
news:op**************@news.mimer.no...

我我现在正在阅读一本STL书籍,并且我已经接受了以下语法:

C类{
私人:
int value;
public:
C(int initValue):value(initValue){}

我可以看到它的作用,但是......为什么?为什么用这种方式写呢,而不是像:C级{
私人:
int value;
公开:
C (int initValue){
value = initValue;
}


第一个片段看起来像是一些继承东西,但很明显它是''初始化私人成员。那么为什么
使用了这个语法,并且它与第二个片段的质量方面有什么不同呢?

I''m reading a STL book right now, and there I''ve
picked up the folowing syntax:

class C {
private:
int value;
public:
C(int initValue) : value(initValue) { }
}

I can see what it does, but...why? Why is it written
this way, and not like:

class C {
private:
int value;
public:
C(int initValue) {
value = initValue;
}
}

The first snippet looks like some inherit-stuff, but
obviously it''s initializing the private member. So why
is this syntax used, and is it at all different from
the second snippet quality-wise?




第一个是初始化,第二是任务。分配和

初始化不是一回事。


在第二个示例中,值首先默认为初始化,然后分配。

对于一个整数,这没有区别,因为默认初始化是一个

no-op。但对于一个课程,它可能不是。


class X()

{

X(); //默认初始化,昂贵

X(const X&)//复制初始化,昂贵

X& operator =(const X&); //赋值,昂贵

};


现在这段代码只进行复制初始化


class C

{

C(const X& xx):x(xx){}

X x;

};


但是这段代码默认初始化后再分配


class C

{

C(const X& xx){x = xx; }

X x;

};


与一个相比,这是两个昂贵​​的操作。


所以一般认为在构造函数中更喜欢初始化到

赋值是好的风格,即使它没有实际的区别,就像

的情况是int'的。


john



The first is initialization, the second is assignment. Assignment and
initialization are not the same thing.

In the second example value is first default initalized and then assigned.
For an integer this make no difference because default initialization is a
no-op. But for a class it might not be.

class X()
{
X(); // default initialization, expensive
X(const X&) // copy initialization, expensive
X& operator=(const X&); // assignment, expensive
};

Now this code only does copy initialization

class C
{
C(const X& xx) : x(xx) {}
X x;
};

But this code does default initialization followed by assignment

class C
{
C(const X& xx) { x = xx; }
X x;
};

That''s two expensive operations compared to one.

So it''s generally thought to be good style to prefer initialzation to
assignment in constructors, even when it makes no actual difference, as is
the case with int''s.

john


这篇关于关于某事的两个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆