0

私はインターネットで C++ のインライン化について多くのことを検索しましたが、誰もが別の実装方法を好むようです。

私の問題は次のようになります。

// header-file
class Test {
    int i;
    public:
        int getI();
};
// source-file
int Test::getI() { return i; }

この関数getI()は数千回呼び出されるため、この関数を「インライン化」すると便利だと思います。そうする最善の方法は何ですか:

// 1) define the function within the class-definition
class Test {
    int i;
    public:
        int getI() { return i; }
};

// 2) define the function within the header-file
inline int Test::getI() { return i; } // directly located under class-definition

// 3) let the fct-definition stay in the source file and write "inline" before it (somehow this does not compile)

どの方法が最良または最もパフォーマンスの高い実装であるかのヒントを教えてください。手伝ってくれてありがとう :)

4

1 に答える 1

0

1と2は同じです。実際にインラインで呼び出すかどうかは、コンパイラに完全に依存しています。クラス内で複雑な「インライン」関数を定義する場合、インラインで記述してもインラインであるとは限りません。したがって、要するに、コンパイラに依存します。

于 2016-12-05T07:14:53.697 に答える