开发者

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.

need to define a conversion operator from my class to INT, but it gives me "syntax error" when declaring it in the H 开发者_JAVA百科file and implementing in the cpp file. maybe i got the syntax wrong? in the H file i have in "public":

operator int();

and in the cpp file i have:

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

if i implement the function in the H file it works, but i don't want to do that.

can anyone help?


I also wanted to separate the class declaration from the implementation. The critical missing ingredient was the 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 this reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜