003 Figuring in C/C++

1. Type Casting

/* dynamic_cast:
 Can be used only with pointers and references to class;
 Base-to-derived conversions are not allowed unless the base class is polymorphic;
 Can cast null pointers even between pointers to unrelated classes;
 Can also cast pointers of any type to void pointers (void*);
 If the cast failure it returns a null pointer or 
   an exception of type bad_cast will thrown if convert to an impossible reference type.
 Note:cast requires the Run-Time Type Information (RTTI) to keep track of dynamic types.
   Some compilers support this feature as an option which is disabled by default. */
/* static_cast
 Pointers/Referrence both Base <-> Derive, 
     but no safety check during runtime & no type-safety checks;
 Any other non-pointer conversion that could also be performed implicitly;
 Any conversion between classes with explicit constructors or operator functions; */
/* reinterpret_cast
 Converts any pointer type to any other pointer type (same to reference);
 The operation result is a simple binary copy of the value from one pointer to the other;
*/
/* const_cast
 Manipulates the constness of an object, either to be set or to be removed. 
 Only for pointer, reference, or a pointer-to-data-member type*/

2. Function with copy parameter & return

include <cstdio>

class B {
public:
	B():d(0){ printf("Construct B()\n"); }
	~B(){ printf("Destruct ~B()%d\n", d); }
	B(int i){ d=i; printf("Construct B(i)%d\n", d); }

private:
	int d;
};

B Play( B b ) {
	return b;
}

int main()
{
	Play( 11 );
	B b1 = Play(5);
	return 0;
}
Out:

Construct B(i)11
Destruct ~B()11
Destruct ~B()11
Construct B(i)5
Destruct ~B()5
Destruct ~B()5
函数返回若有左值,则赋值给左值,否则赋值给临时值。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值