Monday, September 23, 2013

nuts and bolts & small dark corners of C++

L-Value: the value at the left side of the operator An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address).
R-Value: the value at the right side of the operatorrvalues are defined by exclusion, by saying that every expression is either an lvalue or an rvalue

Remember prefix ++ return reference(L-Value), but postfix ++ return Type (R-value).

Left value cannot be temporary.

// i++ return an RValue, no identifiable memory address, cannot be increased any more.

i++ ++ ;//illegal

Right value can be temporary:

// ++i return a reference, a reference can be increased!

++ ++ i; // OK

But L-Value can be a reference returned by a function.
A function call can return an LValue. prefix ++ is the best example in my view.



--------------------------------------------------------

new operator ==> new keyword, what we normally use to create an object

operator new ==> malloc

char *x = static_cast<char *>(operator new(100));
--------------------------------------------------------
1. copy constructor ---> applied in situation when there is one existing object and you want to construct a new one based on that existing one

A_ a2=a1; // even if there is an assignment operator, it will call copy constructor!

2. assignment operator ---> as the name implies, the function of assignment operator is to assign, not construct. It is applied when there are two existing objects and you want to assign one to another
A_ a1, a2;
a1=a2;// assignment operator gets called!

3. Compiler will write constructor(default and copy) and assignment operator for you silently if you do not write it manually. You cannot define copy constructor only without defining default constructor, but you can define default constructor without defining copy constructor!

No comments: