c++ - Correctly Initialize Map of Pointers - map<p*, p*> -
i'm pretty new c++ , looking related q/a on didn't find proper answer issue.
i'm struggling trying instantiate new map of pointers map<p1*, p2*>
.
so far, tried:
std::map<mytype*, mytype*> mapofvertices = new std::map<mytype, mytype>;
or:
std::map<mytype*, mytype*> mapofvertices = new std::map<mytype*, mytype*>;
but i'm getting:
error: conversion ‘std::map*’ non-scalar type ‘std::map’ requested std::map mapofvertices = new std::map;
i'm confused since managed initialize other objects like:
mytype* myobj = new mytype;
i'm sorry if question bit trivial. in advance.
you don't need allocate map object on heap, can create on stack.
std::map<mytype*, mytype*> mapofvertices;
that code sufficiently creates map. if, whatever reason, did want pointer map (which involves allocating on heap), you'd need change declaration to
std::map<mytype*, mytype*> * mapofvertices = new std::map<mytype*, mytype*>;
(note star between type , variable name)
based on code you've provided, , own admission, indeed novice c++, most experienced c++ programmers know work raw pointers when absolutely necessary, , use case not represent such case. strongly, recommend pick book. there's great listing here suggestions.
Comments
Post a Comment