References and Constants
Table of Contents
- What is a reference?
- A class object (i.e. not a built-in object) may be returned by reference or passed by reference for better efficiency.
- When used properly, returning by reference allows the function to appear on the left-hand side of an expression.
- Passing/returning by reference allows the referenced object to be modified.
- If the intention of the class designer is to avoid modification of the passed/returned object, the reference should be made const.
- If a member function will not modify its data members, make the member function const.
- It is illegal to modify any object or reference that is declared const. Therefore, constant objects and references may only call constant member functions.
What is a reference?
A reference is nothing more than an alias. Anything you do with a
reference you are actually doing to the object you are referencing.
float x=3.14; float& intref=x; // intref is a reference to x float* ptr=&intref; // ptr holds the address of x float y=2.58; intref=y; // x is now equal to 2.58
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)