How to declare type conversion in header file and implement in cpp file?

it doesn't work for me.
I have a header file and a cpp file.
I need to define a conversion operator from my class to INT, but it gives me a "syntax error" when declaring it in the H file and implementing it in the cpp file. maybe i got the syntax wrong? in file H, I have "public":

operator int(); 

and in the cpp file I have:

 A::operator int() { return mNumber ;} 

If I implement a function in the H file, it works, but I do not want to do this.
can anyone help?

+4
source share
1 answer

I also wanted to separate the class declaration from the implementation. The critical missing ingredient was const :

 // Foobar.hpp class Foobar { public: Foobar() : _v(42) {} operator int() const; private: int _v; }; 

And then in the implementation file:

 #include "Foobar.hpp" Foobar::operator int() const { return _v; } 

See reference

+1
source

Source: https://habr.com/ru/post/1312461/


All Articles