Inside the C++ Object Model - 2. The Semantics of Contructors

1. Default Constructor

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). 

Default constuctor is created by user or by compiler when it is needed.

In 4 situations default constructor is nontrivial:

  1. Member class object with default constructor
    1. compiler will create a default constructor for class if its member object has a default constructor
    2. only when the costructor is really needed
    3. to avoid to create more than one default constructors in difference compile module, compiler will make it an inline default destructor, even an explicit non-inline static one
    4. compiler will expand user default constructor, invoke necessary default constructors of member class object before user codes
  2. Base class with default constructor
  3. Class with virtual functions
    1. vtbl will be created by compiler
    2. vptr will be created by compiler
  4. Class with virtual base classes
class X {public: int i;};
class A: public virtual X { public: int j;};
class B: public virtual X { public: int k;};
class C: piblic A, public B { public: int l;};


// cannot get certain address of pa->i when compile time, 
// so it will be changed to:
// pa->__vbxX->i = 1024
void foo(const A* pa) { pa->i = 1024;}

int main()
{
    foo (new A);
    foo (new C);
}

2. Copy Constructor

In 3 situations:

  1. initialize an object using another object explicitly
  2. as an argument passed into function
  3. as a return value
  • Use default memberwise initialization recursively
  • Use bitwise copy semantics

 

Reset virtual table and virtual table pointer

  • it is safe if A class object is initialized by another A class object - copying vptr is safe
  • it is safe if base class object is initialized by a derived class object - vptr is reset

If virtual base class object exists - bitwise copy will break the the address arrangement. 

3. Program Transformation Semantics

  • explicit initialization - code will be expand, define object and then call copy constructor
  • argument initialization
  • return value initialization - code will be expand, a return object reference will be added as argument and copy constructor will be invoked

Optimization:

  • Optimization at user level - avoid trivial calls of copy constructor to improve efficiency
  • Optimization at compiler level - NRV: Named Return Value

Is an explicit copy constructor needed?

no reason to define an explicit constructor if 

  1. bitwise copy of member data is quite safe
  2. no virtual function found
  3. no virtual base class found 

and compiler will do the best for you.

If there is huge amouts of memerwise copy needed, then an explicit inline copy constructor is needed.

4. Member Initialization List

You have to use member initialization list when:

  1. initializing a reference member
  2. initializing a const member
  3. invoking a base class object constructor with arguments
  4. invoking a member class object constructor with arguments

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值