need help with out

This is a discussion on need help with out within the C++ Programming forums, part of the General Programming Boards category; this code has an output which is kinda confusing me. Before enterig the scope why does it call the copy ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    need help with out

    this code has an output which is kinda confusing me. Before enterig the scope why does it call the copy contructor before the display, and then before entering a scope why is the destructor called. Plus the overall output is confusing me, could you explain it to me: here is the output:
    [code]
    Switch::Switch()
    Switch::Switch(const Switch& other)
    Is Switch open?: 0
    Switch::~Switch()
    Switch::Switch()
    Switch::Switch(const Switch& other)
    Is Switch open?: 0
    Switch::~Switch()
    Switch::~Switch()
    Switch::Switch(const Switch& other)
    Is Switch open?: 1
    Switch::~Switch()
    Switch::~Switch()
    Code:
    #include <iostream.h>
    
    class Switch {
    public:
             Switch()  { 
    						cout << "Switch::Switch()" << endl;
                            position = false; 
              }
    
             Switch(const Switch& other)    { 
                        cout << "Switch::Switch(const Switch& other)" << endl;
                        position = other.position; 
            }
    
            ~Switch() { 
                        cout << "Switch::~Switch()" << endl;
            }
    
            void turnOn() { position = true; }                                  
            void turnOff() { position = false; }
            bool getPosition() const { return position; } 
    private:
            bool position;
    };
    
    void display(Switch sw) {
            bool result = sw.getPosition();
            cout << "Is Switch open?: " << result << endl;
    }
    
    void main() {
           Switch aSwitch;
            display(aSwitch);
    		{
                        Switch* pSwitch = new Switch();
                        display(*pSwitch);
    					delete pSwitch;
            }
    
            aSwitch.turnOn();
            display(aSwitch);
    }

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: need help with out

    OK, let's go through one line at a time:

    Code:
           Switch aSwitch;
     // Switch::Switch()
    
            display(aSwitch);
    
    //Switch::Switch(const Switch& other)
    //Is Switch open?: 0
    //Switch::~Switch()
            {
                        Switch* pSwitch = new Switch();
    //Switch::Switch()
    
                        display(*pSwitch);
    //Switch::Switch(const Switch& other)
    //Is Switch open?: 0
    //Switch::~Switch()
    
    					delete pSwitch;
    //Switch::~Switch()
            }
    
            aSwitch.turnOn();
            display(aSwitch);
    //Switch::Switch(const Switch& other)
    //Is Switch open?: 1
    //Switch::~Switch()
    And the final destructor is called by the death of aSwitch at the end of the function.

    The reason each call to display has a copy constructor and destructor is because display is passed a parameter of type Switch, not Switch* or Switch&. It does not get passed the original object; instead, a new object is created (via copy constructor), and this temporary is passed to the function. When display exits, the temporary is destroyed.

    If you wanted to pass the original object, you can do something like this:

    void display(Switch &sw)

    and it would be fine. Of course, for const-correctness, the better line would be

    void display(const Switch &sw)

    Clear?
    Last edited by Cat; 07-21-2003 at 11:25 AM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    aah thanks alot, so if display would take a & then there would be call to the constructor or destructor, makes perfect sense, thanks for the excellent explanation.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    when the code leaves the scope isnt the destructor called on automatically........but why when i remove delete from the last line the destructor isnt called????

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21