Topic: stl container using default cstr


Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/02/02
Raw View
thinker <thinker@infomall.co.il> wrote:
: Hi,

: I'm trying to use 2 stl containers (vector and list) on my defiend
: classed, but it's a pre-request that they have default ctor.

Not quite. You only need the default constructor if you
do something like this:

std::vector<YourClass> v(10); // (1)

since vector will use the default constructor to construct
the default value for the second argument of the constructor
vector<YourClass>::vector(size_type n, const T& value = T(),
                          const Allocator = allocator());

It is arguable whether (1) should have the same semantics as

std::vector<YourClass> v;
v.reserve(10);             // (2)

or the one it has now:

std::vector<YourClass> v;
v.resize(10);             // (3)

Personally, I think it should have the semantics of (2), since
that's what most people want when they write (1), and what most
people expact, until they get bitten by it. And it removes
most often encountered reason a vector needs a default constructor
for the contained type.  Unfortunately, we are stuck with (3), probably
for compatibility with build-in arrays.

: Why is
: that, and what can I do to prevent this (ie, I don't want other people
: to have access to default ctor because it has no meaning for both
: classes..)

Fortunately, you don't have to provide other people with the access
to your default constructor or even provide a default constructor
at all.

Solution one:

Don't use (1), (3) and the like. Use (2) + push_back()'s instead.
This has a disadvantage that your vector can't be const.

Solution two:

Just supply the required second argument:

std::vector<YourClass> v(10, YourClass(.....));

This has the disdavantage that all elements of the vector must be
identical initially, and if this is not what you want, it's waste
of time, and misleading to the readers of your code. Since most of
the time it is not what you want, your vector probably can't be
const either.

Solution three:

Write a [preferably Random Access] iterator, which will generate
YourClass'es as you go. Then use other constructor for vector:

std::vector<YourClass> v(YourClassIteratorBegin(), YourClassIteratorEnd());

This has none of the problems, mentioned above.

There are other solutions, but they are all just veriations of the
three above. I think.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: thinker@infomall.co.il (thinker)
Date: 1998/01/28
Raw View
Hi,

I'm trying to use 2 stl containers (vector and list) on my defiend
classed, but it's a pre-request that they have default ctor. Why is
that, and what can I do to prevent this (ie, I don't want other people
to have access to default ctor because it has no meaning for both
classes..)




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]