C ++中的内联运算符 [英] inline operators in C++

查看:48
本文介绍了C ++中的内联运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,亲爱的伙计们,

我在标头中有此声明:

Hello dear fellas,

I have this declaration in a header:

class Comparator
{
public:
    inline bool operator () (const SGEEmotion& left, const SGEEmotion& right) const;
};



.cpp文件中的实现.奇怪的是,这给了我这种方法一个无法解决的外部问题.

但是,如果我不将其标记为内联,那么一切都很好...这是否意味着它不能内联?这是否适用于所有运营商?没什么意义...

提前谢谢.

PS:我正在使用VS08!

更新:

好吧,现在我真的很困惑!这有效:



And an implementation in a .cpp file. Oddly enough, this gives me an unresolved external for this method.

However, if I don''t mark it as inline everything is fine... Does this mean it can''t be inline? Does this apply to all operators? It doesn''t make much sense...

Thanks in advance.

PS: I''m using VS08!

Update:

Ok, now I''m really confused! This works:

class SGEAction
{
    friend class SGEActionFactory;

public:
    class Comparator
    {
    public:
        inline bool operator () (const SGEAction& left, const SGEAction& right) const;
    };
};



这不是:



This doesn''t:

class SGEEmotion
{
public:
    class Comparator
    {
    public:
        inline bool operator () (const SGEEmotion& left, const SGEEmotion& right) const;
    };
};



认为实施不重要...我不知道了:



Didn''t think the implementation was important... I don''t know anymore:

bool SGEAction::Comparator::operator () (const SGEAction& left, const SGEAction& right) const
{ return left.id() < right.id(); }





bool SGEEmotion::Comparator::operator () (const SGEEmotion& left, const SGEEmotion& right) const
{ return left.id() < right.id(); }



没明白...



Don''t get it...

推荐答案

您已经仔细阅读了 了.

http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx [ ^ ]

内联关键字是语言的一部分.

实际发生的情况完全取决于编译器.
Have you read this carefully.

http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx[^]

The inline keyword is part of the language.

What actually happens is entirely compiler dependent.


1.内联关键字始终是程序员的请求.使任何函数内联都不会使其内联.由编译器决定是否将其内联,具体取决于如何刷新此函数(但这没有理由导致编译错误).

2.如果将函数的定义放在声明的头文件中,则该函数将被视为内联函数,并且编译器使其内联的机会也比其他方法要多.

3.如果编译器决定使函数内联,则函数中使用的所有类型都应可见.为此,我可以包含头文件或进行前向声明.

我建议您尝试仅在没有内联关键字的情况下将两个函数定义放在头文件中的声明点处,然后查看它是否有效.如果有,那么您可以尝试使用内联关键字(尽管这将是多余的)
1. inline keyword is always a request from programmer. making any function inline dos not makes it inline. it is upto the compiler to decide whether to make it inline or not depending on how this function is being refrenced( but that is no reason to give compile error).

2. If I put the definition of the function in the header file at the point of declaration then that function will be treated as inline and chances of compiler making it inline are also more than the other approach.

3. If the compiler decide to make the function inline then all the types being used in the functions should be visible. For this I can either include a header file or do a forward declaration.

I suggest you try to put both function definition in header file at the point of declaration only without inline keyword and see if it works. If it does then you can try using inline keyword(although it will be redundant)


这篇关于C ++中的内联运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆