小编use*_*932的帖子

reinterpret_cast - 奇怪的行为

我遇到了与reinterpret_cast相关的奇怪错误.看看下面的代码:

int* var;
reinterpret_cast<void const **>(&var);
Run Code Online (Sandbox Code Playgroud)

VSC++ 2010中的错误:错误C2440:'reinterpret_cast':无法从'int**'转换为'const void**'

gcc 4.1.2中的错误:从'int**'类型的reinterpret_cast到'const void**'的类型转换为constness

gcc中的错误4.6.2:从类型'int**'的reinterpret_cast到'const void**'的类型转换掉了限定符

有没有人知道为什么编译器说我正在抛弃const.我,我的几个同事都不知道它有什么问题.

感谢帮助!

c++ casting const reinterpret-cast

4
推荐指数
1
解决办法
4296
查看次数

Emplace指向shared_ptr的多图的指针不起作用

矢量工作正常

Header
std::vector<std::shared_ptr<SceneNode>> subnodes_m;

Definition
void CompositeSceneNode::AddChild(SceneNode* subnode_p)
{
    subnodes_m.emplace_back(subnode_p);
}
Run Code Online (Sandbox Code Playgroud)

Multimap没有

Header
std::multimap<unsigned int, std::shared_ptr<SceneNode>> subnodes_m;

Definition
void CompositeSceneNode::AddChild(SceneNode* subnode_p, unsigned int layerIndex)
{
    subnodes_m.emplace(layerIndex, subnode_p);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

error C2664: 'std::pair<_Ty1,_Ty2>::pair(const unsigned int &,const _Ty2 &)' :
cannot convert parameter 2 from 'RendererD3DWrapper::SceneNode *'
to 'const std::shared_ptr<_Ty> &'   
Run Code Online (Sandbox Code Playgroud)

有人有线索吗?

c++ multimap shared-ptr c++11 emplace

4
推荐指数
1
解决办法
1045
查看次数

将智能指针传递给参考指针参数的函数

如何将智能ptr传递给参考指针作为参数的函数?

smart_ptr<T> val; // I have this smart pointer

// And I want to pass it to this function, so that this function will fill the smart pointer with proper value
void Foo(T*& sth)
{
    sth = memoryAddress;
}
Run Code Online (Sandbox Code Playgroud)

编辑现在我明白了.谢谢大家的所有答案!

c++ smart-pointers

3
推荐指数
1
解决办法
932
查看次数

使用emplace_back将元素添加到vector时,如何强制编译器使用默认构造函数?

使用emplace_back将元素添加到vector时,如何强制编译器使用默认构造函数?

#include <vector>
#include <utility>

enum E {E1, E2};

struct A
{
  A(){}
  A(int i){}
};

int main()
{
  std::vector<std::pair<E, A>> testMap;

  testMap.emplace_back(E1, 10);
  // testMap.emplace_back(E2);
}
Run Code Online (Sandbox Code Playgroud)

注释掉的行给出以下错误:错误C2664:'std :: pair <_Ty1,_Ty2> :: pair(const std :: pair <_Ty1,_Ty2>&)':无法将参数1从'E'转换为'const std :: pair <_Ty1,_Ty2>&'

c++ c++11

1
推荐指数
2
解决办法
148
查看次数