Thread: this returned destructor called...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463

    "*this" returned destructor called...

    Hi!

    While testing a project I noticed a weird behaviour.
    Here is the draft class:
    Code:
    Class MyClass
    {
    public:
    MyClass();
    ~MyClass();
    MyClass self() { return *this; }
    }
    And here comes the problem:

    Code:
    MyClass anObj;
    anObj.self();
    // PROBLEM HERE - after returning from self() function, de destructor of MyClass gets called
    // do something
    If I change the self() to return a reference:
    Code:
    MyClass& self() { return *this; }
    then the destructor isn't called.

    Do you have a good explanation for this one?
    Last edited by Carlos; 09-24-2003 at 06:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What exactly is iterator returned by .end()?
    By pheres in forum C++ Programming
    Replies: 21
    Last Post: 12-09-2008, 09:50 AM
  2. Replies: 12
    Last Post: 10-16-2008, 02:49 PM
  3. How can the focus be returned to previous window?
    By wow in forum Windows Programming
    Replies: 5
    Last Post: 02-29-2008, 03:00 PM
  4. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-12-2008, 12:37 AM
  5. Varible is different once returned.
    By epoch in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2003, 02:45 AM