Monday, May 19, 2014

CONST

CONST

  • const correctness : C++ compiler's detection of many unexpected changes to objects and flags these violations with error messages at compile time. All tests for constness are done at compile time. 
  • const doesnot imply runtime overhead. Neither runtime space nor speed is degraded. 
  • The purpose of const is correctness, not optimization. The const helps the compiler find bugs, but it does not (normally) help the compiler generate more efficient code. 
  • const correctness is no more tedious than declaring the type of a variable. 
  • The compiler won't allow a const member function to change *this or to invoke a non-const member function for this object. 
  • const should not be used for formal parameter types that are passed by value.
it is inappropriate to use Fred* const in a formal parameter list. do not use Fred& const in any context 
  • The mutable keyword tells the compiler that const member functions are allowed to change the data member.

initialization list


All other things being equal, your code will run faster if you use initialization lists rather than assignment
Why can't I initialize my static member data in my constructor's initialization list?
Because you must explicitly define your class's static data members.

Refrences

Refrences

  • An alias (an alternate name) for an object.
  • If we assign to a reference, we change the state of the referent (the referent is the object to which the reference refers).
  • If we return a reference, the function call can appear on the left hand side of an assignment operator
  • References are usually preferred over pointers whenever you don't need "reseating".

member functions VS non-member functions


    • Virtual functions must be members. If f needs to be virtual, make it a member function of class C.
    • operator>> and operator<< are never members. If f is operator>> or operator<<, make f a non-member function. If, in addition, f needs access to non-public members of C, make f a friend of C.
    • Only non-member functions get type conversions on their left-most argument. If f needs type conversions on its left-most argument, make f a non-member function. If, in addition, f needs access to non-public members of C, make f a friend of C.
    • Everything else should be a member function. If none of the other cases apply, make f a member function of C.

      new vs malloc and delete vs free.

      The difference between 'new' & malloc():

      • Operator new constructs an object (calls constructor of object), malloc does not.
      When new has an object, memory space for the object is not only allocated but the object's constructor is called.
      • Operator new is an operator, malloc is a function.
      • Operator new can be overloaded, malloc cannot be overloaded.
      • Operator new throws an exception if there is not enough memory, malloc returns a NULL.
      • Operator new[] requires to specify the number of objects to allocate, malloc requires to specify the total number of bytes to allocate. 
      Operator new automatically calculates the size of the object that it constructs. Conversely, with malloc(), the programmer has to specify explicitly the number of bytes that have to be allocated.
      • malloc() returns void *, which has to be explicitly cast to the desired type but new returns the proper type.
      Operator new returns a pointer to the desired type, so no explicit typecast is required. But, malloc() returns void *, which has to be explicitly cast to the desired type. This is both tedious and dangerous.
      • Operator new/new[] must be matched with operator delete/delete[] to deallocate memory, malloc() must be matched with free() to deallocate memory.The new/delete couple does not have a realloc alternative that is available when malloc/free pair is used. realloc is used to resize the length of an array or a memory block dynamically.

      The difference between 'delete' & free():

      • delete invokes the destructor of the object to be deallocated, free does not do this. 
      • delete can be overloaded, free cannot.

      enum

      enum

      In C++, enum signifies a slightly stronger type than in C, because C++ do type checking.
      For example, in C, one could write:
      enum Direction { UP, DOWN };
      Direction d = 1;
      In C++, this would be illegal, only UP or DOWN can be assigned to a variable of type Direction, though there is still implicit casting to integer, so one could still write:
      int i = UP;
      The way enum variable declared is different in C & C++, the key word enum is not required in C++.
      In C, once the enum was declared as above, declaring variables of type Direction would have to be done through the enum keyword, like this:
      enum Direction d = UP;
      In C++, the name "Direction" becomes a type in itself, so you can write:
      Direction d = UP;

      new & delete operators

      Properties of 'new'

      • 'new' operator initializes new object. i.e; it first calls the memmory allocation premitive, then call the appropriate constructor of the class. 
      • It is type safe. 
      • The 'new' operator can be overloaded by a class. 
      • 'new' operator never ever returns NULL. Instead, if 'new' operator runs out of memory during new operation, it throws an exception of type bad_alloc. (C's malloc() returns NULL)
      • set_new_handler() installs the new handler.
      • The placement syntax of the new operator, construct the object at a predetermined position in memory (void* place = (void*) 0xFEEDBABE; Fred* p = new(place) Fred(42, 42);).We should explicitly call the object's destructor after placement syntax.
      • If an exception occurs in the constructor during p = new SomeClass(), the sizeof(SomeClass) bytes that were allocated are automatically released back to the heap.

      Properties of 'delete'  

      • 'delete' is a two-step operation: it first calls the destructor on the object pointer, say p, then it releases the memory pointed to by p using a memory deallocation primitive.
      • Calling delete p when p is NULL is safe and is guaranteed to do nothing. 
      • Never delete call a pointer twice, Is a DISASTER!. This is likely to corrupt the heap and its list of free memory. 
      • delete[] p is for deleting an array of things. 
      • Its legal for a member function to say delete this, but be careful.  
      • The realloc move data to a new location, it uses a bitwise copy

      Difference between 'operator new' and the 'new' operator?

      • The term "operator new" is used incase when u are overloading the global "new" operator. 
      • The term "new operator" is used in case of dynamic allocation of memory.When a variable is allocated memory dynamically the new operator is used.