Skip to content

Latest commit

 

History

History
134 lines (95 loc) · 6.07 KB

Lecture-6.md

File metadata and controls

134 lines (95 loc) · 6.07 KB

Lecture 6

Reference

1. Please explain the basic concept of reference(引用).

  • 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, and C& 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.

3. What are the advantages of reference compared with pointer?

  • 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.

5. How to apply references as member variables? Please give a complete example.

  • 引用类型数据成员需要在初始化列表中初始化。
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.

7. Please list some examples of returning multiple values simultaneously from functions.

  • 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.

9. Please explain the least privilege principle preferably with an example.

  • 只给函数完成自身任务的权限。如对于不会修改变量内容的函数,在其后加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.

Copy constructor

11. Please give an example of copy constructor.

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);
}

12. How is reference associated with the C++ class grammar?

  • 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.

15. Why should we avoid copy constructors? How to ensure that copy constructor not be called?

  • Low-efficiency and error-prone.
  • The simplest way to disable copy constructors is to put them in private zone.

16. Please state the differences between move constructors and copy constructors.

  • 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