Thread: OverLoading the "()" parentheses operator

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    OverLoading the "()" parentheses operator

    An requires the use of an overloaded () operator for a comparator class.

    1. Would someone explain the purpose and benefits for overloading the ()?

    2. Please provide a code fragment that shows an example for a declaration and implementation.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
     
    struct Counter
    {
       public:
    	 //default constructor
    	 Counter() : itsVal(0) {}
     
    	 //conversion by construtor
    	 Counter(int i) : itsVal(i) {}
    	 
    	 //overloaded conversion operator
    	 operator unsigned short();
    	
    	 //routine public accessor method for private data
    	 int getItsVal() const {return itsVal};
     
       private:
    	 int itsVal;
    };
     
    Counter::operator unsigned short()
    {
      return(int (itsVal));
    }
     
    int main()
    {
       Counter ctr(5);  //ctr.itsVal == 5;
     
       //implicit use of conversion operator to initialize
       //theShort with ctr.itsVal without using an 
      //explicitly declared public access method like
      //getItsVal() 
       int theShort = ctr;
     
       //using public accessor method 
       int theSecondShort = ctr.getItsVal();
     
       std::cout << "theShort: " << theShort << std::endl;
       std::cout << "theSecondShort: " << theSecondShort <<std::endl;
       return 0;
    }
    revised from J Liberty Teach Yourself C++. Note: conversion operators do not specify a return value.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think he was asking about operator(), the function call operator.

    This operator makes it possible to treat an instance of your class like a function:
    Code:
    struct foo
    {
      void operator()() {
        std::cout << "Hello!" << std::endl;
      }
    };
    What are the benefits? That's kind of hard to explain. It allows you to supply behaviour to template functions, I think that's the most common use.
    http://stud3.tuwien.ac.at/~e0226430/....xhtml#functor
    Here I give one example what the use could be. Requires a browser that accepts application/xhtml+xml. (Not IE)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ah, yes, the infamous function object/functor. Well, I hope one or the other methods of overloading the () operator for a given class is what he/she was wanting.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *ehem* I haven't been posting too much in the past...well year or two, but I can still spot a homework assignment when I see one. Did you guys really need to do it for him/her without questioning the motives of misguided student?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If that's a homework assignment, then the teacher needs a lesson on how to develop homework assignments that are clear in their intent. If you can explain conversion operator declaration/syntax or function object/functor declaration/syntax without an example, I'd be happy to see it.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are indeed correct. The question was ambiguous at best.

  8. #8
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    No! It is not THE homework assignment. The assignment requires the building of a heap priority queue, which involves over 11 steps. The overload was a requirement for the queue. I asked about its use to understand how to apply it to the code. I would have provided the specific instructions for its implementation if I wanted to cheat.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    One useful application of operator() is array subscripting for more than one index, for example in matrices.

    If I have a vector class, I can overload [] and everything is fine:
    Code:
    template<int n, typename T=double>
    class Vector
    {
       ...
       T& operator[] (size_t index)
       {
          return data[index];
       }
       ...
      private:
         T data[n];
    };
    If I create a matrix class, I cannot use the []-operator (unless I return a vector from the matrix) but I can use the ()-operator
    Code:
    template<int n, int m, typename T=double>
    class Matrix
    {
      ...
      T& operator()(size_t, i, size_t j)
      {
        return data[i][j];
      }
      ...
      private:
        T data[i][j];
    };
    That's one of the uses. Another is for function objects, which are used frequently in the STL, like sorting predicates etc.
    I will post an example of that soon.
    Last edited by Sang-drax; 09-24-2004 at 06:20 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here's one simple use of the ()-operator when dealing with the STL.

    Imagine that I wanted to calculate the sum in an std::vector

    Code:
    template<typename T>
    class SumCalculator
    {
    public:
      SumCalculator() sum(0) {}
    
      void operator()(const T& t)
      {
        sum += t;
      }  
    
      T getSum()
      {
        return sum;
      }
    private:
      T sum;
    };
    
    
    int main()
    {
      vector<int> numbers;
      numbers = ...
    
      //Calculate the sum
      SumCalculator calculator;
      for_each(numbers.begin(), numbers.end(), calculator);
      int sum = calculator.getSum();
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM