5

I know that this might sound like a stupid question, but why do I get an error which says something like "

cannot convert Object* to Object

" when I try to instantiate a new Object by using the statement "

Object obj = new Object();

"?

Am I to understand that the "new" keyword is reserved for pointers? Or is it something else?

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 10
    There's nothing from with `new`. Only with your understanding of it. –  Jan 17 '11 at 19:30
  • 8
    Nothing is wrong. C++ just has different semantics and you have to forget what you assumed you knew. Understanding C++ requires understanding where objects are stored (heap or stack). Then you understand why `new` necessarily returns a pointer. – Benoit Jan 17 '11 at 19:30
  • 4
    C++ is neither Java nor C#; there are differences, and you have identified one. – andand Jan 17 '11 at 19:34
  • 1
    Why does the title say C++, your question have C++, yet you tagged it C? – GManNickG Jan 17 '11 at 19:56
  • 5
    In any case, if you want to learn C++, you need to get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – GManNickG Jan 17 '11 at 19:58
  • @andand, +1 totally agree. To the poster please read up on pointers and references in C++. – Mike Bailey Jan 19 '11 at 01:23
  • Surprised to see 4 up votes for this question. – Arunmu Jan 20 '11 at 11:57
  • @delnan. Obviously, but what I was asking was why did the compiler react the way it did with new? – Michael M. Adkins Jan 30 '11 at 02:16

6 Answers6

38
Object* obj = new Object();

new always return pointer to object.

if you write just Object obj it means that obj will hold the object itself. If it is declared this way inside function then memory will be allocated on stack and will be wiped once you leave that function. new allocates memory on heap, so the pointer can be returned from function. Note that pointer can also point to local (stack) variable also.

Andrey
  • 59,039
  • 12
  • 119
  • 163
  • @Andrey You're lucky. [There's bored people around here, today](http://stackoverflow.com/questions/4716629/c-error-returning-a-char-array/4716656#4716656). You only got one downvote. ;-) – Linus Kleen Jan 17 '11 at 19:35
  • 1
    @Andrey: Wasn't me but if it were I'd imagine it were the `Object* ob...` vs `Object *ob...` notation I admit I was thinking about it...;) – FrustratedWithFormsDesigner Jan 17 '11 at 19:37
  • 5
    @FrustratedWithFormsDesigner it is matter of taste, i don't downvote if i don't like the notation. Personally i prefer `Object* ob` because it looks as a type. yeah i know that `Object* ob1, ob2` should be `Object *ob1, *ob2`. – Andrey Jan 17 '11 at 19:41
  • @goreSplatter i don't care about downvote, just want to know what is wrong. – Andrey Jan 17 '11 at 19:41
  • 4
    @FrustratedWithFormsDesigner: that's pretty idiomatic for C++ (as an old C fart the `T* p` convention annoys me, but the more C++ I do the more I understand why it became the convention). For me, that wouldn't be sufficient reason for a downvote. – John Bode Jan 17 '11 at 19:44
  • @John: You're right, it's probably not worth a downvote (though I've seen downvotes for even less than this), but that's the only thing I can see in there that might provoke it, and I mentioned it because I was thinking it, and maybe I wasn't the only one? – FrustratedWithFormsDesigner Jan 17 '11 at 19:49
  • @Andrey: Maybe someone thought that 20 upvotes was too many for such an obvious answer. – ThomasMcLeod Jan 17 '11 at 20:11
  • @ThomasMcLeod: Punitive downvoting? When the guy already has 19.4k rep? Not sure there's much point in that... – FrustratedWithFormsDesigner Jan 17 '11 at 20:17
  • 1
    Perhaps it was the use of stack and heap. It is only storage duration which is guaranteed by the C++ standard, things can be stored wherever the compiler wants as long as the semantics don't change. – James Greenhalgh Jan 17 '11 at 23:07
8

Since new returns a pointer, you ought to use

Object *obj = new Object();
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
6

Exactly. New creates an object on the heap, and returns a pointer to it.

Hilydrow
  • 918
  • 1
  • 6
  • 15
6

the new operator makes a pointer to a object

there for Object *obj = new Object() should work.

but just Object obj() constructs the object just fine but in stack space

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
Bram
  • 765
  • 1
  • 6
  • 14
6

Object obj;

is all you need. It creates the object obj.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
1

I would have simply left a comment, but apparently I need a certain rep to do so.

In Java, variables used to store Objects are implicitly pointers to the Object. So new works the same way in C++ as Java, but you're not made aware of it in Java. I am going to guess that's the reason for your confusion.

usm
  • 245
  • 2
  • 17