What Is the Default Constructor for C++ Pointer

What is the default constructor for C++ pointer?

It'll create a NULL (0) pointer, which is an invalid pointer anyway :)

call the default constructor for the pointer of class as a value of map in c++

No, there is no way to make value initialised pointer to be a valid pointer to object, and that is what you will always get for objects created by std::map::operator[]. Such pointer is always null.

Instead of storing a pointer to node, you could store the node itself in the map element. The syntax of accessing the elements would be different, but you would achieve what you want otherwise:

map<int,Node> dict;
dict[i].val = i; // map now contains a node

C++ default constructor does not initialize pointer to nullptr?

Pointers are "POD types"...a.k.a. "Plain Old Data". The rules for when and where they are default-initialized are summarized here:

Default initialization of POD types in C++

So no. It doesn't matter what your constructor for a class is, if it's a raw pointer as a member of the class. You aren't actually instantiating the class. So members like Foo * or std::vector<Foo> * or anything ending in * will not be initialized to nullptr.

The smart pointer classes are not POD. So if you use a unique_ptr<Foo> or a shared_ptr<Foo> that is creating instances of classes, that do have a constructor that makes them effectively null if you do not initialize them.

Does it matter if I do MyCLass* o = new MyCLass; or I do MyCLass* o = new MyCLass(); in C++11?

One question per question, please.

Do the parentheses after the type name make a difference with new?

What's the different between pointer new and default constructor in Qt?

It is because in the first snippet, the object gets placed on the stack, so it will be destructed once it goes out of scope.
The new keyword places it on the heap. This means that it will live unil you delete it again, or exit the app. So you have to think about this, because if you don't make sure your object gets deleted, you have a memory leak.

These days there are plenty of smart pointers (also from Qt) out there that will delete your object for you.
Qt also will delete children of objects that get deleted, which can also help you manage your memory.

This is a very important bit of c++ to understand, so you shouldn't try to learn it all from a stackoverflow answer :)


Without knowing your app, i am going to guess that the best option for you here is, is just to add the widget as a member variable of the main window, and just call show() when you need it.
This means that it is constructed at the same time as MainWindow.

Passing a class object pointer to default constructor (C++ Subroutines)

Either make your constructor take Sub * instead of Sub &, or make an additional constructor that takes no Sub & and sets parent to nullptr internally.

Struct pointer (address), and default constructor


Does taking address of a C# struct cause default constructor call?

No. It just circumvents the compiler check.

The "use of possibly unassigned field" is a nicety to protect you against yourself. But it can easily be worked around. And in this case it does not seem so critical.

PS: The memory seems to be zero-initialized anyway.

Yes, that will almost always (see below) be the case in .NET, making the "default constructor call" question a bit academic. What happens to your memory is not so tightly coupled to the compiler warning.

C++ default constructor, initializing pointer with new object

That's because you are trying to delete too much:

  • you are deleting a non-allocated object in the second constructor (remove delete yc;)
  • you are trying to delete a stack-allocated object, b. delete a; will try to delete a pointer to b, which is an object on the stack; what happens depend on your OS (I expect an exception/core dump/whatever)

EDIT: another problem I spotted..
a->setMyClass(NULL)

I would suggest:

  • this post on smart pointers
  • this blog post on RAII
  • any C/C++ primer explaining stack vs. heap allocation (static vs. dynamic?)

Will the compiler generated default constructor initialize the pointers in std::array to nullptr?

To quote cppreference on the constructor of std::array:

initializes the array following the rules of aggregate initialization
(note that default initialization may result in indeterminate values
for non-class T)

By declaring your variable like std::array<Node *, 100> children; you invoke the default constructor. And, according to the rules of initialization, PODs (int, char, double, pointers, ...) are not default initialized. So no, your array will not be initialized with nullptr if you don't use aggregate initialization.

Aggregate initialization

std::array<Node *, 100> children;

invokes the default constructor, but no aggregate initializer is given so aggregate initialization won't happen. However

std::array<Node *, 100> children{}
std::array<Node *, 100> children = {};

not only invokes the default constructor, but also performs an aggregate initialization. In this case, the aggregate {} is just empty. And, following the rules of aggregate initialization, if there are less initializers than data members, every uninitialized member will be default initialized. So

Node x;
std::array<Node *, 100> children = {&x};

for example, will initialize the first array element with the pointer to x and every successive element will be default initialized to nullptr.



Related Topics



Leave a reply



Submit