Overloading + operator that can't be member nor friend??

This is a discussion on Overloading + operator that can't be member nor friend?? within the C++ Programming forums, part of the General Programming Boards category; 4) Addition – To add two quaternions you have to add the corresponding components. Thus (1.1, 2.2, 3.3, 4.4) + ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Overloading + operator that can't be member nor friend??

    4) Addition – To add two quaternions you have to add the corresponding components. Thus (1.1, 2.2, 3.3, 4.4) + ( 1, -1.2, 4.4, 10) = (2.1, 1, 7.7, 14.4).
    Implement both the += operator and the binary + operator. The += should be a member function. The operator+ function can’t be a member or a friend function.

    okay well i'm still pretty newbie to this whole c++ thing, but i've made a class for the quaternions, and i just simply don't know how to overload the + operator without making it a member or a friend..how would i access the private members of the class if it's neither of those???

    assignment due soon so any help or just anything to get me started would be great, - thanks

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> how would i access the private members of the class if it's neither of those???

    You could create accessor member functions to get private member data of a class
    Code:
    class myClass
    {
    public:
        myClass();
        ~myClass();
        int getData();
    private:
        int data;
    };
    
    myClass::myClass{}
    myClass::~myClass{}
    int myClass::getData{ return data }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Overload += first, then do this:
    Code:
    quaternion operator+(const quaternion &lhs, const quaternion &rhs)
    {
      quaternion temp(lhs);
      temp += rhs;
      return temp;
    }
    That's the most common way to overload +.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    5
    thank you so much, you guys are a life saver ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

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