Operating Overloading with Pointer Arguments

This is a discussion on Operating Overloading with Pointer Arguments within the C++ Programming forums, part of the General Programming Boards category; I am trying to implement a class x that has an overloaded operator(). This overloaded operator needs to have pointer ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Operating Overloading with Pointer Arguments

    I am trying to implement a class x that has an overloaded operator(). This overloaded operator needs to have pointer arguments. Can this be done? Here is my code so far.
    Code:
    class X {                                                                                    
      friend ostream& operator <<(ostream &out, const X *ptr)                                      
      {                                                                                             
        out << ptr->l << " " << ptr->n;                                                            
        return out;                                                                                 
      }                                                                                             
                                                                                                    
      friend bool operator<(const X *ptr1, const X *ptr2)                                           
      {                                                                                             
        return ptr1->n > ptr2->n;                                                                       
      }
    
    .....
    public:
    ....
    private:
    char l;
    int n;
    ....
    The reason why I want to do this is because I call pointers of type X and they are pointing to some value. I need these values to sort my container. I know its a little vague but it would help me out a lot... if you have any questions about the requirements please do not hesitate to ask. Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You can overload the () operator just like any other operator. Just put the pointers as arguments just like any other method of the class.

    BTW: Functors or Function Objects are classes that override the () operator and are used extensively in the STL.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    I am sorry about being more confusing than I should be... I was wondering if my overloaded <() was legal. Thank you.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    No, it's not legal. Operator overloads require that at least one operand be of class type. Since pointers are not of class type, your compiler should reject that code.

    From the C++ FAQ Lite: http://www.parashift.com/c++-faq-lit...html#faq-26.10

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 04:20 AM
  3. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 12:26 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Overloading new/delete with additional arguments
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2004, 06:26 AM

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