473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3092
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=(const FooBar &f);
~FooBar();
.......
}

int main()
{
Foo me;

FooBar b;
......

me.vec.push_back(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.com wrote in message

class FooBar{
public:
FooBar(0;
FooBar(); // This should help
FooBar(const Foobar &f){*this=f;}
Foobar &operator=(const 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.com 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
<lc****@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
class FooBar
{
public:
FooBar(0;
This line shouldn't compile. I'm assuming you meant FooBar();
FooBar(const Foobar &f){*this=f;}
Foobar &operator=(const FooBar &f);
~FooBar();
.......
}

int main()
{
Foo me;

FooBar b;
......

me.vec.push_back(b);
}


This should do the following:

Call the FooBar default constructor to initialize b

Construct a new last element for me.vec by copying b into it.

So if you think that something else is happening, we need to see what the
default constructor for FooBar is doing, and also what your evidence is that
your program is not doing the right thing.
Jan 3 '06 #11
<lc****@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Anyway, I put break point in the default constructor and found it was
never called.


Please post a complete program that shows the problem without the need to
set breakpoints.
Jan 3 '06 #12
lc****@yahoo.com wrote:
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;}

This copy constructor is highly suspect. You do know that
constructors are mutually exclusive? Only one ever runs
for an object. You assign into an uninitaizized *this here.
Jan 3 '06 #13
lc****@yahoo.com wrote:
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's how vectors work. Your objects are free to be copied either
by the assignment operator or the copy constructor at various times
in the vector. The object MUST work in light of that.

A default constructor is not necessary. The few places where a
non-copy-initialized object is used the call has an additional
argument which is the "prototype" object to copy (it's just defaulted
to a default constructed object).

Your use of the term "wrong" with "default constructor" is confusing.
There is only one default constructor. If you have more than one,
your program is ill-formed.
Jan 3 '06 #14
lc****@yahoo.com wrote:
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.


Actually, std::vector never calls the default constructor for T during
push_back (nor any other operation AFAIK), just the copy constructor.

Mirek
Jan 4 '06 #15
>>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


AFAIK, vector does not require default constructor. The only place where
it is required is for some default arguments.

Mirek
Jan 4 '06 #16
> 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".


Incorrect. It just copy-constructs element into its target place (usual
implementation uses placement new). Default constructor is never called
(it is more optimal this way).

Mirek
Jan 4 '06 #17

Ron Natalie wrote:

[note: typos corrected]
class FooBar
{
public:
FooBar();
FooBar(const FooBar &f){*this=f;}
This copy constructor is highly suspect. You do know that
constructors are mutually exclusive? Only one ever runs
for an object. You assign into an uninitaizized *this here.


Actually this copy-constructor is valid. All the elements in "this"
have been initialised by this point.

Usually it is better though to implement operator= in terms of
copy-construction, not the other way round. That way you are safer if
an exception is thrown somewhere.

Jan 4 '06 #18
lc****@yahoo.com wrote:
FooBar(const Foobar &f){*this=f;}


Implement operator= in terms of the copy ctor, not the other way
around. The idiom used by Herb Sutter in Exceptional C++ item 13 (also
available as a GotW -- it's "Writing Exception-Safe Code, Part 6") is
just right. It goes like this:

Foo & operator=(Foo const& that) {
Foo temp(that); // Construct a temporary -- this is the only line
that can throw.
swap(temp); // Implement a no-throw swap and use it here
return *this;
}

I like the following terse variant, which takes advantage of
pass-by-value to construct the temporary:

Foo & operator=(Foo that) {
swap(that);
return *this;
}

Just to get a little crazy, if swap() returned *this rather than void,
you could make it even more terse:

Foo & operator=(Foo that) {
return swap(that);
}

But that's probably going overboard, and doesn't line up with
std::swap.

Luke

Jan 4 '06 #19

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

Similar topics

0
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...
1
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...
6
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;...
1
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
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; ...
8
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.