Thread: Temporary object destruction

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Temporary object destruction

    Hi

    I was playing with the new operator overload that I have learned, the typecast operator. I made a little code that implements a specified behavior:

    1) A manager is responsible for an object.
    2) When needed, the manager assing a temporary employee to work on the object.
    3) After the job is done, the employee is dismissed.
    4) At the same time, the modified object is used for other activities

    I noticed that, playing with the typecast operator I can return an operator from a function, so it will be destroyed automatically, and cast the operator to the object, so I can use it later. I know that this implementation is not good. I just want to know if this behavior is correct, if it is unsafe, compiler dependant, among others...

    You can see the code here or by downloading the example.

    Thanks for any help!

    Edit: please, see program output to betetr understand what I mean

    Code:
    class Box {
    public:
        Box () {
            thisId = ++id;
            cout << "Box " << thisId << " is now packed." << endl;
        }
    
        string getStatus () {
            char message[60];
            sprintf(message, "Box %d is now being assigned", thisId);
            return message;
        }
    
        ~Box () {
            cout << "Box " << thisId << " is now destroyed." << endl;
        }
    private:
        static int id;
        int thisId;
    };
    
    int Box::id = 0;
    
    template<class T>
    class Operator {
    public:
        Operator (T& obj)
        : object(obj) {
            object = obj;
            // do something to object
        }
        
        operator T* () {
            return &object;
        }
        
        ~Operator() {
            //write some log
            cout << "Operator dismissed." << endl;
        }
    private:
        T& object;
    };
    
    template<class T>
    class Manager {
    
    public:
        Manager(T& obj)
        : object(obj) {
            object = obj;
            cout << "Manager is now leading." << endl;
        }
        
        T* getAnObject() {
            return (T*) Operator<T>(object);
        }
        
    
    private:
        T& object;
    };
    
    int main() {
        Box box;
        Manager<Box> manager( box );
    
        string status = manager.getAnObject()->getStatus();
        
        cout << status << endl;
        
        // cin.get();
        
        return 0;
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's a misuse of the conversion operator overloading. You should only overload it if there's a meaningful equivalence between the two types in question. There is definitely no such thing in this case.

    Also, the Operator ought to be destructed as getAnObject returns, which is probably too early.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by CornedBee
    It's a misuse of the conversion operator overloading. You should only overload it if there's a meaningful equivalence between the two types in question. There is definitely no such thing in this case.
    I know, that's why I said that this solution is not good. It is just an example. I am more interested in the behavior.

    I think that a better example would be a string being converted to an integer. Instead of overloading the = operator, the typecast could be used,... or not.

    Quote Originally Posted by CornedBee
    Also, the Operator ought to be destructed as getAnObject returns, which is probably too early.
    Not in this case, it is my intention to destroy the operator exactly at that time, when he "delivers the box".

    Edit: but, if the Operator is destroyed before the function returns, and not after, as would a normal temporary object, it isn't good.

    I am interested in the reason why the operator is still correctly destroyed after I cast it to a pointer to another thing.
    Last edited by Mortissus; 07-05-2006 at 06:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2008, 01:34 PM
  2. Order of Object Destruction
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2006, 08:25 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. returning a temporary class object
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 03:54 PM