开发者

Functions defined in header / cpp file behaves different

File: A.h

class A
{
public:
    struct x X;
    int show()
    {
        x.member_variable ? 0: -1;
    }
};

Now if A.cpp is complied which includes A.h (which is actually in a huge project space) we see that x.member_variable value is not as expected. But if remove the show() method and place it in A.cpp the code behaves fine - me开发者_开发技巧aning that x.member_variable value is correct.

How such a thing may happen - one thing we saw from objdump is that if the function is defined in A.h the the method is treated as inline function which otherwise is not if defined in A.cpp?

How the code can behave differently altogether?


Did you forget the return?

int show()
{
return x.member_variable ? 0: -1;
}

Without it, the code is still legal but the value returned will be somewhat random. There may happen to be a correlation between the behavior and where you put the code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜