Interviews, and a C ++ class structure [correlation function, the copy constructor, destructor, friend]

Constructor: initialization value can be parameters and returns no value, reloadable, there may be a plurality of

Destructor: release the object memory, no parameters and returns no value should not be overloaded, only the presence of a

The copy constructor: copy of the object, its shape must be reference


1. What are the default empty class will add something? how to write? Empty class size is how much? why?

1) Empty (): default constructor

2) Empty (const Empty &) : copy constructor

3) ~ Empty (): destructor

4) Empty & operate = (const Empty &): assignment operator

Empty class size is 1 , because C ++ required for each instance of the class must have a unique address , the compiler automatically assigned a byte size of empty class, thus ensuring that each instance has a unique memory address


2. The order of execution of the constructor? Execution order destructors

Constructor:

1) the parent class constructor , if there are multiple parent classes, the order of these calls parent class constructor order they appear in the parent class subclasses

2) members of the class object constructor , if multiple member classes, the order of construction of these calls are members of a class is declared as a sequential member of the class in the class

3) subclass constructor

Destructor:

1) sub-class destructor

2) destructor member of a class object

3) destructor parent class


3. constructors and destructors may throw an exception it?

Not recommended Throws in the constructor, not throw exceptions destructor!

1) throw an exception in the constructor, will lead to the destructor is not executed , cause a memory leak, you need to manually release the memory or to use auto_ptr smart pointer: constructor throws an exception because the default constructor is not finished, destructor will not be called, so that a memory leak

2) throw an exception in the destructor, exception handling is calling the destructor of the exception object, it will fall into infinite recursion , it is necessary to abnormal encapsulated in the destructor , rather than an exception to be thrown


4. Class members are initialized? Which faster? why?

1) Assignment Initialization : do the assignment in the constructor, assignment operator is then allocated after the data members good memory space

2) a list of initialization : use initialization list after the colon, it is initialized when the data members allocated memory space

Faster initialization list : assignment will produce a temporary object , the object appears temporarily reduce the efficiency program


5. How do you stop the compiler automatically generates a copy constructor and assignment functions and to avoid being called?

1) The copy constructor and assignment functions manually override and arranged privte, and will not be declared only achieved

Manually override the default to avoid compiler generates, to avoid being called external private class or subclass, can be produced only declaration does not implement connection error when the member functions of this class and calls the friend function

2) inherited class Uncopyable

Copy constructor and assignment operator Uncopyable classes are private, not called by subclasses, which can prevent the copying objects and the assignment

3) manually override deleted and the functions defined functions : deleted function calls the function to prohibit


6.什么情况下会生成默认构造函数

1)类成员含有构造函数时:为了能够调用类成员的构造函数,所以本类必须要有构造函数

2)父类中含有构造函数时:为了能够调用父类的构造函数,所以子类必须有构造函数才能调用父类的构造函数,才能初始化父类成员

3)类中含有虚函数时:对象的虚函数表指针需要通过构造函数进行初始化

4)虚继承时:指向虚基类的指针要在构造函数中被初始化

构造函数只有在被需要时才会自动生成!

编译器生成构造函数和拷贝构造函数的情况是一样的,也就是说编译器合成拷贝构造函数也是在上面四种情况下


7.类的析构函数什么时候会被调用?

1)对象生命周期结束时

2)delete指向对象的指针时,或delete指向对象的基类类型指针,而基类析构函数是虚函数时

3)对象A是对象B的成员,B的析构被调用时,A的析构函数也会被调用


8.为什么友元函数必须在类的内部进行声明?

因为编译器在编译类的时候就必须知道谁可以访问类的私有部分!所以友元函数必须在类的内部进行声明,可以在类的外部进行定义!

Guess you like

Origin www.cnblogs.com/yinbiao/p/10990796.html