开发者

Declare class member to have internal linkage

Basically I have code which looks like this inside a header file:

class Bar;
class Foo
{
public:
   Bar GetBar();
};

class Bar
{
    F开发者_Python百科oo CreateFoo() {}
};

Bar Foo::GetBar()
{...}

The problem with this code is that as soon as the header is included in more then one file the linker will complain that there are multiple definitions of Foo::GetBar. However I can't put it inside the class definition where that would work, because Bar isn't defined at that point. I don't want to put it inside a seperate .cpp file, because the rest of the library I'm writing (which isn't that heavy weight anyway) is mostly templates I would have to place in a header anyway and it seems a bit anoying to require linking something else in only because I had to put one function outside the header.

So is there anyway to solve this problem without creating another .cpp file?


inline Bar Foo::GetBar()
{...}


You can declare this Foo::GetBar() function inline. I should solve the multiple definitions.


Make the function explicitly inline:

inline Bar Foo::GetBar()
{...}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜