Is it possible to create operator overloads for non-objects (like typedef-ed arrays)?

Recommended Answers

All 3 Replies

Typedefs are just aliases, they don't "create a new type". So, you have to overload the operator for the underlying type. You can also overload with the typedef, but that will just overload for the underlying type as well.

So, basically, the rule applies as for the underlying type. If the type is a type for which you cannot overload (e.g. built-in type) or should not overload (like an STL container), then it is not possible. And if the type already has an overload for it, then it will conflict with your typedef'd overload.

Find a better option. Wrap it is a new class. You could also provide, in that new class, an implicit conversion operator. This will allow all built-in operator overloads to act on the type if there is no specific overload that you provided for wrapper class. But this technique can have surprising or undesirable side-effects.

Typedefs are just aliases, they don't "create a new type". So, you have to overload the operator for the underlying type. You can also overload with the typedef, but that will just overload for the underlying type as well.

So, basically, the rule applies as for the underlying type. If the type is a type for which you cannot overload (e.g. built-in type) or should not overload (like an STL container), then it is not possible. And if the type already has an overload for it, then it will conflict with your typedef'd overload.

So you are saying that I would have to wrap the following type in a class to used overloaded operators?

typedef float Float_4x4[4][4];

Yes, definitely. C++ cannot allow you to overload operators for its built-in types or any array of them, that would be too easy to abuse and too dangerous, with no gain. You definitely need to wrap this in a class, and you should also want to do so, because defining such a 4x4 matrix definitely requires more than a simple typedef.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.