D
daveb
I'm trying to write some code that calls the constructors of STL
containers explicitly, and I can't get it to compile. A sample program
is below. One compiler complains about the last two lines (the map
constructor calls), saying that I'm trying to take the address of a
constructor. Another compiler complains about all four of the last
lines, just saying "invalid use of class". What is the correct syntax
for calling a container constructor explicitly?
You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.
Thanks for the help!
- Dave
#include <vector>
#include <map>
#include <cstdlib>
int
main()
{
typedef std::vector<int> VecType;
typedef std::map<int, int> MapType;
VecType v, *vp;
MapType m, *mp;
vp = (VecType *)malloc(sizeof(VecType));
mp = (MapType *)malloc(sizeof(MapType));
// call constructors
v.VecType::vector();
vp->VecType::vector();
m.MapType::map();
mp->MapType::map();
}
containers explicitly, and I can't get it to compile. A sample program
is below. One compiler complains about the last two lines (the map
constructor calls), saying that I'm trying to take the address of a
constructor. Another compiler complains about all four of the last
lines, just saying "invalid use of class". What is the correct syntax
for calling a container constructor explicitly?
You're probably wondering why I'm not calling "new" instead of malloc
and the constructor. It's because what I really need to do is to call
a proprietary memory allocation function (i.e., not malloc) and then
call a parameterized constructor on the returned space. So neither
"new" nor "placement new" will work for me.
Thanks for the help!
- Dave
#include <vector>
#include <map>
#include <cstdlib>
int
main()
{
typedef std::vector<int> VecType;
typedef std::map<int, int> MapType;
VecType v, *vp;
MapType m, *mp;
vp = (VecType *)malloc(sizeof(VecType));
mp = (MapType *)malloc(sizeof(MapType));
// call constructors
v.VecType::vector();
vp->VecType::vector();
m.MapType::map();
mp->MapType::map();
}