RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    Operators which can not be overloaded in C++

    February 20th, 2010

    Any operator which operates on value(s) can be overloaded.

    For example,

    
    c = a + b;
    

    As the operator + operates on two values, it can be easily overloaded.

    Now look at the unary operator *. It operates on the variable name, not on the value.

    
    b = *a;
    

    where a is not a value, but a name.

    Similarly the following operators can not be overloaded.

    sizeof - The operator to find the size of a variable in memory

    ? -The ternary operator

    :: – The scope resolution operator

    . – The operator to access class members

    -> – The operator to access class members from a class pointer type variable

    * – The indirection operator


    Auto Pointers

    February 14th, 2010

    Pointers are still a nightmare for most of the C++ beginners. The confusion where to deallocate, deep copy or shallow copy results in memory leaks and occasional program crashes. A possible remedy to this is auto pointers.

    Image courtesy : http://www.usscouts.org
    

    Auto pointers provide some kind of automatic garbage collection. That means the programmer doesn’t have the headache of deleting the pointer, thereby ensuring that there won’t be memory leaks due to his carelessness.

    Unlike normal pointers, there can be only one owner for an auto pointer at a time, though ownership can be transferred.

    An auto pointer can be created using the syntax

    auto_ptr<T> variable name (new  T), where T is the class type to which the pointer belongs.

    An example is

    auto_ptr<int> myInt(new int(10));

    To transfer the ownership, we can simply assign the above to another auto_ptr variable.

    auto_ptr<int> myNewInt = myInt;

    In this case, myNewInt points to the location where 10 is stored and myInt = NULL:

    To assign an auto_ptr to a normal pointer, we can use the auto_ptr method release. Hence, the above functionality can be obtained in the following way also.

    int* myNormalInt = myInt.release();

    On calling release, the auto_ptr is set to NULL where as the memory location pointed by it is not destroyed, but assigned to the new variable.

    To access the memory location pointed by the auto_ptr variable, we can use the method get().

    A small example is described below.

    First we create a simple class

    
    // A simple class
    class CMyClass
    {
    public:
       // Constructor
       CMyClass(int no)
       :m_No(no)
       {
       }
       // Display the member variable
       void Display()
       {
          cout << "No is << " << m_No << "\n";
       }
    private:
       int m_No;
    };
    

    Now various operations using auto_ptr are shown below.

    
    #include "stdafx.h"
    #include <iostream>
    #include <memory>
    #include "assert.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
       // Create an auto_ptr
       auto_ptr<CMyClass> myClass(new CMyClass(10));
       // Assign the auto pointer to a new auto_ptr
       auto_ptr<CMyClass> myNewClass = myClass;
       // myClass will be NULL now
       assert(myClass.get() == NULL);
       // myNewClass now points to the location
       // where myClass pointed to
       myNewClass->Display();
       // myNewClass can be assigned to a normal
       // pointer as below
       CMyClass* myNormalClass = myNewClass.release();
       // myNewClass now points to NULL
       assert(myNewClass.get() == NULL);
       // myNormalClass now points to the location
       // myNewClass pointed to
       myNormalClass->Display();
       // Finally, we can delete myNormalClass.
       // Note that the auto_ptr variables do
       // not need deletion
       delete myNormalClass;
       myNormalClass = NULL;
       return 0;
    }
    

    Cppkid has a new home

    February 13th, 2010

    Image courtesy : http://www.freeprintablecoloringpages.net

    A couple of years ago, cppkid stepped into the world of technical blogging with free space provided generously by Wordpress with an idea to share what he was learning. He has now got his own home in the cyber world under the name techievibes.

    He is always grateful to the support he has got and he hopes that it will continue in the future also. He once again expresses his sincere thanks to all who have provided support (through both criticism and praise).

    See you with next post soon…..