dynamic allocation of class object without constructor [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I dynamically allocate a class object that has no defined constructor?
I tried:
A * newPtr = new A();
But it's giving me some kind of memory leak on gdb.
Cheers!

If there's no defined constructor and standard rules allow it then you get a implicitly-declared default constructor, otherwise your code wouldn't compile at all.
So if you are not defining any constructor and your code compiles then for sure a default one is declared and defined (so you actually have a constructor).
You get a leak because you need to delete the pointer to free its memory from the heap.

Related

Use case of explicit deletion of move constructor in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I understand move constructor, but in some places I see move constructor is deleted. In what scenarios is deleting a move constructor is useful/makes sense?
Even if the the object needs to be singleton, we can delete the copy constructor but not the move constructor.
Its generally not needed, other than for purposes of being verbose. Just an example of "can doesnt mean should". In your example of deleting the copy constructor (or even just defined), the move constructor is implicitly deleted. In scenarios where this is done, you could imagine explicitly deleting the move constructor as well.

Detection of dynamic allocation with the new operator C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is new the only operator that allows us to detect dynamic memory allocation in C++?
I'm asking this because I want to release all dynamic memory allocations for the destructor of my class.
In modern C++ you keep heap references using std::shared_ptr and std::unique_ptr. They will free memory automatically when they are destroyed or when you manually reset() them.

Move constructor with vector of pointers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How should i do the move constructor for a vector with pointers?
for example :
I have this in class A as a field - vector<A*> AList;
In the function (A &&otherA) ( which is the move constructor) ,
should i write like this :
AList(std::move(other.AList)) or in an other way?
Yes, you can do it like that.
Or, if you don't need the move assignment operator to do anything else, just leave it out and the compiler will do this for you.
This goes for moving any member vector.

How one object of same class assign to the other object of the same class in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know that we can directly assign same class objects in C++,but what actually happen behind the scene?
There's something called "default copy-constructor" and "default assignment-operator". Unless you overload these methods in the class, the default behavior is that all non-static members of the class are copied one-by-one from the source to the target class.
A little more: This includes pointers, btw. Which is why you generally should overload these operators and follow the rule of three if you have pointers as members.

Scopes in blank function c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hy, my question is simple..
I have this function.
CPythonMessenger::CPythonMessenger(): m_poMessengerHandler(NULL)
{
}
What scope have and why is there since constructor is empty and also is not used m_poMessengerHandler(NULL) i want to say the function is not used anywhere is constructor.
I guess you are examining (or maybe it is your code) THIS.
CPythonMessenger is the default constructor for the class CPythonMessenger as you can see in the relative header file HERE. After the : you can invoke a method (or another constructor) that must be run when creating an object of CPythonMessenger type. In particular, it creates an instance of m_poMessengerHandler that is then used in several place in the other class methods.