Operator overloading and constructors

This is a discussion on Operator overloading and constructors within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm currently overloading the istream operator to construct an object. Code: istream& operator>>(istream& is, Person&) { string name; int ...

  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,801
    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,801
    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
    21,743
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,743
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,743
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,743
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 2 12 LastLast
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, 09:16 AM

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