- Reference is a const pointer that is automatically deferenced. And it needs to be defined and initialized simultaneously. And once defined, it cannot be changed to refer to other variables.
2. Is it legal to declare a reference variable without defining it? Is it legal to change the value of a reference variable(改变引用变量的值)? Please explain in detail with examples.
- No, because references are like constant pointer. If not defined at creation the whole thing makes no point.
- Reference variables’ states can be changed (for example,
int a; int &b = a; b = 2;
would change a’s value, andC& b = a; b.i = 2;
would change a’s members), but the reference (which is to say, the instance the pointer refers to) cannot be changed.
- The reference is an alias of a given object/variable. It’s more like a constant pointer, so it’s much safer. No NULL references are allowed (but NULL pointers are allowed), so reference is safer.
4. It is possible to return a reference of a local variable of a function? Why? What are the advantages of returning references rather than values in functions?
-
No. The local variable will be destroyed. The reference will point to illegal space.The returned reference can be used as lvalue.
-
Avoid making copies of function arguments and/or return value for better efficiency.
- 引用类型数据成员需要在初始化列表中初始化。
class A
{
int &a;
public:
A(int &p):a(p){}
void modify() {a = 0;}
};
6. It is possible to use reference variables as elements in std containers such as std::vector? And how about pointers?
- No, because containers store objects and references are not objects. (However, wrappers can be used to fulfil the same effect.) Pointers can be stored finely.
- Use reference to carry back the value.
To compute the min, max, and average values simultaneously.float average (int arr[], const int& n, int& min, int& max);
8. What are the differences between passing references (引用传参) and passing values (值传参) in function arguments?
-
Passing reference is equal to passing constant pointer. It avoids copy to arguments.
Passing values will first call copy constructor of arguments. It consumes time and resource. -
passing values in function arguments will not change the original one but just use it values.
passing references in function so that you can changed the value of the argument and save it. -
passing references don’t call copy-constructor and the passing values automatically call the copy-constructor and make a copy for use.
- 只给函数完成自身任务的权限。如对于不会修改变量内容的函数,在其后加
const
。
10. What is the difference between rvalue references and lvalue references? Please state the basic concept and usage of rvalue references.
- Differences:
- Rvalue references use the value of the reference, but lvalue references use the address of the reference.
- Rvalue references can refer to addresses in the register, but lvalue references must refer to real objects.
- Rvalue references:
- Basic concept: Refer to an address in the register in order to directly modify the data in the register, not bothering to create a temporary object.
- Usage: use
&&
instead of&
, and do not declare the name of the reference variable.
class MyString{
char* data;
......
public:
MyString(const MyString& _string);
int length();
......
}
MyString::MyString(const MyString& _string){
data = new char[strlen(_string.data)+1];
strcpy(data,_string.data);
}
- Reference is necessary for copy constructor.
- Actually, reference is a must for copy constructor, rather than a need. The object can't be passed into the constructor by value because you're trying to define the function that handles passing by value.
- Reference is often used with operator overload.
13. Please list the cases where copy constructors are called. How to avoid the copy constructor in the listed cases?
- 新对象创建和参数传递时会调用拷贝构造函数。
- 避免方法:传参时加引用,将拷贝构造函数定义为private,将拷贝构造函数定义为delete。
14. What’s the function of compiler-generated copy constructor? What is the fatal disadvantage of depending on the compiler-generated copy constructor? Please give an example of the fatal error.
- The copy constructor can be automatically generated by the constructor.
- It only does shallow copy, which means, the constructor copies the memory blocks of the object. It can severely breaks when the object store pointers or references because of shallow copying. Example is the following:
class C{
D *d;
~C() { delete d; }
};
// a is of type C
C b = a;
//...
When the objects go out of scope, the pointer stored in a is actually deleted twice, resulting in segmentation fault.
- Low-efficiency and error-prone.
- The simplest way to disable copy constructors is to put them in private zone.
- move constructor is called when a rvalue is passed into parameter.
move constructor is able to use the resource of temporary rvalue.
move constructors is used when new objects are being defined by rvalue reference. - copy constructor is called when a lvalue is passed into paramenter.
copy constructors is used when new objects are being defined by an existing object of the same class