-5

Is there a way to do the same in C++?

Python

int.__add__(1, 1)

I can make something in C++, but it is still ugly and not very optimised and short.

I just want this : 1+1, but without + . I absolutely want to use the int "class".

It's for matrix multiplication, addition, and subtraction... I just want to have better-looking and shorter code.

Community
  • 1
  • 1
Henry
  • 577
  • 4
  • 9
  • 3
    How about you define a function `int add(int a, int b) { return a + b; }` and use it like `int r = add(1, 1);` – Javier Silva Ortíz Nov 23 '19 at 17:49
  • 4
    `int` isn't a class. – chris Nov 23 '19 at 17:50
  • I don't want to use the + – Henry Nov 23 '19 at 17:50
  • 6
    Give some context: *why*? – jonrsharpe Nov 23 '19 at 17:52
  • 3
    There is [`std::plus`](https://en.cppreference.com/w/cpp/utility/functional/plus) but you don't want to use it unless you know really well why you need it. – n. m. could be an AI Nov 23 '19 at 17:55
  • 5
    How about just accepting that *different languages* work *differently* and *you* have to adapt to whatever language you are currently using..? C++ is *not* Python, never will be. Python is *not* C++ and never will be. They are *different* and you have to adopt different coding styles and idioms depending on what language you are using. – Jesper Juhl Nov 23 '19 at 17:57
  • Also; why would you ever do `new int`? – Jesper Juhl Nov 23 '19 at 18:01
  • 2
    "I just want to have a better-look code, more short" **Then use `+`**. – n. m. could be an AI Nov 23 '19 at 18:03
  • 1
    The `+` operator is about as short and simple as you're going to get in any practical and readable way. `int a = b + c;` is literally shorter than `int a = add(b, c);` or most (if not all) alternatives that could be done in C++. Furthermore, any other C++ developer is going to know what `+` means at a glance, as opposed to whatever else you might come up with. – TheUndeadFish Nov 23 '19 at 18:13
  • 1
    I suspect you want something like `Matrix operator+(Matrix a, Matrix b);` but your question is vague and hard to be sure. Can you be more specific to your problem? – Galik Nov 23 '19 at 18:18
  • 1
    Maybe it would help if you presented what you did in C++? Maybe also explain the persepective from which "`1+1`" is at least one of: ugly, not optimized, or not short? What syntax would be ideal for you, and why do you see it as superior to `1+1`? – JaMiT Nov 23 '19 at 19:29

2 Answers2

3

I can make something in C++, but it is still ugly and not very optimised and short.

Last time I checked, int.__add__(1, 1) was way longer than 1+1. Just about anything is longer than 1+1. As for optimisations, you are not in a position to talk about what is more optimised, having not measured anything.

It's for matrix multiplication, addition, and subtraction

The same integer + operator is useful in lots of contexts. Matrix operations are among them. There is no need to single them out.

I just want this : 1+1, but without +

What you want is of secondary importance. Programming is a team sport. You do what everyone else does. Not only because it is likely to be tried and tested and the best thing after the sliced bread, but also because if everyone is doing something in a particular way and you are doing it differently, others will find it hard to understand what you mean.

This is not to say you cannot break conventions and introduce innovations. People do it all the time. But they are not asking anyone how to! If you need to ask, you are not in a position to lead the crowd.

I just want to have better-looking and shorter code.

Then absolutely positively use +. A no-brainer.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

You want to use a function (or functor) for addition instead of the + operator. C++ has std::plus for that. I don't use C++ but I think one or both of these should work:

int a = std::plus(1, 1);
int a = std::plus<int>(1, 1);
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45