Thread: Operator overloading question

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Question Operator overloading question[Solved]

    hi,
    I have two classess classA-var and classB-var
    I want to overload * operator so I can return classA-var*classB-var
    the header is in classA :
    classA& operator+(classB& var);
    and implemented it .
    it works fine for classA-var*classB-var and it returns a classA-var2;

    how do I implement it so it would work for classB-var*classA-var too and returns classA-var2.

    thanks.
    Last edited by ranish; 04-27-2010 at 01:21 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do the same thing, only do it in ClassB and switch the types.

    You'll need to be using forward declarations and have the implementation inside the source file if you aren't already doing that.

    You could also make these functions non-member functions. That might be a better solution.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    thanks I've solved the problem by making friend ClassB in ClassA
    the header is in classB :
    classB& operator+(classA& var);
    and implementing it to retrieve a ClassA type :
    ClassA &ClassB:: operator+(ClassA &var2)

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 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