Thread: Operator overloading question

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    Operator overloading question

    I have a class written, and i need to overload the operator + for it, so it just sums everything up really.

    Code:
    Folder f1;
    f1.add_entry("name","post");
    // f1.name[0] = "name"
    // f1.post[0] = "post"
    //both arrays have more space
    
    
    Folder f2;
    f2.add_entry("name2","post2");
    // f2.name[0] = "name2"
    // f2.post[0] = "post2"
    //both arrays have more space.
    
    Folder f3;
    So i need:

    Code:
    f3 = f1 + f2;
    So f3 then contains all the info from f1 and f2.


    How do i write that function, i'm lost with unary operator overloading...

    BTW, the variables in the classes are mostly strings with a bool and a double in there.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Create a member function called "operator +" which returns an object of the same type as the class, and which receives as a parameter any type that you want.

    Code:
    class MyClass;
    {
    public:
       MyClass operator+(int rValue);
       MyClass operator+(double rValue);
       MyClass operator+(const MyClass& rValue);
    };
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM