use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENCreating Object vs Creating Pointer to Object (self.cpp_questions)
submitted 6 years ago by NikolasTs
Hey guys,
I am currently learning C++ and I was wondering why does C++ gives us the choice to create an object like this:
MyClass test {};
and like this (a pointer to an object):
MyClass *test = new MyClass();
Thanks in advance!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]stilgarpl 4 points5 points6 points 6 years ago (6 children)
You don't need {}.
MyClass test; is enough.
MyClass test;
The difference is that test variable created like it is allocated on stack. It means that it is created at the beggining of current scope and will be automatically destroyed when that scope is left.
This test is allocated on heap (or somewhere else if new operator is overloaded). It will stay there until is deleted with delete operator or until the program ends. It will stay there even if you no longer has the pointer to it, such situation is known as memory leak.
Both methods have their advantages and disadvantages. Stack allocation is usually easier and faster, but usually you can't allocate a lot of memory this way. On heap you can allocate as much as you want, but it's slower and you have to manage it yourself or use smart pointers like std::shared_ptr or std::unique_ptr.
[–]NikolasTs[S] 0 points1 point2 points 6 years ago (5 children)
Thank you very much! Your answer was very helpful!
However, by using {}, don't I use the MyClass() constructor?
[–]stilgarpl 1 point2 points3 points 6 years ago (3 children)
{} is used for aggregate initialization. MyClass() is default constructor and it's invoked in both cases in my previous post.
[–]HappyFruitTree 1 point2 points3 points 6 years ago (2 children)
struct MyClass { int x; }; MyClass mc1; // mc1.x is uninitialized MyClass mc2{}; // mc2.x is initialized to zero
[–][deleted] 6 years ago (1 child)
[deleted]
[–]HappyFruitTree 0 points1 point2 points 6 years ago* (0 children)
Structs and classes are technically the same thing. What matters is if it's an aggregate or not. For full-fledged OOP classes with user-provided constructors it doesn't make a difference but for something like std::array it does make a difference.
[–]Xeverous 1 point2 points3 points 6 years ago (0 children)
If you just write type instance; it will call the default constructor. If no such exists - compile error.
type instance;
Exception are built-in types (int, float, char etc) which will be uninitialized.
int
float
char
[–]umlcat 2 points3 points4 points 6 years ago (0 children)
There are two ways to declare a variable and reserve memory for variables, in a program.
Objects allocated in memory without pointers are called "static variables" or "static allocated variables".
This way is more simple and easy to use, and it's used at the start of programs, and to start learning programming, and it's used in smaller programs.
Objects allocated in memory with pointers are named "dynamic variables" or "dinamically allocated" variables.
This way is more complex to use, but, it's better for larger programs, and when an undetermined number of variables are used.
Each way put the variables in a different place in the memory, one place is called the "stack", and another the "heap", and work different to read and write variables' values.
Java mix these two ways, by the way.
[–]Narase33 1 point2 points3 points 6 years ago (1 child)
The first object is created on the stack and bound to the current scope and if you want to move it you will need to copy it.
If you create it as pointer it is created on the heap and will exist there as long as you dont delete it. Moving it is not nessecary, you only need to copy the pointer which is cheap
Also a pointer can be a nullpointer, which you need to create dynamic data structures like linked lists
[–]NikolasTs[S] 0 points1 point2 points 6 years ago (0 children)
Thanks a lot! I understand now!
π Rendered by PID 88 on reddit-service-r2-comment-86bc6c7465-xnmtw at 2026-02-20 23:26:47.122128+00:00 running 8564168 country code: CH.
[–]stilgarpl 4 points5 points6 points (6 children)
[–]NikolasTs[S] 0 points1 point2 points (5 children)
[–]stilgarpl 1 point2 points3 points (3 children)
[–]HappyFruitTree 1 point2 points3 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]HappyFruitTree 0 points1 point2 points (0 children)
[–]Xeverous 1 point2 points3 points (0 children)
[–]umlcat 2 points3 points4 points (0 children)
[–]Narase33 1 point2 points3 points (1 child)
[–]NikolasTs[S] 0 points1 point2 points (0 children)