I am trying something like this -

//creation of multimap
multimap <int, string> prioritized_list;
//declared pointer for multimap
multimap <int, string>::const_pointer plist_pointer;
//tried to create instance of multimap.
plist_pointer=new prioritized_list;

I have to create new instance of multimap in a function, that needs to be called multiple times.

I am new to C++. Please let me know if I am considering anything wrong.

有帮助吗?

解决方案

You most likely don't want to use new. Unlike in languages like Java, the new keyword is not needed for every object creation in C++, and unlike in many other languages, objects created with new are not automatically destroyed when you no longer need them. You instead have to delete them manually with delete.

Also, new does not return the newly created object itself but a pointer to it, allowing you to do nasty things like storing the pointer, later deleting the object and then accidentally attempting to access it through the stored pointer, which may crash the program.

All of this creates endless problems and bugs. Especially if you are a newbie (or if you gain more experience and think you can finally handle the complexity :)).

In C++, normal object creation doesn't require new. It is much simpler. Your first line is already object creation:

multimap <int, string> prioritized_list;

No need for new here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top