info on pointer operators??

This is a discussion on info on pointer operators?? within the C++ Programming forums, part of the General Programming Boards category; Hello, i've come across this problem and got curious, after fiddling with the code for a while, i got operator ...

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

    info on pointer operators??

    Hello, i've come across this problem and got curious, after fiddling with the code for a while, i got operator working for non - pointer class,

    like

    MyClass a,b;
    a += b;


    but what fails are the pointers, (the below)

    MyClass *a, *b;
    a += b;

    this fails with the error, invalid operand of types.


    How do you get the above working? any hint, suggestion, or solution will be appreciated

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How do you get the above working? any hint, suggestion, or solution will be appreciated
    Well a and b are pointers, so:
    Code:
    a += b;
    is adding two pointers, not the contents of the class. What you want is:
    Code:
    *a += *b;
    But this is only guaranteed to work when a and b are pointing to allocated memory. It may work by accident, but to be correct you'd need to allocate memory first for both pointers:
    Code:
    a = new MyClass;
    b = new MyClass;
    
    *a += *b;
    And once you're done with a and b, you'd need to deallocate the memory:
    Code:
    delete a;
    delete b;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 09:38 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 08:33 AM
  3. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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