Thread: Pass pointer to overloaded operator

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    Pass pointer to overloaded operator

    I have an example here:
    Code:
    #include <iostream>
    
    class test{
        private:
            int a;
        public:
            test(){a=3;};
            test operator+(whatgoesinhere? x){test temp;temp.a=a+variable a that we got from b;return temp;}
    };
    
    using namespace std;
    
    int main()
    {
        test *a = new test, *b = new test;
        a=a+b;//call to the function should look like this but with the correct prefixes so the return gets into a
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    The question is? You pass pointers to operator +. The operands should be of 'test' type not 'test*'.
    Code:
    test operator + (const test& a)
    ...
    test a;
    test b;
    a = a + b;

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    You sure helped me a lot... If I were to use testa a, b and not their pointers with allocated memory then i'd just have a test x in the arguments...
    Can some one answer me what I've asked no amtter how stupid or illogical it is, basicly my question is how do you pass a pointer with allocated memory to a function, in this example the class to the operator +.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    *a = *a + *b; // pointer dereference
    Don't forget to release (delete) memory.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to an overloaded operator...
    By yaya in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2009, 01:50 AM
  2. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  3. Overloaded << operator
    By DivineSlayer936 in forum C++ Programming
    Replies: 19
    Last Post: 04-07-2007, 12:46 PM
  4. Need help with overloaded * operator
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2006, 10:52 AM
  5. About Overloaded Operator. Please Help
    By Antigloss in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2005, 07:48 AM