Thread: Hel with destructor

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    Hel with destructor

    Code:
    #include <iostream>
    using namespace std;
    
    class Student {
        int no;
        double gpa;
      public:
        Student();
        Student(double g);
        Student(const Student&);
        void operator++();
        Student& operator=(const Student& s);
        friend Student operator+(const Student& s, double d);
        friend ostream& operator<<(ostream& os, const Student& s);
        ~Student();
    };
    Student::Student() {
        cout << '#';
        no = 0;
        gpa = 0.0;
    }
    Student::Student(double g) {
        cout << '@';
        no = 1;
        gpa = g;
    }
    Student::Student(const Student& s) {
        cout << '*';
        *this = s;
    }
    void Student::operator++() {
        no++;
    }
    Student& Student::operator=(const Student& s) {
        cout << '=';
        no = s.no;
        gpa = s.gpa;
        return *this;
    }
    Student::~Student() {
        cout << '~' << *this << endl;
    }
    Student operator+(const Student& s, double d) {
        cout << '+';
        Student result = s;
        cout << '+';
        result.gpa = (result.gpa * result.no + d);
        result.no++;
        result.gpa /= result.no;
        return result;
    }
    ostream& operator<<(ostream& os, const Student& s) {
        return os << s.no << '-' << s.gpa;
    }
    int main() {
        Student s[2];
        cout << endl;
        s[0] = 2.0;                           // THIS ONE CALLS THE Student::Student(double g). 
        s[1] = 3.0;                             IT Also calls Student& Student::operator(const  Student& s) and the destructor which I dont get it.
        s[0] = s[0] + 2.0;
        ++s[0];
        cout << s[0] << endl;
        cout << s[1] << endl;
        return 0;
    }
    Output:
    ##
    @=~1-2
    @=~1-3
    +*=+*=~2-2
    =~2-2
    3-2
    1-3
    ~1-3
    ~3-2

    Question:
    I dont get why I got @=~1-2
    and @=~1-3.

    The destructor was called but I dont know how.
    Last edited by byebyebyezzz; 08-03-2011 at 07:28 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The line
    Code:
    s[0] = 2.0;
    calls (1) a constructor to make a temporary object out of 2.0; (2) the assignment operator to copy the newly-constructed temporary object into the object s[0]; (3) a destructor to get rid of the temporary object.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    Quote Originally Posted by tabstop View Post
    The line
    Code:
    s[0] = 2.0;
    calls (1) a constructor to make a temporary object out of 2.0; (2) the assignment operator to copy the newly-constructed temporary object into the object s[0]; (3) a destructor to get rid of the temporary object.
    I get part 1 and 2.
    Do you mind explain more on how it calls the assignment operator?
    I thought the assignment operator can only be call when it meet the right condition. In this case, It doesnt return a reference I think. I am a bit confused.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know what "right condition" you refer to. The temporary object is passed by const reference to the operator= function, if that's what you mean.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    Student& Student:perator=(const Student& s)

    for this function, you need to return a reference to Student.

    but s[0] is an object.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And?

    (EDIT: I suppose to be clear: you never use the return value in any way, shape, or form, so it doesn't matter what it returns. For our purposes, it could return void and it would still work. The act of copying happens inside the function. The return as a reference merely lets you chain the thing:
    Code:
    some_other_object = s[0] = 2.0;
    Here you use the return value of the operator= as the right side of the second operator= (and by second, I mean the one on the left). In your example, the return value is ignored.
    Last edited by tabstop; 08-03-2011 at 08:36 AM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by byebyebyezzz View Post
    Student& Student:perator=(const Student& s)

    for this function, you need to return a reference to Student.

    but s[0] is an object.
    s[0] has nothing to do with it. Returning *this was the correct thing to do there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with destructor
    By C_programmer.C in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2011, 05:57 AM
  2. How to use a destructor
    By Noobie in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2003, 12:40 AM
  3. destructor and how to use it
    By Micko in forum C Programming
    Replies: 1
    Last Post: 11-22-2003, 12:50 PM
  4. destructor
    By bharathbangera in forum C++ Programming
    Replies: 5
    Last Post: 05-18-2003, 12:31 PM
  5. Destructor help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 04:58 AM