473,671 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector<Foo> how to force default constructor

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

Jan 1 '06 #1
18 3116
We need to see some code.

Jan 2 '06 #2
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=(cons t FooBar &f);
~FooBar();
.......
}

int main()
{
Foo me;

FooBar b;
......

me.vec.push_bac k(b);
}

Jan 2 '06 #3
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.

Jan 2 '06 #4
It is related to push_back.

As I understand it, STL always insert a copy of the object. in this
case, a copy of "b".

First, it calls the (wrong) default constructor, then use the
assignment operator to assign the data inside "b" to the newly created
object, then put it into "vec".

Jan 2 '06 #5
That's right; a copy of "b" will be inserted into the vector. However,
I can't see your implementation of the FooBar() constructor. The bug
could be there. Your implementation of the copy constructor uses the
assignment operator. But I can't see your implementation of the
assignment operator either, so the bug could be there as well.
Nonetheless, The fact that you've attempted to define your own copy and
assignment constructor is curious. If you want it to exhibit the
default behavior, then don't define them at all. Otherwise, the bug is
probably there.

Jan 2 '06 #6

lc****@yahoo.co m wrote in message

class FooBar{
public:
FooBar(0;
FooBar(); // This should help
FooBar(const Foobar &f){*this=f; }
Foobar &operator=(cons t FooBar &f);
~FooBar();
.......
}


--
Bob R
POVrookie
Jan 2 '06 #7
There was a typo: it should be FooBar(), not FooBar(0, fogot the shift
key.

Anyway, I put break point in the default constructor and found it was
never called.

Jan 2 '06 #8

lc****@yahoo.co m wrote:
It is related to push_back.

As I understand it, STL always insert a copy of the object. in this
case, a copy of "b".
True, that is what logically happens.
First, it calls the (wrong) default constructor, then use the
assignment operator to assign the data inside "b" to the newly created
object, then put it into "vec".


That is apparently what your implementation does. However, part of the
vector<T> specification is that T must have proper object semantics.
Apparently, your FooBar doesn't, and thus vector<FooBar> doesn't work.

That said, I'm not 100% sure if push_back officially requires the
default ctor.
Some vector members do, some don't, and I'd have to look up the exact
rules. Personally, I just stick to "int semantics".

HTH,
Michiel Salters

Jan 2 '06 #9
"Apparently , your FooBar doesn't, and thus vector<FooBar> doesn't
work."

Its a typo, like the man said.

Lets get on with the answers.

Jan 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1658
by: Newsgroup - Ann | last post by:
Hi: I saw the following codes from the FAQ about the contiguous storage of vector. I am just wondering how does the implementation of the <vector> guarantee the contiguous? What if a vector v was defined first as a 5-element vector, and then was increased dramatically but the corresponding contiguous area has already been occupied by other objects? Maybe sounds stupid to you gurus, but it does confuse me:( Thanks. ...
1
2260
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector foo containing vectors of the size 3. I then want to compare a vector<double> of size 3 (coord) with foo to find a sequence of elements in foo that equals coord. However, when I try this it returns when just one of the element of coord equals one...
6
3774
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings; strings.push_back("abc"); strings.push_back("xyz"); strings.push_back("lmnop");
1
2406
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
20
17813
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
8
4862
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
2
3068
by: eb | last post by:
I have this working code : foo.h /* nothing */ foo.cpp ... std::vector<std::vector<T * my_T(board_size,board_size) ; for (i=0; i<board_size; i++)
1
2348
by: OriginalCopy | last post by:
This is a demonstrative code which could be used for debugging purposes. And yet I don't know how to insert the necessary data on line 63, purely syntactically speaking ? I'm a beginner with STL, and from what I've read insert() accepts references for almost all of its overloaded versions. If so, what could one do so those element's memory space doesn't wipe out? I suppose one should use the heap. In rest I hope the code speaks for itself, it's...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.