Sample for effective C++

#include <string>
#include <list>
#include <iostream>

#define MAX 100       // Item 2: Prefer consts, enums, and inlines to #defines

class Shape{
public:
Shape(){}
~Shape(){}       // Item 7: Declare destructors virtual in polymorphic base classes
virtual void Draw();
// ...
};

class Point: public Shape{
public:
Point() {}       // Item 4: Make sure that objects are initialized before they're used. Comment: So we must initialize the members in the constructor.
Point(int _x, int _y) {
   x = _x;
   y = _y;
}
~Point(){ std::cout<< "x :=" << x << "y:=" << y << std::endl; };
virtual void Draw() {
   // ...
}
private:
int x;
int y;
// ....
};

class Picture {
public:
Picture( std::string name,   // const std::string& name, comment: user defined object. ( passed by value --> passed by reference ), use "const" if it won't be changed
   std::list<Shape*> pic );
private:
std::string theName;
std::list<Shape*> thePic;
};

Picture::Picture( std::string name,   // Item 4: Make sure that objects are initialized before they're used. Comment: Use the member initialization list instead of the assignments.
                 std::list<Shape*> pic)   // :theName( name ), thePic( pic )
{
theName = name;
thePic = pic;        // ISSUE: Comment : the list store the pointers.
}

int main()
{
Picture A( "string",pic );    // "string" to std::string( "string" ) is implicit type conversion.
            // There is one way to prevent the implicit type conversion. Declare the constructors explicit. Refer exp2.


Picture B = A;       // Item 5: Know what functions C++ silently writes and calls.
}


// exp2
//class C {
//public:
// explicit C(int x){};                       // not a default constructor
//};
//void fun( C c )
//{
// // ...
//}
//fun( 1 ); // error.
//fun( C(1) ); // work.

// exp3
//class Empty{};
//
it's essentially the same as if you'd written this:
//
//class Empty {
//public:
// Empty() { ... }                            // default constructor
// Empty(const Empty& rhs) { ... }            // copy constructor
// ~Empty() { ... }                           // destructor — see below
// Empty& operator=(const Empty& rhs) { ... } // copy assignment operator
//};
// These functions are generated only if they are invoked. Like
// Empty e1;                               // default constructor and destructor
// Empty e2(e1);   or   Empty e2 = e1      // copy constructor
// e2 = e1;                                // copy assignment operator

// Item 6: Explicitly disallow the use of compiler-generated functions you do not want
// Solution: declare the corresponding member functions private and give no implementations. or

class HomeForSale {

public:

...

private:

...

HomeForSale(const HomeForSale&);            // declarations only

HomeForSale& operator=(const HomeForSale&);

};


//class Uncopyable {
//protected:                                   // allow construction
// Uncopyable() {}                            // and destruction of
// ~Uncopyable() {}                           // derived objects...
//private:
// Uncopyable(const Uncopyable&);             // ...but prevent copying
// Uncopyable& operator=(const Uncopyable&);
//};
//
//class HomeForSale: private Uncopyable {     // class no longer
// ...                                       // declares copy ctor or
//};                                          // copy assign. operator

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值