矢量<富>如何强制默认构造函数 [英] vector<Foo> how to force default constructor

查看:60
本文介绍了矢量<富>如何强制默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个类Foo的向量。


当我执行push_back()时,我希望stl可以调用我为Foo写的默认构造函数

。但是,stl构成了自己的默认值,即用垃圾初始化的



有没有办法强制stl使用我的默认构造函数? />

谢谢

Hi,

I have a vector of class Foo.

When I do a push_back(), I expect stl to call the default constructor
I wrote for Foo. But instead, stl makes up its own default that is
initialized with garbage.

Is there a way to force stl to use MY default constructor?

Thanks

推荐答案

我们需要查看一些代码。
We need to see some code.


对不起,我输入了整个网络,然后网络发生故障。


class Foo

{

public:

vector< FooBar> vec;

.......

}


class FooBar

{

public:

FooBar(0;

FooBar(const Foobar& f){* this = f;}

Foobar& operator =(const FooBar& f);

~FooBar();

.......

}


int main()

{

Foo me;


FooBar b;

......


me.vec.push_back(b);

}

Sorry, I typed the whole thing then the network went down.

class Foo
{
public:
vector<FooBar> vec;
.......
}

class FooBar
{
public:
FooBar(0;
FooBar(const Foobar &f){*this=f;}
Foobar &operator=(const FooBar &f);
~FooBar();
.......
}

int main()
{
Foo me;

FooBar b;
......

me.vec.push_back(b);
}


这里我没有看到任何错误。在push_back之前,FooBar()构造函数被称为

为< b"调用.FooBar副本

构造函数是不必要的;这是默认行为。


很可能这个bug在你的代码中的其他地方。

I don''t see anything wrong here. The FooBar() constructor is called
automatically for "b" before push_back() is called. The FooBar copy
constructor is unnecessary; that''s the default behavior.

Most likely the bug is somewhere else in your code.


这篇关于矢量&lt;富&GT;如何强制默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆