is overloading parantheses c++ a good idea [duplicate] - c++

This question already has answers here:
What are C++ functors and their uses?
(14 answers)
Closed 4 years ago.
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];
}

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.

Related

C# extension methods analogue in C++ [duplicate]

This question already has answers here:
Extension methods in c++
(7 answers)
C++ Class Extension
(9 answers)
Closed 5 years ago.
C# has this little nice feature: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
This is really cool. Let me give you an example:
I want to add concat method to std::vector but I don't want to inherit it. This feature would be very useful. Do you have any analogues feature in C++ that allows to add a function to a type without inheriting from the original type? I am asking for a language-level feature, please.
In C++ this is not directly possible; that being said, you could implement a function that has a reference to a std::vector as a first argument, serving a similar purpose as an extension method (which more or less are just syntactic sugar for that).
A free function might be your friend here e.g.
namespace VectorMethods
{
std::string contat(const std::vector<std::string>& vec)
{
// return result of concatenating vector
}
}

Why can't a ternary operator be overloaded? [duplicate]

This question already has answers here:
Why is it not possible to overload the ternary operator?
(6 answers)
Closed 5 years ago.
I was always curious about that, but never found a solid answer or an easy to understand explanation. I have tried with other operators and it works just fine. This operator in particular is an interesting one. I just can't get my head wrap around it.
FAQ of stroustrup :
There is no fundamental reason to disallow overloading of ?:. I just
didn't see the need to introduce the special case of overloading a
ternary operator. Note that a function overloading expr1?expr2:expr3
would not be able to guarantee that only one of expr2 and expr3 was
executed.

Overload the assignment operator in C++ [duplicate]

This question already has answers here:
C++ assignment operator - compiler generated or custom?
(5 answers)
Closed 7 years ago.
I have a general question i C++ that I coudln't get a clear answer to and I'm hoping I get an answer to it here. As a programmer, do I need to explicitly overload the assignment operator for a class when all instance data fields in this class are standard built in C++ data types.
No. You only need to overload the defaults if you are doing manual resource management. If all of your members can be treated as POD types then you will be fine.
No, you don't.​​​​​​​​​​​​​​​​​​​​​

finding addressof a object whose ampersand operator is overloaded and private [duplicate]

This question already has answers here:
If an operator is overloaded for a C++ class how could I use a default operator instead?
(3 answers)
Closed 8 years ago.
I have a class which is overloading ampersand(&) operator and made it private. I don't have a latest C++11 compliant compiler so is there any way by which I can get address of my object using current C++ compiler only
Thanks
reinterpret_cast<T *>(&reinterpret_cast<char&>(obj))
Dunno if it's safe though.. (well clearly it's a bit dodgy)
Use the addressof() function from Boost.Utility. If you don't want to use Boost, you can still look at it's implementation which consists of just a single header.

C++ Classes - dot notation vs pointer [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the difference between the dot (.) operator and -> in C++?
What's the difference between using dot notation and the pointer way?
Instantiating an object with or without a pointer.
Instantiate w/o a pointer = then use dot notation
Instantiate w/ a pointer = then use ->
What are the differences between both? When and why should one be used over the other?
If I understand your question: in C++, a->b is just shorthand for (*a).b -- they're exactly the same (Edit: unless you've overloaded them to behave differently!), it's just that the first is easier to type. :)
If you're referring to using string a; versus string* a = new string(), that's a different topic -- look up stack-based and heap-based allocation.