Thread: Operator overloading and constructors

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Operator overloading and constructors

    Hi,

    I'm currently overloading the istream operator to construct an object.

    Code:
    istream& operator>>(istream& is, Person&)
    {
    	string name;
    	int age;
    
    	is >> Person(name, age);
    
    	return is;
    	
    }
    This works as such: cin >> Person("darren", 32);

    I know, I can just construct objects without using the >> operator, but this is an exercise in the book. My question is when I do it this way, I am not giving the object a name as such. How can I do that? For example if I was constructing this just via the constructor I would use: Person p("darren", 32).

    Thanks.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Sorry, I also use it as such :
    Code:
     while(cin>> person) {//...do something}
    Thanks.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Nope, my code doesn't work

    How can I overload >> to work with a class constructor and then use that to create objects?

    Thanks.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Normally you'd do something like:
    Code:
    #include <string>
    #include <iostream>
    
    class Person
    {
    public:
        std::string name;
        int age;
    };
    
    std::istream& operator>>(std::istream& is, Person& rhs)
    {
        return is >> rhs.name >> rhs.age;
    }
    
    int main()
    {
        Person foo;
    
        while(std::cin >> foo)
        {
            // Do something with foo
        }
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by hk_mp5kpdw View Post
    Normally you'd do something like:
    Code:
    #include <string>
    #include <iostream>
    
    class Person
    {
    public:
        std::string name;
        int age;
    };
    
    std::istream& operator>>(std::istream& is, Person& rhs)
    {
        return is >> rhs.name >> rhs.age;
    }
    
    int main()
    {
        Person foo;
    
        while(std::cin >> foo)
        {
            // Do something with foo
        }
    
        return 0;
    }
    I did try that earlier, but because the class has a constructor, when I declare Person, it expects the appropriate arguments.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Can you not simply declare a default constructor or are there some arbitrary constraints we are not aware of? If so let us know so we can really help without having to guess what we can and cannot use, start by showing us the code for your Person class so we can know what its constructors are.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by hk_mp5kpdw View Post
    Can you not simply declare a default constructor or are there some arbitrary constraints we are not aware of? If so let us know so we can really help without having to guess what we can and cannot use, start by showing us the code for your Person class so we can know what its constructors are.
    Hi,

    The exercise in the book says to declare the constructor and modify the operator overload to accomodate these.

    The constructor is:

    Code:
    Person(string n, int a);
    
    Person::Person(string n, int a)
    :name(n), age(a)
    {
    }
    Thanks again.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The thing is, in order to use an overloaded operator>> to insert into a Person object, the Person object must already exist. That is, it must have already been constructed. What exactly did the book say? What is your current Person class definition?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    The thing is, in order to use an overloaded operator>> to insert into a Person object, the Person object must already exist. That is, it must have already been constructed. What exactly did the book say? What is your current Person class definition?
    The book said:

    1. Define input >> operator for Person to read from cin.
    2. Give Person a constructor initialising name and age.
    3. Make representation of Person private, and provide const member functions name() and age() to read the name and age.
    4. Modify >> to work with the redefined person.

    Thanks.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. In this case, a typical solution is to declare that operator>> as a friend function of the class. This way, you will be able to implement it with:
    Code:
    return is >> rhs.name >> rhs.age;
    even though the name and age member variables are private.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Ah. In this case, a typical solution is to declare that operator>> as a friend function of the class. This way, you will be able to implement it with:
    Code:
    return is >> rhs.name >> rhs.age;
    even though the name and age member variables are private.
    Thanks, The problem with this book is that there are often exerices that require further knowledge of what they have not yet covered!!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by darren78
    The problem with this book is that there are often exerices that require further knowledge of what they have not yet covered!!
    Well, there is another way of doing it that does not require one to declare a friend function, but it is perhaps a little less elegant:
    Code:
    std::istream& operator>>(std::istream& is, Person& rhs)
    {
        std::string name;
        int age;
        if (is >> name >> age)
        {
            rhs = Person(name, age);
        }
        return is;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Well, there is another way of doing it that does not require one to declare a friend function, but it is perhaps a little less elegant:
    Code:
    std::istream& operator>>(std::istream& is, Person& rhs)
    {
        std::string name;
        int age;
        if (is >> name >> age)
        {
            rhs = Person(name, age);
        }
        return is;
    }
    Do I not need to declare a Person object first using this example and then pass to this function?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by darren78
    Do I not need to declare a Person object first using this example and then pass to this function?
    Yes, you still need to do that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Yes, you still need to do that.
    So, as well as having a constructor such as:

    Code:
    Person::Person(string n, int a)
    : name(n), age(a)
    {
    }
    I should have one as such:
    Code:
    Person::Person()
    {
    }
    Just to allow an empty object to be created and used to initialise further objects to be stored in the vector? Is that correct?

    Thanls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructors
    By MarlonDean in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2008, 01:21 AM
  2. Protecting Constructors
    By kidburla in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2006, 11:18 PM
  3. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  4. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM