C ++陷阱:const对象不常表现。 [英] C++ Pitfall: const objects do not behave constantly.

查看:46
本文介绍了C ++陷阱:const对象不常表现。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认为const对象也会有不变的行为是错误的。


考虑下面的代码片段:


class Person

{

public:

Person();


string get_name()const

{

随机发生器骰子; // Randomizer是一些返回随机字符串的类。

返回dice.getRandomString();

}


void set_name( const string& name);

};


void func(const Person& person)

{

//请注意,对象''person''被声明为const,因此它是不可变的。


//但是,以下两行可以打印不同的名称,

//即使我们正在调用const成员函数''getName''。


//在这里打印一个名字,

cout<< person.get_name()<< endl;


//但它可以在这里打印一个不同的名字。

cout<< person.get_name()<<结束;

}


因此,对象行为不保证对于const对象

是恒定的,即使它的状态不变。


这也意味着编译器无法使用const对象进行优化。


所以对我来说,声明一个对象''const''是实际上只是一种机制来支持
允许对象的成员函数的某个子集被调用

(那些被声明为''const'')并禁止调用另一个成员

函数,(那些未声明为''const''的函数)。没有其他条件

可以假设。


-


-kira

It is erroneous to think that const objects will have constant behaviors too.

Consider the following snip of code:

class Person
{
public:
Person();

string get_name() const
{
Randomizer dice; // Randomizer is some class that returns random strings.
return dice.getRandomString();
}

void set_name(const string &name);
};

void func(const Person &person)
{
// note that the object ''person'' is declared const, so it is immutable.

// However, the following two lines can print different names,
// even though we are invoking const member function ''getName''.

// print one name here,
cout << person.get_name() << endl;

// but it could print a different name here.
cout << person.get_name() << endl;
}

Thus, object behavior is not guarantee to be constant for const object
even though its state is unchanged.

This also means that compiler cannot make optimizations with const object.

So to me, declaring an object ''const'' is really just a mechanism to
allow a certain subset of the object''s member functions to be called
(those that are declared ''const'') and disallow calling the other member
functions, (those that are not declared ''const''). No other conditions
can be assumed.

--

-kira

推荐答案

Kira Yamato写道:
Kira Yamato wrote:

认为const对象具有常量是错误的

行为也是如此。
It is erroneous to think that const objects will have constant
behaviors too.



您对常量行为的定义是什么?

What is your definition of "constant behaviour"?


>

考虑以下代码片段:


class Person

{

public:

Person();


string get_name()const

{

Randomizer dice; // Randomizer是一些返回随机

字符串的类。 return dice.getRandomString();

}


void set_name(const string& name);

};


void func(const Person& person)

{

//注意对象''person''被声明为const ,所以它是

不可变。

//但是,以下两行可以打印不同的名字,

//即使我们正在调用const成员函数''getName''。


//在这里打印一个名字,

cout<< person.get_name()<< endl;


//但它可以在这里打印一个不同的名字。

cout<< person.get_name()<<结束;

}


因此,对象行为不保证对于const对象

是恒定的,即使它的状态不变。
>
Consider the following snip of code:

class Person
{
public:
Person();

string get_name() const
{
Randomizer dice; // Randomizer is some class that returns random
strings. return dice.getRandomString();
}

void set_name(const string &name);
};

void func(const Person &person)
{
// note that the object ''person'' is declared const, so it is
immutable.
// However, the following two lines can print different names,
// even though we are invoking const member function ''getName''.

// print one name here,
cout << person.get_name() << endl;

// but it could print a different name here.
cout << person.get_name() << endl;
}

Thus, object behavior is not guarantee to be constant for const object
even though its state is unchanged.



您是否认为有人可以将不变性与

重复性混淆?

Is it possible that you think somebody can confuse immutability with
repeatability?


这也意味着编译器无法使用const

对象进行优化。
This also means that compiler cannot make optimizations with const
object.



你如何实现这一飞跃?我们在说什么优化?这是

How do you make that leap? And what optimizations are we talking
here?


所以对我来说,声明一个对象''const''实际上只是一种机制

允许对象的成员函数的某个子集被调用

(那些被声明为''const'')并禁止调用另一个

成员函数,(未声明为''const''的成员函数)。没有其他

条件可以假定。
So to me, declaring an object ''const'' is really just a mechanism to
allow a certain subset of the object''s member functions to be called
(those that are declared ''const'') and disallow calling the other
member functions, (those that are not declared ''const''). No other
conditions can be assumed.



这几乎是''const''的实际定义。你有没有读过一些书或文章?然后请报价。


V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问

That''s pretty much the actual defintion of ''const''. Have you been
reading some book or article that says otherwise? Then please quote.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On 2007-09-25 17:00:16 -0400,Victor Bazarov < v。******** @ comAcast.netsaid:
On 2007-09-25 17:00:16 -0400, "Victor Bazarov" <v.********@comAcast.netsaid:

Kira Yamato写道:
Kira Yamato wrote:

>认为const对象也具有持续的行为是错误的。
>It is erroneous to think that const objects will have constant
behaviors too.



您对常量行为的定义是什么?


What is your definition of "constant behaviour"?



我当时把它想象成一台状态机。如果状态保持相同,那么相等的输入应该产生相等的输出。


在我的例子中,我期待两个调用
get_name()返回相同的内容。

I was thinking of it as a state machine. If the state remains the
same, then equal inputs should produce equal outputs.

In my example, I was expecting the two instances of invocations of
get_name() to return the same thing.


>
>

>>
请考虑以下代码片段:

类人员
{
公众:
人();

字符串get_name()const
{随机函数骰子; // Randomizer是一些返回随机
字符串的类。 return dice.getRandomString();
}
void set_name(const string& name);
};

void func(const Person& ;人)
//
//注意对象''person''被声明为const,因此它是不可变的。
//但是,以下两行可以打印不同的名字,
//即使我们正在调用const成员函数''getName''。

//在这里打印一个名字,
cout<< person.get_name()<< endl;

//但它可以在这里打印一个不同的名字。
cout<< person.get_name()<<因此,对象行为不保证对于const对象来说是恒定的,即使它的状态没有改变。
>>
Consider the following snip of code:

class Person
{
public:
Person();

string get_name() const
{
Randomizer dice; // Randomizer is some class that returns random
strings. return dice.getRandomString();
}

void set_name(const string &name);
};

void func(const Person &person)
{
// note that the object ''person'' is declared const, so it is
immutable.
// However, the following two lines can print different names,
// even though we are invoking const member function ''getName''.

// print one name here,
cout << person.get_name() << endl;

// but it could print a different name here.
cout << person.get_name() << endl;
}

Thus, object behavior is not guarantee to be constant for const object
even though its state is unchanged.



您是否有可能认为有人可以将不变性与

重复性混淆?


Is it possible that you think somebody can confuse immutability with
repeatability?



显然是这样。我在想什么*关注*使用关键字

''const''是什么。我认为这样做是为了让事情宣布

''const''应该产生可重复的结果。

Apparently so. I was thinking what the *intented* use of the keyword
''const'' is. I thought it was intented to be so that things declared
''const'' should yield repeatable results.


>
>

>这也意味着编译器无法使用const
对象进行优化。
>This also means that compiler cannot make optimizations with const
object.



你如何实现这一飞跃?我们在说什么优化



How do you make that leap? And what optimizations are we talking
here?



例如,如果它确实被视为状态机(因此

不变状态应该从''const'产生未改变的响应'

函数),然后编译器可以删除第二次调用

get_name()并使用第一次调用的结果。

For example, if indeed it were treated like a state machine (so that
unchanged states should yield unchanged responses from ''const''
functions), then the compiler can remove the 2nd invocation of
get_name() and just use the result from the first invocation.


>
>

>所以对我来说,声明一个对象''const''实际上只是一种机制来允许对象的某个子集''要调用的成员函数
(那些被声明为''const''的函数)并禁止调用其他成员函数(那些未声明为''const''的函数)。没有其他的条件可以假设。
>So to me, declaring an object ''const'' is really just a mechanism to
allow a certain subset of the object''s member functions to be called
(those that are declared ''const'') and disallow calling the other
member functions, (those that are not declared ''const''). No other
conditions can be assumed.



这几乎是''const''的实际定义。你有没有读过一些书或文章?那么请引用。


That''s pretty much the actual defintion of ''const''. Have you been
reading some book or article that says otherwise? Then please quote.



无处可说。我正在学习C ++,主要是通过其他人的代码来学习C ++。所以,我大多只是猜测

语言的每个部分正在做什么,并通过试验和错误来学习。


所以,我想在这种情况通过关键字

''const''来创造这种机制是一个糟糕的选择,即使这是它找到最多的b $ b应用程序。但是,关键字应该是选择它实际上是什么,而不是它想要做的事情。在

''const''的情况下,这两件事显然不是一回事。

Nowhere says that. I''m learning C++ mostly by examples from other
people''s code. So, I''m mostly just guessing what each part of the
language is doing, and learning by trials and errors.

So, I suppose in this case coining this mechanism by the keyword
''const'' is a poor choice, even though that is where it finds most
applications in. However, keywords should be chosen for what it
actually does instead of what it is intended to do. In the case of
''const'', these two things are clearly not the same thing.


>

V
>
V



-


-kira

--

-kira


在2007-09-25 22:37,Kira Yamato写道:
On 2007-09-25 22:37, Kira Yamato wrote:

认为const对象也会有不变的行为是错误的。


考虑以下代码片段:
It is erroneous to think that const objects will have constant behaviors too.

Consider the following snip of code:



发布时请考虑用几个空格替换标签。

Please consider replacing tabs with a few spaces when posting.


class Person

{

public:

Person();


string get_name()const

{

Randomizer dice; // Randomizer是一些返回随机字符串的类。

返回dice.getRandomString();

}


void set_name( const string& name);

};


void func(const Person& person)

{

//请注意,对象''person''被声明为const,因此它是不可变的。


//但是,以下两行可以打印不同的名称,

//即使我们正在调用const成员函数''getName''。


//在这里打印一个名字,

cout<< person.get_name()<< endl;


//但它可以在这里打印一个不同的名字。

cout<< person.get_name()<<结束;

}


因此,对象行为不保证对于const对象

是恒定的,即使它的状态不变。


这也意味着编译器无法使用const对象进行优化。


所以对我来说,声明一个对象''const''是实际上只是一种机制来支持
允许对象的成员函数的某个子集被调用

(那些被声明为''const'')并禁止调用另一个成员

函数,(那些未声明为''const''的函数)。没有其他条件

可以假设。
class Person
{
public:
Person();

string get_name() const
{
Randomizer dice; // Randomizer is some class that returns random strings.
return dice.getRandomString();
}

void set_name(const string &name);
};

void func(const Person &person)
{
// note that the object ''person'' is declared const, so it is immutable.

// However, the following two lines can print different names,
// even though we are invoking const member function ''getName''.

// print one name here,
cout << person.get_name() << endl;

// but it could print a different name here.
cout << person.get_name() << endl;
}

Thus, object behavior is not guarantee to be constant for const object
even though its state is unchanged.

This also means that compiler cannot make optimizations with const object.

So to me, declaring an object ''const'' is really just a mechanism to
allow a certain subset of the object''s member functions to be called
(those that are declared ''const'') and disallow calling the other member
functions, (those that are not declared ''const''). No other conditions
can be assumed.



我几乎不会把这称为陷阱,我还没有给任何人带来一些OO编程技巧的人没有把握常量

对象的概念(即它们的状态是恒定/不可更改的)。个人

我认为const概念是一个非常强大的概念,特别是当

参与函数的参数时。作为一个例子,当我作为程序员使用

a第三方API时,我知道我可以安全地将任何字符串传递给

函数,并将const字符串引用作为参数,由于函数不会更改字符串

。如果没有const我就不会有那个

保证而我必须复制并传递它才能安全。


-

Erik Wikstr ?? m

I would hardly call this a pitfall, and I have yet to meat anyone with a
bit of OO programming skill who fail to grasp the concept of constant
objects (namely that their state are constant/unchangeable). Personally
I think that the const concept is a really powerful one, especially when
parring arguments to functions. As an example when I as a programmer use
a third party API I know that I can safely pass any string I have to a
function taking a const string reference as parameter, since the string
will not be changed by the function. Without const I would not have that
guarantee and I would have to make a copy and pass that to be safe.

--
Erik Wikstr??m


这篇关于C ++陷阱:const对象不常表现。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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