2

Learning C++, I got an operator function in a class but don't know how to call it:

class Getbytes { public: Getbytes(); int operator() (unsigned char* C1, unsigned char* C2) {do something to C1 and C2 and return int; }; } main () { Getbyes myBytes; //Here, how to call the "operator() (unsigned char* C1, unsigned char*C2)"? myBytes?? } 
2
  • 1
    A function-call operator makes objects callable as functions, so you simply "call" the object like any other function. Commented Jan 26, 2020 at 12:23
  • 1
    Your main is lacking a return type. main must be declared with a return type and the return type must be int. You are also missing a semicolon after the closing } of the class definition. On the other hand the semicolon doesn't belong after a function definition. So the ; after the closing } of the operator() overload does not belong there. Commented Jan 26, 2020 at 12:23

2 Answers 2

4

You call it as myBytes(); or myBytes.operator(); if you want to be verbose about it.

Of course you also need to pass the arguments the function needs. Like myBytes("foo", "bar");

Sign up to request clarification or add additional context in comments.

Comments

2

You can call it like

Getbytes myBytes; unsigned char s1[] = "Hello"; unsigned char s2[] = "World"; myBytes( s1, s2 ); 

or

myBytes.operator()( s1, s2 ); 

If the operator does not change the object itself of the class Getbytes then it should be declared like

int operator() (unsigned char* C1, unsigned char* C2) const; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.