C++基础知识拾遗

 

一些基础的知识很容易忘记,so记下来以备查询。。。

 

Variable 变量

lvalue (pronounced "ell-value"): An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

rvalue (pronounced "are-value"): An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned.

 

Object 对象

An object is a region of memory that has a type. More specifically, evaluating an expression that is an lvalue yields an object.

 

Variable Initialization: 变量初始化

copy-initialization syntax uses the equal (=) symbol.

direct-initialization places the initialize in parentheses.

     int ival(1024);     // direct-initialization --copy constructor
     int ival = 1024;    // copy-initialization   --copy constructor
     int ival;
     ival = 1024;                                 --assignment operator
 

Initialization happens when a variable is created and gives that variable its initial value. Assignment involves obliterating an object's current value and replacing that value with a new one.

As for copy constructor, they are shallow copy and deep copy.

Shallow copy only copies the binary.

Deep copy ‘dive into’ the members, and copy their logical data. (We must implement copy constructor for the class include pointer member variables.)

 

Const Qualifier: const 限定符

Const qualifier transforms an object into a constant. It can Not be modifiable and be initialized when it is defined.

const std::string hi = "hello!"; // ok: initialized

Const objects are local to a file by default. To make a const variable accessible to other files we must explicitly specify that it is extern.

  // file_1.cc
  // defines and initializes a const that is accessible to other files
  extern const int bufSize = fcn();
  // file_2.cc
  extern const int bufSize; // uses bufSize from file_1
                           // uses bufSize defined in file_1

Const in class

The const variable not static must be initialized in constructor.

The static const variable must be initialized when it is defined.

 

Reference: 引用

A reference is an alias. Just another name for the object to which it is bound.

A reference must be initialized using an object of the same type as the reference.

      int ival = 1024;
      int &refVal = ival; // ok: refVal refers to ival
      int &refVal2;       // error: a reference must be initialized
      int &refVal3 = 10;  // error: initializer must be an object

A const reference is a reference that may refer to a const object:

  const int ival = 1024;
  const int &refVal = ival;      // ok: both reference and object are const

 参数传递准则:

            class A {};

            当以A作为参数传递时, A& 表示传值,而const A&表示const reference。对于传值方式需要调用ctor/dtor,而采用const reference传递,仅仅需要将地址压栈。

 

Dynamically allocate memory. (new and delete)

  
New operator = operator new + ctor
Delete operator = operator delete + dtor
 
   int i(1024);              // value of i is 1024
   int *pi = new int(1024);  // object to which pi points is 1024
   string s(10, '9');                   // value of s is "9999999999"
   string *ps = new string(10, '9');    // *ps is "9999999999"
   
   string *psa = new string[10]; // array of 10 empty strings
   调用operator new[]分配10string对象的内存,然后对每个元素调用string的default ctor
   int *pia = new int[10];       // array of 10 uninitialized ints
   int *pia2 = new int[10] ();   // array of 10 uninitialized ints

       delete[] pia;

 

new分配失败,在新的编译器下会抛出异常std::bad_alloc

当然,我们也可以用在新的编译器下不抛出异常而返回0,需要加上nothrow.

int* pi = new(nothrow) int[1024*1024*510];

 

其实,更好的方法是使用set_new_handler()。它的函数原型如下:  

new_handler set_new_handler( new_handler _Pnew) throw( );

 

VS2008 EX:

int nomemory(size_t bb)
{
	cerr << "baby, no memory...." << endl;
	return 0;
}

int main()   
{
	_set_new_handler(nomemory);
	int* pint = new(nothrow) int[1024*1024*510];

 

Inline Functions

inline functions avoid function call overhead. We can define one function as inline function by specifying the keyword inline before the function's return type.

Attentions:

1. The inline specification is only a request to the compiler. The compiler may choose to ignore this request. In general, the inline mechanism is meant to optimize small, straight-line functions that are called frequently. Many compilers will not inline a recursive function.

2. Unlike other function definitions, inlines should be defined in header files.

 

Desconstructor

当我们规定类只能在堆上分配内存时,就可以将析构函数声明为私有的。

class Demo

{

public:

    Demo():

private:

   ~Demo();

};

如果在栈上分配空间,类在离开作用域时会调用析构函数释放空间,此时无法调用私有的析构函数。

如果在堆上分配空间,只有在delete时才会调用析构函数。可以添加一个destroy()函数来释放,从而解决不能在析构函数中添加delete的问题

 

 

To be continue....

 

一些非常有用的链接:

C++ FAQ: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.26 

 

参考文献:

C++ primer (4 edition)

http://blog.csdn.net/jia_xiaoxin/article/details/3348045

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值