2011-12-14 21 views
6

constポインタをintに宣言しますか?</p> <pre><code>int* p1; // pointer to int const int* p2; // pointer to constant int int* const p3; // constant pointer to int const int* const p4; // constant pointer to constant int </code></pre> <p>とDに:私たちは、次のしているC++では

int* p1;    // pointer to int 
const(int)* p2;  // pointer to constant int 
?? ?? ??    // constant pointer to int 
const(int*) p4;  // constant pointer to constant int 

constant pointer to intの構文は何ですか?

答えて

5

私はあなたがそれをシミュレートすることができると思います。

struct Ptr(T) 
{ 
    T* _val; 

    this(T* nval) const 
    { 
     _val = nval; 
    } 

    @property T* opCall() const 
    { 
     return cast(T*)_val; 
    } 

    alias opCall this; 
} 

void main() 
{ 
    int x = 1; 
    int y = 2; 
    const Ptr!int ptrInt = &x; 
    assert(*ptrInt == 1); 

    *ptrInt = y; // ok 
    assert(*ptrInt == 2); 
    assert(x == 2); 

    ptrInt = &y; // won't compile, good. 
} 
関連する問題