2017-07-19 23 views
-1

は、C++の 'new'演算子についての詳細な説明が必要です。'new'演算子の理解

int *n = new int(10); // initialize the pointer to integer as '10' 
cout << *n;   // o/p -> 10 

が、これは動作しません:以下の作品がなぜ は誰かが説明できますか?

int p = new int (10); // error: invalid conversion from 'int*' to 'int' [-fpermissive] 
+7

なぜ、2番目のものが動作すると思われますか? 'new'はポインタを返します。 – NathanOliver

+0

重複? https://stackoverflow.com/questions/2726356/the-new-operator-in-c-pointer-question – Twtheo

+0

[完全なC++ブックガイドとリスト](https://stackoverflow.com/questions/388242/the- – molbdnilo

答えて

0

ポインタに使用される新しい演算子、その種類 をint型ではない

int x = 5; 
    int *p = &x; 
    int *q = new int(4); 

、あなたは純粋なタイプ ように、クラス構造体とそれを使用することはできませんが、私はただのchar ..などをint型意味

これは新しいの宣言です:

void* operator new[] (std::size_t size); 
    void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept; 
    void* operator new[] (std::size_t size, void* ptr) noexcept; 

あなたはそれはあなたがやったことと同じであるC/C++ 書き込み

int x = p; // error : invalid conversion from 'int*' to 'int' 

に望めないので、ポインタの型がvoidを返すだ見るものとして。

0

それはエラーメッセージ自体...とは全く明らかである エラー:「INT」は成功した場合、「新しい」のポインタを返すので

に「int型*」から無効な変換。そのため、intに代入しようとするとエラーが発生します。