2

The following works but feels ugly when I do the (*this)(5) part.

struct MyStruct
{
    void operator()(int a)
    {
        // Do something with "a"
    }

    void myFunc()
    {
        (*this)(5);
    }
};

I will need to overload the () operator and use it from within other class methods.

  • `this` is a pointer, and [you can't overload the operators of pointer types](https://stackoverflow.com/questions/3871039/how-to-overload-operator-for-a-pointer-to-the-class) as they are not user-defined. If this kind of aesthetic wart bothers you, C++ might not be the language for you. Similar things are all over the place. `this` rightly should be a reference rather than a pointer, but the feature was added to the language before references were. – Karl Knechtel Nov 03 '21 at 18:12
  • @KarlKnechtel I don't get your point. Nowhere is the OP trying to overload or call the operator for the pointer this. He is clearly calling the operator on `*this` which is not a pointer. – bolov Nov 03 '21 at 18:15
  • @bolov Right, so if that's sufficient, then there is *not a reproducible problem* here and the question should be closed. But I don't think that's the case. OP, by my reading, wishes *not* to have to dereference `this` in order to invoke the operator. Hence the complaint that the valid syntax `(*this)(5)` "feels ugly". The *reason* we have to write `*this`, so as to "call the operator on ... not a pointer", is that `this` is a pointer (as I said). Hence the `*`. To dereference the pointer, and thus get the non-pointer referent. Yeah? – Karl Knechtel Nov 03 '21 at 18:16
  • @KarlKnechtel I don't know why the OP thinks that is ugly, I can only presume and I try as much as possible not to presume. Anyway there are other options without explicitly dereferencing the pointer this. – bolov Nov 03 '21 at 18:18
  • Yes. But `Bhoo` for doing it. I would call you out in a code review for doing that. Make `operator()` call a named function. Then your code can also call a named function and be self documenting. – Martin York Nov 03 '21 at 18:23
  • I'm not really experienced in C++ and I actively seek working solutions that are clean, not only working, hence the question. My real code is for a Matrix class, and I overload the `()` operator to do things like `A(2, 3) = 0.5` and so behind the `()` I convert the matrix indices 2 and 3 to a single index to obtain data from a 1D array. So having used calls like `A(2, 3) = ...` or `B(0, 1) = ...` to suddenly have to use a _seemingly_ odd `(*this)(4, 5) = ...` which didn't feel elegant. – Yaseen Ahmed Nov 03 '21 at 18:47
  • `operator()(x,y) {return at(x,y);} myFunc(x,y) {return at(x,y);}` https://stackoverflow.com/a/1971207/14065 – Martin York Nov 03 '21 at 22:28

1 Answers1

10

You have a few options:

  • (*this)(5)

  • this->operator()(5)

  • or just operator()(5)

  • Create a method that you call from within the operator(), e.g.:

    void do_work(int a) { /* ... */ }
    void operator()(int a) { do_work(a); }
    void myFunc() { do_work(5); }
    

Whichever you choose is just a matter of personal taste.


Just for fun, here are some more (ridiculous) options:

  • std::invoke (mentioned in comments):

    std::invoke(&MyStruct::operator(), this, 5);
    
  • mem_fn:

    auto call_op = std::mem_fn(&MyStruct::operator());
    call_op(this, 5);
    
  • lambda:

    auto call_op = [this] (int a) { operator()(a); };
    call_op(5);
    
  • bind_front

    auto call_op = std::bind_front(&MyStruct::operator(), this);
    call_op(5);
    
bolov
  • 72,283
  • 15
  • 145
  • 224