“const”是指“const”。修改 [英] The "const" modifier

查看:62
本文介绍了“const”是指“const”。修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用const的意义究竟是什么?一个函数之前的修饰符

声明?实际上,你可以在三个地方使用它。例如,请看

查看以下函数声明(来自介绍性书籍

Absolute C ++,作者:W。Savitch,第315页)


class Money

{

Money();

const Money运算符+(const Money& amount2)const;

私人:

int美元;

int美分;

}


我没有复制整个代码(还有更多的成员函数),但

这应该足以满足这个问题了。


const的含义在参数amount2之前对我来说很明显;

const就在分号发出信号之前,如果我做对了,那么函数可能不会改变调用对象。但是第一个

" const"


干杯,

马蒂亚斯


-

发送电子邮件至Anweisung in der Adresse befolgen

What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn''t copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?

Cheers,
Matthias

--
Für emails Anweisung in der Adresse befolgen

推荐答案

Der Andere写道:
Der Andere wrote:
使用const的意义究竟是什么?一个函数
声明之前的修饰符?实际上,你可以在三个地方使用它。例如,看看下面的函数声明(来自W. Savitch的介绍书
Absolute C ++,第315页)

类金钱
金钱();
const金钱算子+(const Money& amount2)const;
私人:
int dollar;
int cents;
}

我没有复制整个代码(还有更多的成员函数),但
这应该足以满足这个问题了。
const的含义在参数amount2之前对我来说很明显;
const就在分号发出信号之前,如果我做对了,函数可能不会改变调用对象。但是第一个
const怎么样?

干杯,
马蒂亚斯

-
Für发送电子邮件给Anweisung in der Adresse befolgen
What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn''t copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?

Cheers,
Matthias

--
Für emails Anweisung in der Adresse befolgen




这些问题在C ++ FAQ中讨论:
http://www.parashift.com/c++-faq-lit....html#faq-29.6
http://www.parashift .com / c ++ - faq-lit ... rrectness.html

在发布前搜索常见问题总是一个好主意。

一个也应该搜索新闻组。


-

托马斯马修斯


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome。 txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
< a rel =nofollowhref =http://www.josuttis.comtarget =_ blank> http://www.josuttis.com - C ++ STL图书馆书籍
http://www.sgi.com/tech/stl - 标准模板库



These issues are discussed in the C++ FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-29.6
http://www.parashift.com/c++-faq-lit...rrectness.html

Searching the FAQ before posting is always an excellent idea.
One should also search the newsgroups too.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


Der Andere写道:
Der Andere wrote:
使用const的意义究竟是什么?一个函数
声明之前的修饰符?实际上,你可以在三个地方使用它。例如,看看下面的函数声明(来自W. Savitch的介绍书
Absolute C ++,第315页)

类金钱
金钱();
const金钱算子+(const Money& amount2)const;
私人:
int dollar;
int cents;
}

我没有复制整个代码(还有更多的成员函数),但
这应该足以满足这个问题了。
const的含义在参数amount2之前对我来说很明显;
const就在分号发出信号之前,如果我做对了,函数可能不会改变调用对象。但是第一个
const呢?
What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn''t copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?




这意味着返回的临时对象是const。这一段时间我发布了一个关于

的帖子:

http://groups.google.com/groups?thre...733r%404ax.com


很遗憾它并没有产生太多的讨论。那个帖子是由GotW#6提示的
,这里:

http://www.gotw.ca/gotw/006.htm


这可能对您有所帮助。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

联系我请使用最近发布的地址。



It means the returned temporary object is const. I posted a thread about
this a while back:

http://groups.google.com/groups?thre...733r%404ax.com

Unfortunately it didn''t generate much discussion. That thread was
prompted by GotW #6, here:

http://www.gotw.ca/gotw/006.htm

Which may be enlightening for you.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Thomas Matthews< Th ********************* ****@sbcglobal.net>写道:
Thomas Matthews <Th*************************@sbcglobal.net> wrote:
Der Andere写道:
Der Andere wrote:
使用const的意义究竟是什么?一个函数
声明之前的修饰符?实际上,你可以在三个地方使用它。例如,看看下面的函数声明(来自W. Savitch的介绍书
Absolute C ++,第315页)

类金钱
金钱();
const金钱算子+(const Money& amount2)const;
私人:
int dollar;
int cents;
}

我没有复制整个代码(还有更多的成员函数),但
这应该足以满足这个问题了。
const的含义在参数amount2之前对我来说很明显;
const就在分号发出信号之前,如果我做对了,函数可能不会改变调用对象。但是第一个
const呢?
What exactly is the sense of using a "const" modifier before a function
declaration? Actually, you can use it in three places. For instance, take a
look at the following function declaration (from the introductory book
"Absolute C++" by W. Savitch, p. 315)

class Money
{
Money();
const Money operator + (const Money& amount2) const;
private:
int dollars;
int cents;
}

I didn''t copy the whole code (there are many more member functions), but
this should suffice for the question.

The meaning of the "const" before the parameter "amount2" is obvious to me;
the "const" just before the semicolon signals that the function may not
change the calling object if I got it right. But what about the first
"const"?



这些问题在C ++ FAQ中讨论过:
http://www.parashift.com/c++-faq-lit....html# faq-29.6
http ://www.parashift.com/c++-faq-lit...rrectness.html

在发布前搜索常见问题总是一个好主意。
还应该也可以搜索新闻组。



These issues are discussed in the C++ FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-29.6
http://www.parashift.com/c++-faq-lit...rrectness.html

Searching the FAQ before posting is always an excellent idea.
One should also search the newsgroups too.




我在FAQ中没有看到他的问题在哪里被回答...



I don''t see where his question is answered in the FAQ...


这篇关于“const”是指“const”。修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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