Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading ** in C++

In Python, we have nice simple syntax to take an arbitrary int/float to a power. Namely, for you non-Python programmers, we can have the following statement:

y = 2 ** 3 
print y

This will print 8 to the console, and has nice syntax as there is a built in "power" operator. Is it possible to overload "**" as a single operator in C++? Specifically, I want to accomplish something like this:

int a = 2;
int b = 3;
cout << (a**b) << endl;

or, if this isn't possible, something like this:

MyInt a = new MyInt(2); // Wrapper class around ints to play nicely with **
MyInt b = new MyInt(3);
cout << (a**b) << end; // Assume ostream overridden for MyInt

These should also print 8 to console. I understand that it would be much easier to override the "^" operator to do the same thing, but I'm mostly interested to see whether or not I can overload "**". Would the operator "*" (for the MyInt class case, if it were a member function) have to look to see if the argument was another "*", as I don't know of a way to specify "**" as a single operator? Is it even possible to pass an operator as an argument?

Additional/bonus stipulation if possible (as if I haven't already said enough): No macros!!!

like image 628
Matt Messersmith Avatar asked Mar 28 '16 22:03

Matt Messersmith


People also ask

Is overloading possible in C?

And C doesn't support Function Overloading.

Does C support function overriding?

The object-oriented programming languages which support function overloading include Java and C++. As the C compiler doesn't allow it to be used in the code hence, it isn't easy to implement function overloading in C. Yet; we can still achieve the same thing with some technique.

What is overloading in OOP?

Overloading. Method overloading is a form of polymorphism in OOP. Polymorphism allows objects or methods to act in different ways, according to the means in which they are used. One such manner in which the methods behave according to their argument types and number of arguments is method overloading.

What is function overloading in C with example?

Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is ...


2 Answers

You mean something like this?

#include <iostream>
#include <cmath>

struct MyInt {
    int val;
    struct MyProxy { int val; };

    MyProxy operator *() const{ return MyProxy{val}; }
    MyInt operator * (const MyProxy& b) { return MyInt{ static_cast<int>(std::pow(val, b.val)) }; }

};

std::ostream& operator << (std::ostream& o, const MyInt& m) { return o << m.val; }

int main(){
    MyInt a{5}, b{3};
    std::cout << a**b << std::endl;
}

See it live here http://coliru.stacked-crooked.com/a/ab56b9cd6e422e12

Explanation:

  1. Overload the unary * operator to return a proxy object
  2. Overload the binary * operator to use the proxy... Simple :-)
  3. I used a proxy class to avoid subtle bugs..

C++ is fun... I want to believe you are doing this for fun AND not in production... Cause its a very bad idea in production, just call std::pow

like image 86
WhiZTiM Avatar answered Oct 27 '22 01:10

WhiZTiM


Short answer: No. There is no way to "overload" arbitrary character/token sequences in C++.

Longer answer: a**b is equivalent to a * (*b), so you could overload both binary * (i.e. multiply) and unary * (i.e. dereference) in some gross way. But that would be completely opaque/unexpected to anyone reading your code, and painful to debug/maintain in future.

Just write a function called pow()!

like image 27
Oliver Charlesworth Avatar answered Oct 27 '22 00:10

Oliver Charlesworth