【发布时间】:2011-02-23 17:23:51
【问题描述】:
下面的 sn-p 给出警告:
[C++ Warning] foo.cpp(70): W8030 Temporary used for parameter '_Val' in call to 'std::vector<Base *,std::allocator<Base *> >::push_back(Base * const &)'
.. 在指示的行上。
class Base
{
};
class Derived: public Base
{
public:
Derived() // << warning disappears if constructor is removed!
{
};
};
std::vector<Base*> list1;
list1.push_back(new Base);
list1.push_back(new Derived); // << Warning on this line!
编译器是 Codegear C++Builder 2007。
奇怪的是,如果 Derived 的构造函数被删除,警告就会消失... 是我还是编译器?
编辑:我发现删除警告的唯一方法是类似于以下内容:
Derived * d;
list1.push_back(d = new Derived); // << No warning now...
【问题讨论】:
-
使用 gcc 对这段代码没有警告。
-
这可能不是全部代码。 Base 和 Derived 是否有构造函数(非编译器生成的)?
-
@MadKeithV - 给予或接受#include
,即所有代码。我从一个更复杂的案例开始,并将其简化为显示上述问题的基本内容。 -
您的问题出在编译器上。 BCC32有很多问题,我想这是另一个。
标签: c++ warnings c++builder