2

I have programmed in c++ for a couple of months, and I am starting to understand the core. At the moment I am trying to make a calender program with classes and inheritance (just to get more comfortable with object oriented programming), and a few weeks ago I learned about operation overloading.

I am wondering, is it a bad idea to overload parantheses for an object, such that I could for instance write this, or can an error occure becuase the compiler can confuse it for something else(constructor or something like that)?

//creating a valid year-object
Year year1998 = Year(1998,true);

// the parantheses operator returns a day(another object)
Day d = year1998(1,10);

//the overloading
Day& Year::operator()(int monthNumber, int dayNumber){
    //Just returns a day from the month class
    return months[monthNumber][dayNumber];
}
  • 2
    You can find information for this under "constructors", not "operator () overloading". – nwp Mar 13 '18 at 15:54
  • 3
    Sounds like a bad idea in this particular case. Why would "calling" a year result in a day? I would simply write a method called `day`. – mkrieger1 Mar 13 '18 at 15:55
  • In general, operator overloading is an evil thing, when used in place of proper member functions. You need a valid reason to use operator overloading, not just "this looks fun". An example of valid operator overloading is when you need compatibility with another type and therefore overload the cast operator. – Lundin Mar 13 '18 at 15:56
  • To answer your two questions 1) the compiler will have no issue discerning your constructor from your `operator()` call. 2) Usage of `operator()` as you wrote is an example of using your class as a "functor", see above link – Cory Kramer Mar 13 '18 at 15:56
  • Note: `operator()` is the function call operator. It's not "overloading parenthesis as in (foo + (bar - baz))". Just in case that's what you thought it did - it was not entirely clear to me in your question. – Jesper Juhl Mar 13 '18 at 15:57
  • 2
    For your case it is a bad idea as it is not idiomatic. Does calling a year with two random ints make sense? Keep it simple. Add a member that does get_day(). While it is useful to know what can be overloaded in C++ and how to do it, excessive showboating will cause more problems than it solves. – djgandy Mar 13 '18 at 16:02

1 Answers1

-3

Overloading this operator is a basic concept in C++ and is extensively used in function objects, a typical example being comparison functions.

You can savely use this operator, but nowadays often lambda functions are preferred to function objects.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • If you downvote an answer it is typically a great idea to state the reason. – ypnos Mar 13 '18 at 15:57
  • The most lame experience I had so far on SO… – ypnos Mar 13 '18 at 16:01
  • 4
    [No, it is not](https://meta.stackoverflow.com/q/357436/1889329). – IInspectable Mar 13 '18 at 16:01
  • 1
    I am with you, the question was in general "is it a bad idea to overload parantheses for an object", not regarding only this example, and function objects are a good general use case. – James Beilby Mar 13 '18 at 16:02
  • 3
    The question has nothing to do with function objects or lambdas. You completely missed the point of the question. – nwp Mar 13 '18 at 16:03
  • 1
    Not that it's particularly relevant, but lambda expressions are merely one way to create a class that overloads `operator()`. – Jerry Coffin Mar 13 '18 at 16:06