混合 C 和 C++、原始指针和(增强)共享指针
我正在使用 C++ 处理一些遗留的 C 代码。
我有一个数据结构(在初始化期间),制作指向传递给其初始化指针的 ptr 的结构的副本。
这是我正在尝试做的事情的简化 - 希望“简化”中没有丢失任何重要细节:
/* C code */
typedef struct MyData
{
double * elems;
unsigned int len;
};
int NEW_mydata(MyData* data, unsigned int len)
{
// no error checking
data->elems = (double *)calloc(len, sizeof(double));
return 0;
}
typedef struct Foo
{
MyData data data_;
};
void InitFoo(Foo * foo, const MyData * the_data)
{
//alloc mem etc ... then assign the STRUCTURE
foo.data_ = *thedata ;
}
C++ code
-------------
typedef boost::shared_ptr<MyData> MyDataPtr;
typedef std::map<std::string, MyDataPtr> Datamap;
class FooWrapper
{
public:
FooWrapper(const std::string& key) {
MyDataPtr mdp = dmap[key];
InitFoo(&m_foo, const_cast<MyData*>((*mdp.get())));
}
~FooWrapper();
double get_element(unsigned int index ) const {
return m_foo.elems[index];
}
private:
// non copyable, non-assignable
FooWrapper(const FooWrapper&);
FooWrapper& operator= (const FooWrapper&);
Foo m_foo;
};
int main(int argc, char *argv[])
{
MyData data1, data2;
Datamap dmap;
NEW_mydata(&data1, 10);
data1->elems[0] = static_cast<double>(22/7);
NEW_mydata(&data2, 42);
data2->elems[0] = static_cast<double>(13/21);
boost::shared_ptr d1(&data1), d2(&data2);
dmap["data1"] = d1;
dmap["data2"] = d2;
FooWrapper fw("data1");
//expect 22/7, get something else (random number?)
double ret fw.get_element(0);
}
本质上,我想知道的是:
从地图检索的数据与从地图检索的数据不同的原因是什么?存储在地图中的那个?
I am working in C++ with some legacy C code.
I have a data structure that (during initialisation), makes a copy of the structure pointed to a ptr passed to its initialisation pointer.
Here is a simplification of what I am trying to do - hopefully, no important detail has been lost in the "simplification":
/* C code */
typedef struct MyData
{
double * elems;
unsigned int len;
};
int NEW_mydata(MyData* data, unsigned int len)
{
// no error checking
data->elems = (double *)calloc(len, sizeof(double));
return 0;
}
typedef struct Foo
{
MyData data data_;
};
void InitFoo(Foo * foo, const MyData * the_data)
{
//alloc mem etc ... then assign the STRUCTURE
foo.data_ = *thedata ;
}
C++ code
-------------
typedef boost::shared_ptr<MyData> MyDataPtr;
typedef std::map<std::string, MyDataPtr> Datamap;
class FooWrapper
{
public:
FooWrapper(const std::string& key) {
MyDataPtr mdp = dmap[key];
InitFoo(&m_foo, const_cast<MyData*>((*mdp.get())));
}
~FooWrapper();
double get_element(unsigned int index ) const {
return m_foo.elems[index];
}
private:
// non copyable, non-assignable
FooWrapper(const FooWrapper&);
FooWrapper& operator= (const FooWrapper&);
Foo m_foo;
};
int main(int argc, char *argv[])
{
MyData data1, data2;
Datamap dmap;
NEW_mydata(&data1, 10);
data1->elems[0] = static_cast<double>(22/7);
NEW_mydata(&data2, 42);
data2->elems[0] = static_cast<double>(13/21);
boost::shared_ptr d1(&data1), d2(&data2);
dmap["data1"] = d1;
dmap["data2"] = d2;
FooWrapper fw("data1");
//expect 22/7, get something else (random number?)
double ret fw.get_element(0);
}
Essentially, what I want to know is this:
Is there any reason why the data retrieved from the map is different from the one stored in the map?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用map的 operator[] 插入元素会删除以前的数据存储在同一键上,请尝试使用 insert 代替。
与从地图获取数据相同,如果您的密钥未存储,operator[] 将创建一个元素,尝试使用 查找。
另外
const_cast((*mdp.get()))
会在 Mydata* 中转换 MyData,const_cast(mdp.get())
代码> 会很好...Using operator[] of map to insert element will erase the previous data stored at the same key, try using insert instead.
Same deal with getting data from map, operator[] will create an element if your Key is not stored, try using find instead.
Also
const_cast<MyData*>((*mdp.get()))
will cast a MyData in Mydata*,const_cast<MyData*>(mdp.get())
will be good ...