Thread: Contrustors!!

  1. #1
    Jenny
    Guest

    Contrustors!!

    what is contrustors and destructors in c++?
    if can,pls write a simple c++ containing these two thing up there..........just wanna to know more about this two thing......thanks...

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Constructor's and Destructor's are members of classes. Basically the constructor initializes it and the destructor handles the clean-up.
    Code:
    class MyClass
    {
    private:
      int i;
      LPRECT lprcSrc;
    public:
      MyClass(); //constructor
      ~MyClass(); //destructor
      LPRECT SetUpRect();
    };
    
    MyClass::MyClass()
    {
       i=0;
       lprcSrc=SetUpRect;
    }
    
    MyClass::~MyClass()
    {
       i=0;
       if(lprcSrc)
       {
          delete lprcSrc;
          lprcSrc=NULL;
       }
    }
    
    LPRECT MyClass::SetUpRect()
    {
       if(lprcSrc)
       {
         delete lprcSrc;
         lprcSrc=NULL;
        }
       LPRECT lprc;
       lprc=new RECT;
       lprc->right=32;
       lprc->bottom=32;
       return(lprc);
    }

  3. #3
    Jenny
    Guest

    Unhappy Explain again!

    hi,jdinger
    thanks for the whole coding, did u code in vc++?is it in c++ or vc++?i am not really understand it,can u once again explain each sentence by sentence,thanks once again!
    hear u jdinger


    jenny!~~~

  4. #4
    Unregistered
    Guest
    LPRECT is a typedef for a pointer to type RECT which is a class that defines a rectangle. This syntax is used extensively in Windows API.

    This line:

    lprcSrc=SetUpRect;

    needs a set of () before the semicolon.

    lprSrc is probably an abbreviation for long pointer to type RECT with name of Src (usually short for source). It's a type of hungarian notation that you get used to when reading about/using Windows API. Don't worry about what long pointer means, it's just a pointer.

    In the constructor the member called i is initialized directly, but lprSrc is initialized indirectly by calling the member function SetUpRect() and assigning the return value to lprSrc.

    In SetUpRect() if lprSrc already has a value stored in it, the original memory assigned to lprSrc is deleted and the value of lprSrc is changed to NULL. Then a local variable is declared and is given a memory address by calling new operator. The variables of the RECT to which it points are intialized, and the value of the local variable is returned to the constructor where it is assigned to to the class member.

    The destructor deletes the last memory address stored in lprSrc since the SetUpRect() member won't be able to do so.


    Although a bit more advanced than basic constructor/destructor demonstrations usually given, this is all fine and dandy. The only hesitancy I have with it is that lprSrc has no value, or more presicely is pointing to a random address before it receives it's first address from SetUpRect(). Therefore deleting it in SetUpRect() before developing the local variable to return _may_ delete memory you don't want deleted. I would probably assign NULL to lprSrc in the constructor before calling SetUpRect() and then assign the return value of SetUpRect() to lprSrc, but that may be overkill.

Popular pages Recent additions subscribe to a feed