C++ Revisited

C++ Revisited

I have learnd C++ before and I know C++.But I cannot write any piece of C++ program without compiler's complaints. Itis the time to re-visit it.

I take it as a new language though I knowsomehting about it, though I am quite familiar with C and Java. It is a newlanguage.

Here is what I get when learning C++ andthey are worth referencing, I think, when learning C++.

  1. C++ is a new language for you, once you are determined to learn it, though you might know it, though you are familiar with other programming languges like C, C# or Java.
  2. C++ is a superset of C. But please forget C when learning and using C++. You know, it is a new languages(if Bjarne Stroustrup name it as Z++ or X++, many people won't connect C with it anymore).
  3. Keep in mind that reference is an aliase to a variable, but not the variable, As a result, whenever you see a reference, you should ask yourself, what and where is the variable? Is there another aliase?
  4. Reference cannot be applied to literal constants

int &a = 4; // Error

  1. For object arguments pass-by-reference; for built-in types, pass-by-value.

Pass-by-value would invoke copy constructorand assignment operator which is costly for objects. For built-in types, copyand assign would cost too much and most times, people like passing literalconstants which would cause error if argument is pass-by-value.

void Grade::setScore( const float &sc); math.setScore( 89.5f ); // Error setScoreuse pass-by-reference.

  1. Return object as possible as you can instead of returning reference or pointers.

Return reference might cause fatal errors,you might return a reference to local variables.

Returning pointers might cause memory leak.

Return objects is much safer thoughinvoking copy constructor.

  1. Please read <Effective C++> at least twice before you write formal C++ codes.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值