const Point* pt1

This is a discussion on const Point* pt1 within the C++ Programming forums, part of the General Programming Boards category; This is my code Code: friend Point operator-(const Point*, const Point*); Code: Point operator-(const Point* pt1, const Point* pt2){ int ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    const Point* pt1

    This is my code
    Code:
    friend Point operator-(const Point*, const Point*);
    Code:
    Point operator-(const Point* pt1, const Point* pt2){
    	int x = pt1->x - pt2->x;
    	int y = pt1->y - pt2->y;
    	return Point(x, y);
    }
    yes I am trying to use Point* Instead of Point&
    I am getting this Following Error
    Code:
    oops_member_op_overload.cpp:26: error: 
    ‘Point operator-(const Point*, const Point*)’ must have an argument of class or enumerated type
    oops_member_op_overload.cpp:51: error: 
    ‘Point operator-(const Point*, const Point*)’ must have an argument of class or enumerated type

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    11
    maybe you could share a little more of you code to get an idea how you declares and uses your Pointer-class?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't overload these operators for pointers, because pointers are built-in types and they already have these operators defined. Substracting one pointer from another gives you their distance.

    You can't use operator overloading to change the way how the language fundamentally works. Similarly, you can't make + operator for int-type do something else than addition.

    Code:
    #include <iostream>
    
    class A
    {
    };
    
    int main()
    {
        A* p = new A[2];
        std::cout << &p[1] - &p[0] << '\n'; //1
        delete [] p;
    }
    Last edited by anon; 06-30-2007 at 09:09 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You can use a smartptr or proxy class. Here a an example for demonstration purposes. (meaning very incomplete for general use)

    Code:
    #include <iostream>
    
    class Point
    {public:
    
         int mx,my;
         Point (int x, int y):mx(x), my(y)
         {}
    };
    
    class PointProxy
    {
         Point* mpPoint;
    public:
    
         PointProxy(Point* p):mpPoint(p)
         {}
    
         Point* operator->()
         {
              return mpPoint;
         }
         Point operator-(PointProxy& proxy)
         {
              return Point(mpPoint->mx - proxy->mx, mpPoint->my - proxy->my);
         }
    };
    
    
    
    
    int main()
    {
         PointProxy A = new Point(10,15);
         PointProxy B = new Point(12,19);
    
         Point C = A-B;
    
         //to show that they are treated like Pointers to point
         std::cout << A->mx << std::endl;
         std::cout << B->mx << std::endl;
    
         //to show operator- worked
         std::cout << C.mx << std::endl;
         std::cout << C.my << std::endl;
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I donno what does this mean can you tell me whats this ??
    Code:
    Point (int x, int y):mx(x), my(y){}
    I am confused with the ':'
    Code:
    I know Point::function(){}
    But not Point::function():somethingelse{}

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by anon
    You can't overload these operators for pointers, because pointers are built-in types and they already have these operators defined. Substracting one pointer from another gives you their distance.
    So while overloading Do I always need to use
    Code:
    const class_name&

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by noobcpp View Post
    I donno what does this mean can you tell me whats this ??
    Code:
    Point (int x, int y):mx(x), my(y){}
    I am confused with the ':'
    Code:
    I know Point::function(){}
    But not Point::function():somethingelse{}
    it's a way to initialize your member variable on creation
    Though not exactly the same think of
    Code:
    Point (int x, int y):mx(x), my(y){}
    as the same as

    Code:
    Point (int x, int y)
    {
         mx = x;
         my = y:
    }
    except the first way is more efficient and in some ways the only way to initialize some member variables, like const

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Can you show me an example on that ??

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Maybe you should check a reference site like

    http://www.cprogramming.com/tutorial...lists-c++.html

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by noobcpp View Post
    I am confused with the ':'
    Code:
    I know Point::function(){}
    But not Point::function():somethingelse{}
    It's called an initializer list and works for constructors only.

    Some rather stupid example
    Code:
    class Circle {
    public:
        Circle( int X, int Y, int R );
    protected:
       const int x;  // they should not be allowed
       const int y;  // to change after construction
       int r;
    };
    
    /* this will not work */
    //Circle::Circle( int X, int Y, int R ) {
    //    x = X; y = Y; r = R;
    //}
    
    /* the only way to initialize x and y */
    Circle::Circle( int X, int Y, int R ) 
    : x(X), y(Y), r(R)
    {
    
    }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

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