40

リファクタリングを見つけて置き換えた後、私はこの宝石に行き着きました:

const class A
{
};

「constclass」とはどういう意味ですか?コンパイルは問題ないようです。

4

5 に答える 5

51

そのconst例ではこれは無意味であり、コンパイラはエラーを返すはずですが、これを使用してクロージング}との間にそのクラスの変数を宣言すると、;それらのインスタンスは次のように定義されますconst


const class A
{
public:
    int x, y;
}  anInstance = {3, 4};

// The above is equivalent to:
const A anInstance = {3, 4};
于 2008-10-16T00:26:57.803 に答える
35

「constclass」とはどういう意味ですか?コンパイルは問題ないようです。

私にとってはそうではありません。あなたのコンパイラは丁寧でそれを無視しているだけだと思います。

編集:うん、VC ++は黙ってconstを無視し、GCCは文句を言う。

于 2008-10-16T00:26:15.293 に答える
22

あなたがこれを持っていた場合:

const class A
{
} a;

その場合、それは明らかに「a」が定数であることを意味します。そうでなければ、それはおそらく無効なc++だと思います。

于 2008-10-16T00:25:37.650 に答える
9

次の例のように、後でクラスのインスタンスを宣言しない限り、意味がありません。

const // It is a const object...
class nullptr_t 
{
  public:
    template<class T>
      operator T*() const // convertible to any type of null non-member pointer...
    { return 0; }

    template<class C, class T>
    operator T C::*() const   // or any type of null member pointer...
    { return 0; }

  private:
    void operator&() const;  // Can't take address of nullptr

} nullptr = {};

nullptrC++0xを待っている場合の暫定的な実装。

于 2009-10-24T04:25:37.247 に答える