C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

c++ effective心得

1.Operator overloading

 

一般的:  c& c::operator=(const c&){。。。。return *this;};   --------格式  运算符的重载
   w = x = y = z = "hello";//可以返回引用,效率高
   a+b+c+d     //必须返回类,而不是返回引用
   c c::operator=(const c &){ 。。。。。。return *this;};
  
Operators that can be overloaded
+, -, *, /, %, ^, &, +=,-=,*=,/=, (), [], >>, ==,=,->,++,--, ->*, … new, delete
Operators that can not be overloaded
., ::, ?:, .*
//Notes for ++, -- operator
区别:
 T& operator ++() //prefix ++a
 T operator++(int) //postfix a++


一般情况下几乎总要遵循operator=输入和返回的都是类对象的引用的原则.
必须返回一个对象时不要试图返回一个引用,当需要在返回引用和返回对象间做决定时,你的职责是选择可以完成正确功能的那个。至于怎么让这个选择所产生的代价尽可能的小,那是编译器的生产商去想的事。

2.
const成员函数不被允许修改它所在对象的任何一个数据成员。mutable在处理“bitwise-constness限制”问题时是一个很好的方案,但它被加入到c++标准中的时间不长,所以有的编译器可能还不支持它。
explicit表示必须显示的调用该函数。

3.void including more times
#findef
#define

#endif

等价于
#progma once


4  .struct
                             #pragma pack(pop, n)
struct Employee{
 char cName[5];
 short  nAge;
        float  dSalary;
 bool  bMarried;
};
sizeof(Employee) depends on struct member alignment. Default is 4 bytes alignment. sizeof(Employee) = 16
2 bytes data wants to start from even address, 4 bytes data wants to start from address that is 4 times, 1 byte data can start from any address
我们可以用以下改变默认的对齐方式。
#pragma pack(pop, n)

5。enum :It can define a set of const values.

6。In fact, C++ compiler creates a global function for each member function, and add another parameter whose type is our class. 一般位类名_函数名_参数列表

7。constructor   ※   deconstructor

Copy Constructor 
Copy constructor is a constructor that has a parameter whose type is class reference.
and the class reference is const.
eg:
CRectangle( const CRectangle& other)
eg:
   CComplex a;
   CComplex b=a; // Copy constructor is invoked
   b=a; //Assignment operator is invoked

Converting Constructor
一般定义位explicit函数,必须被显示的调用


8。const in class
  4 cases in a class
 const int member;只能在初始化列表里进行初始化
 int funct( const int nFactor,…);参数位常熟,在函数种不可被修改
 const int func(…); 返回的值位常熟不可以再被赋值和修改
 int func(…) const; 常函数,不可以修改所在类的成员,要修改的成员必须有mutable关键字


9。inline
You can see there is no function calling in assembly.
A function defined within a class definition is an inline function.
Although a function is inline, that whether to expend or not depends on compiler or its settings.一般函数里有循环的系统认为不内联
10.  Member-initializer-list
Const, reference and base class constructor (that has arguments) must be initialized or called in member-initializer-list
Members are initialized by order that they are defined in class definition, not by order their initializers are in member-initializer-list.
eg:
CCircle::CCircle() : PI(3.14159), m_dOrgX(0.0), m_dOrgY(0.0)
{
}
基类的初始化,按照子类定义时的顺序,不时初始化列表的顺序

11。 class Default functions
Constructor [do nothing]
Copy constructor [bitwise copy]
Destructor ( ~ ) [do nothing]
Assignment operator ( = ) [bitwise copy]
Address operator ( & )
Dereference operator ( * )

//当有指针是自己定义拷贝构造和赋值
eg:
        if(other.m_pBuffer!=NULL) {
 m_pBuffer=new TCHAR[tcslen(other.m_pBuffer)+1];
 tcscpy(m_pBuffer,other.m_pBuffer);}

12 this 指针
Point lower;
lower.setX(20).setY(30).doubleMe() ;
因为:
     Point& setX(int x) { _x = x; return *this;}  
     Point& setY(int y) { _y = y; return *this;}  
     void doubleMe()
 {  _x *= 2;
  _y *= 2;  
 }

13。    const   static     int PI=1;类中的成员直接赋值,只有这一种情况可以,必须为int.

        double CCircle::PI = 1.0;类中的静态成员初始化必须再类外,而且还的有类型, 如前面.
        静态成员的初始化不能再构造函数中进行!

14.  Dynamic_cast
The dynamic_cast is used for safe casting from a pointer to a base class to a pointer to a derived class, often referred to as safe down casting. It is used when one must use the features of the derived class that are not present in the base class.

typeid            typeid( object );
The typeid operator returns a reference to a type_info object that describes `object`.

If the expression is of class type and the class contains one or more virtual member functions, then the answer may be different than the type of the expression itself. For example, if the expression is a reference to a base class, the typeid operator indicates the derived class type of the underlying object.

PTTI运行时刻类型识别允许“用指向基类的指针或引用来操作对象”的程序能够获取到“这些指针或引用所指的对象”的实际派生类型。
1. Dynamic_cast它允许在运行时刻进行类型转换,从而使程序能够在一个类层次结构中安全地转换类型,把基类指针转化成派生类指针,或把指向基类的左值转换成派生类的引用,当然只有在保证转换能够成功的情况下可以。
2. typeid操作符,它指出指针或引用指向的对象的实际派生类型。它在程序中可以用于获取一个表达式是一个类类型,并且含有一个或多个虚函数成员,则答案会不同于表达式本身的类型。例如,如果表达式是一个基类的引用,则typeid会指出底层对象的派生类类型。


15. What exceptions can a function throw if it has an exception specification of the form throw()?  (6’) throw all kinds of exceptions
If  it has no exception specification?
What means the syntax: catch (…) catch all types of exceptions

posted on 2005-11-09 12:56 梦在天涯 阅读(1800) 评论(2)  编辑 收藏 引用 所属分类: CPlusPlus

评论

# re: c++ effective心得 2006-08-17 12:26 keyws

T& operator ++() //prefix ++a
T operator++(int) //postfix a++
怎么解释这个?即编译器如何决定调用哪个?

  回复  更多评论   

# re: c++ effective心得 2008-10-14 14:33 ggz

@keyws
一个是++a 一个是a++  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1785191
  • 排名 - 5

最新评论

阅读排行榜