Pass pointer to overloaded operator

This is a discussion on Pass pointer to overloaded operator within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    681
    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;
    I never put signature, but I decided to make an exception.

  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
    681
    Code:
    *a = *a + *b; // pointer dereference
    Don't forget to release (delete) memory.
    I never put signature, but I decided to make an exception.

  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, 12:50 AM
  2. Replies: 3
    Last Post: 12-09-2008, 10: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

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