This may seem really simple, but I don't know how to actually make a program that tells me this.
Asked
Active
Viewed 212 times
-2
-
2```new``` always gives you a pointer. – Jonathan S. May 10 '22 at 11:19
-
12"but I don't know how to actually make a program that tells me this." try `foo* f = new foo;` and `foo f = new foo;` only one of them is not a compiler error – 463035818_is_not_an_ai May 10 '22 at 11:24
-
time to get used to a good reference right from the start. I suggest this one: https://en.cppreference.com/w/cpp/language/new – 463035818_is_not_an_ai May 10 '22 at 11:26
-
5`#include
#include – Eljay May 10 '22 at 11:31struct CLASS {}; int main() { auto x = new CLASS; std::cout << typeid(x).name() << "\n"; }` -
2A [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is more useful to you at this point than a language reference. – molbdnilo May 10 '22 at 11:40
-
_I don't know how to actually make a program that tells me this._ https://wandbox.org/permlink/l8HtwzyWLP6D7Gx7 – Paul Sanders May 10 '22 at 12:58
2 Answers
2
As stated in comments, new
will always give you a pointer.
But as new
implies delete
, I strongly advise you to take a look at a concept call RAII, especially why it's so popular.

X99
- 905
- 10
- 24
1
Does "new CLASS" return CLASS or CLASS*
It returns a pointer to the type of the object that you're trying to allocate.
From new expression:
The new expression attempts to allocate storage and then attempts to construct and initialize either a single unnamed object, or an unnamed array of objects in the allocated storage. The new-expression returns a prvalue pointer to the constructed object or, if an array of objects was constructed, a pointer to the initial element of the array.

Jason
- 36,170
- 5
- 26
- 60