CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2003
    Posts
    347

    virtual distructor

    class abc
    {
    public:
    virtual ~abc(){}
    };


    class bca : public abc
    {
    public:
    ~bca(){}
    };

    main()
    {
    abc *obj1 = new abc;
    bca obj2;
    abc = &obj2;
    delete obj1
    }


    Now how the compiler is calling the distructor of bca????

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Code:
    class abc
    {
    public:
        virtual ~abc(){}
    };
    
    
    class bca : public abc
    {
    public:
        ~bca(){}
    };
    
    main()
    {
        abc *obj1 = new abc;
        bca obj2;
        abc = &obj2;    // <<<---------
        delete obj1
    }
    The marked line is not legal. What is your question?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: virtual distructor

    Code:
    main()
    Illegal. The main() function returns an int.
    Code:
       abc = &obj2;
    abc is a type. Illegal.
    Code:
      delete obj1
    No semicolon. Illegal.
    Now how the compiler is calling the distructor of bca????
    First, provide compilable, legal code.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2003
    Posts
    347
    Sorry for giving a not compiled code.

    class abc
    {
    public:
    virtual ~abc(){}
    };


    class bca : public abc
    {
    public:
    ~bca(){}
    };

    main()
    {
    abc *obj1 = new abc;
    bca obj2;
    obj1 = &obj2;
    delete obj1;
    }


    Now how the compiler is calling the distructor of bca???

    deletes calls the distr of bca also, how compiler is doing this??How compiler know till how many derive class he has to go???

    Thanks

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    Your code is ill-formed.

    First, obj2 was not dynamically allocated, but you are attempting to delete an address that does not point to a dynamically created area.

    When you assigned &obj2 to obj1, you can't call delete on a value that wasn't dynamically allocated. This is undefined behavior. Undefined behavior means anything can happen, including crash.

    Also, your code will still not compile --
    Code:
    int main()
    not
    Code:
    main()
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Feb 2003
    Posts
    377
    It appears this is what you are trying to do - delete a base class pointer that points to an instance of the derived class.
    Code:
    class abc
    {
    public:
      virtual ~abc(){}
    };
    
    
    class bca : public abc
    {
    public:
      ~bca(){}
    };
    
    int main()
    {
      abc *obj1 = new bca;
      delete obj1;
    }
    The reason the derived class destructor is called is because the base class destructor is virtual, meaning the actual type of the object pointed at should be used to find the appropriate function. When the virtual keyword is used, then the compiler knows to look up the appropriate function based on the dynamic type of the object at runtime. Even though the pointer is an abc*, there is information stored that knows that the thing it points to is actually of type bca. This is the point of the virtual keyword. If that is what you meant to ask then I hope this helped.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by jlou
    It appears this is what you are trying to do - delete a base class pointer that points to an instance of the derived class.
    The OP is deleting a pointer that wasn't dynamically allocated, therefore anything can happen, everything from "working" to crashing. It is equivalent to the following:
    Code:
    int main()
    {
      abc *obj1 = new abc;
      abc obj2;
      obj1 = &obj2
      delete obj1;
    }
    This is clearly illegal -- the address that obj1 is pointing to at the time the delete is called was not dynamically allocated (&obj2). Therefore there are two problems, an illegal delete, and if that happened to "work", a memory leak since the original obj1 value that was dynamically allocated is now gone.

    In the code you posted, it is correct -- you are deleting a pointer (regardless of whether it points to a base or derived) that was dynamically allocated.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Feb 2003
    Posts
    377
    It appears to me that the OP was having trouble coming up with an example to illustrate his or her question. Hopefully I was able to cut to the chase and answer the question that was trying to be asked. If the OP was really asking about deleting a pointer to memory that wasn't dynamically allocated, then hopefully your answer has helped him or her.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    OK, but if a question deals specifically with the behavior of some aspect of C++, the OP should post the exact code that they have questions about.

    One variable out of place, one missing indirection, a missing semicolon, etc. may play a major part in how a piece of code would behave.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured