By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

Copy Constructor for an Array Element ???

P: n/a
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//

#include <stdio.h>
#include <stdlib.h>

class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};

int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC); // Compiles and Executes Correctly
}
free(Temp);
return 0;
}

Jul 22 '05 #1
Share this Question
Share on Google+
26 Replies


P: n/a
"Peter Olcott" <ol****@worldnet.att.net> wrote...
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
The latter. Constructors do not have names, therefore they cannot
be found during name lookup and hence cannot be _called_. They can
only be _invoked_ during construction of the object.

Given this situation, you'd be better off using _placement_new_
(read about it in a good C++ book).
//

#include <stdio.h>
#include <stdlib.h>

class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};

int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC); // Compiles and Executes Correctly
}
free(Temp);
return 0;
}


V
Jul 22 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message:
"Peter Olcott" <ol****@worldnet.att.net> wrote...
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?


The latter. Constructors do not have names, therefore they cannot
be found during name lookup and hence cannot be _called_. They can
only be _invoked_ during construction of the object.

Given this situation, you'd be better off using _placement_new_
(read about it in a good C++ book).


I'd recommend using an assignment operator or a member function assign
(like std library containers) instead of placement new, unless you are
absolutely sure you need it.

And if you do use placement new to constuct an object on top of an
existing object, make sure to call the objects destructor first
(unless you are absolutely sure it's not necessary.)

Jonathan
Jul 22 '05 #3

P: n/a
Peter Olcott wrote:
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//

#include <stdio.h>
#include <stdlib.h>

class X { private:
// representation int Data;
public:
X(void): Data(56) { } // default
X(const X& Y): Data(Y.Data) { } // Copy Constructor
// void Construct(const X& Y) { Data = Y.Data; }; // too late. *this already constructed
X& modify(const X& Y) { Data = Y.Data; return *this; } };

int main(int argc, char* argv[]) {
X ABC;
X* Temp = (X*)malloc(sizeof(X)*10); // you shouldn't do this
for (int N = 0; N < 10; ++N) { Temp[N] = ABC; // uses default assignment
Temp[N].modify(ABC); }
free(Temp);
return 0;
}


Jul 22 '05 #4

P: n/a
> I'd recommend using an assignment operator or a member function assign
(like std library containers) instead of placement new, unless you are
absolutely sure you need it.
I just created a std::vector class for antique (pre-template) C++ compilers.
My intention was to implement this within minimal execution time.
Apparently, it seems that even the modern std::vectors must use a kludge
to initialize their members. They seem to require default construction and
then assignment, rather than the single integrated step of copy construction.
Hopefully this shortcoming will by addressed in the continuing evolution
of C++.
And if you do use placement new to constuct an object on top of an
existing object, make sure to call the objects destructor first
(unless you are absolutely sure it's not necessary.)

Jonathan

Jul 22 '05 #5

P: n/a
"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:ZP********************@bgtnsc05-news.ops.worldnet.att.net...
I'd recommend using an assignment operator or a member function assign
(like std library containers) instead of placement new, unless you are
absolutely sure you need it.
I just created a std::vector class for antique (pre-template) C++

compilers. My intention was to implement this within minimal execution time.
Apparently, it seems that even the modern std::vectors must use a kludge
to initialize their members. They seem to require default construction and
then assignment, rather than the single integrated step of copy

construction.

[snip]

Well, not really:

std::vector<int> a;
a.reserve(3);
a.push_back(2);
a.push_back(4);
a.push_back(-2);

reserve just allocates enough space and a copy constructor is used for each
element. No default constructor and no assignment. The only inefficiency
that I know of is that the number of push_back's is counted.
Jul 22 '05 #6

P: n/a
> > class X {
private:
// representation
int Data;
public:
X(void): Data(56) { } // default
X(const X& Y): Data(Y.Data) { } // Copy Constructor
// void Construct(const X& Y) { Data = Y.Data; }; // too late. *this already constructed


I don't think so. I think that the example might be the only
way to copy construct array elements.
X& modify(const X& Y) { Data = Y.Data; return *this; }
};

int main(int argc, char* argv[]) {
X ABC;
X* Temp = (X*)malloc(sizeof(X)*10); // you shouldn't do this
for (int N = 0; N < 10; ++N) {

Temp[N] = ABC; // uses default assignment
Temp[N].modify(ABC);
}
free(Temp);
return 0;
}

Jul 22 '05 #7

P: n/a
> std::vector<int> a;
a.reserve(3);
a.push_back(2);
a.push_back(4);
a.push_back(-2);

reserve just allocates enough space and a copy constructor is used for each
element. No default constructor and no assignment. The only inefficiency
that I know of is that the number of push_back's is counted.

Try the same sort of example with a class that does memory allocation,
such that a deep copy (rather than bitwise) is required.

Jul 22 '05 #8

P: n/a

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:nf********************@bgtnsc05-news.ops.worldnet.att.net...
std::vector<int> a;
a.reserve(3);
a.push_back(2);
a.push_back(4);
a.push_back(-2);

reserve just allocates enough space and a copy constructor is used for each element. No default constructor and no assignment. The only inefficiency that I know of is that the number of push_back's is counted.
Try the same sort of example with a class that does memory

allocation, such that a deep copy (rather than bitwise) is required.


Cy is correct.A vector obtains unitialized memory from an allocator,
and initilizes each element, when appropriate, then initializes them
using copy constructors with placement new. A default constructor is
not even required to exist. (See 20.1.4).

Of course, if the copy constructor is expensive, then push_back will
be too. There's no way to avoid this.

Jonathan
Jul 22 '05 #9

P: n/a

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c1*************@ID-216073.news.uni-berlin.de...

Cy is correct.A vector obtains unitialized memory from an allocator,
and initilizes each element, when appropriate, then initializes them
using copy constructors with placement new. A default constructor is


This came out garbled. It should say:

A vector obtains unitialized memory from an allocator, then initilizes
each element, when appropriate, using copy constructors with placement
new

Jonathan
Jul 22 '05 #10

P: n/a
On Thu, 26 Feb 2004 01:49:24 GMT, "Peter Olcott"
<ol****@worldnet.att.net> wrote:
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
It does, via "placement new". See below.
//

#include <stdio.h>
#include <stdlib.h>

class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};

int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC); // Compiles and Executes Correctly
new (Temp + N) X(ABC); //placement construction
}
free(Temp);
return 0;
}


Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #11

P: n/a
> > Cy is correct.A vector obtains unitialized memory from an allocator,
and initilizes each element, when appropriate, then initializes them
using copy constructors with placement new. A default constructor is


This came out garbled. It should say:

A vector obtains unitialized memory from an allocator, then initilizes
each element, when appropriate, using copy constructors with placement
new

Jonathan

I placed messages in every constructor and destructor of my class,
and had std::vector resize() itself. In both of my compilers the number
of elements constructed was one more than I asked for. This seems to
indicate that the compiler is somehow created an extra temporary
object.

I will look into this <placement new> more deeply, though.
Jul 22 '05 #12

P: n/a

"tom_usenet" <to********@hotmail.com> wrote in message
news:44********************************@4ax.com...
On Thu, 26 Feb 2004 01:49:24 GMT, "Peter Olcott"
<ol****@worldnet.att.net> wrote:
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?


It does, via "placement new". See below.
//

#include <stdio.h>
#include <stdlib.h>

class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};

int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC); // Compiles and Executes Correctly


new (Temp + N) X(ABC); //placement construction


None of my three compilers would accept this.
}
free(Temp);
return 0;
}


Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Jul 22 '05 #13

P: n/a
On Thu, 26 Feb 2004 13:02:27 GMT, "Peter Olcott"
<ol****@worldnet.att.net> wrote:
new (Temp + N) X(ABC); //placement construction


None of my three compilers would accept this.


What was the error?

You need #include <new> at the top (or possibly <new.h> for
pre-standard compilers). Then it should be accepted by any recent
compiler, and certainly MSVC6+, GCC 2.95+, etc.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #14

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<40************@jpl.nasa.gov>...
Peter Olcott wrote:
Of the 21 lines in Peter Olcott's posted program, only 6 were quoted
unmodified by Trollsdale.

If we ignore whitespace modifications, 10 lines were quoted unmodified
(less than half of the posted program). Is this a record?

Not to mention the fact that all of the supplied
modifications are wrong or irrelevant, and not helpful.
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//

#include <stdio.h>
#include <stdlib.h>

class X {

private:
// representation
int Data;
public:
X(void): Data(56) { } // default
X(const X& Y): Data(Y.Data) { } // Copy Constructor
// void Construct(const X& Y) { Data = Y.Data; };

// too late. *this already constructed
X& modify(const X& Y) { Data = Y.Data; return *this; }
};

int main(int argc, char* argv[]) {
X ABC;
X* Temp = (X*)malloc(sizeof(X)*10); // you shouldn't do this
for (int N = 0; N < 10; ++N) {

Temp[N] = ABC; // uses default assignment
Temp[N].modify(ABC);
}
free(Temp);
return 0;
}

Jul 22 '05 #15

P: n/a
> You need #include <new> at the top (or possibly <new.h> for
pre-standard compilers). Then it should be accepted by any recent
compiler, and certainly MSVC6+, GCC 2.95+, etc.


Okay Great, now two out of three compilers accept this, and
function correctly (invoking the object's copy constructor).

The problem is that I need it to work on ant AT&T 2.0, pre-template,
circa 1990 compiler. My whole purpose is to create std::vector for my
antique compiler.

I want to have a development machine that is reasonably functional
and will fit in my pocket. The second requirement eliminates all
notebook computers and laptops. The first requirement
(when combined with the second requirement) eliminates
most everything besides HP Jornada, 690, 720, and 728.

None of these machines are Intel compatible, thus by themselves will not
run any compiler. When you add PocketDOS, you can now run any
software that runs on an 8088, this includes Borland Turbo C++ 1.0.
It runs on the Jornada's from 2.5 to 7.5 times faster than an 8088.

The major shortcoming of this old compiler is that lack of std::vector,
so I wrote one. Now All I need to do is to find the best way to copy
construct array elements. Do you have any ideas besides <placement new> ?


Jul 22 '05 #16

P: n/a

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:QZ*********************@bgtnsc04-news.ops.worldnet.att.net...
Cy is correct.A vector obtains unitialized memory from an allocator, and initilizes each element, when appropriate, then initializes them using copy constructors with placement new. A default
constructor is
This came out garbled. It should say:

A vector obtains unitialized memory from an allocator, then initilizes each element, when appropriate, using copy constructors with placement new

Jonathan
I placed messages in every constructor and destructor of my class,
and had std::vector resize() itself. In both of my compilers the

number of elements constructed was one more than I asked for. This seems to
indicate that the compiler is somehow created an extra temporary
object.


Was the default constructor ever called by the implementation? It
shouldn't be -- that was my point.

Jonathan
Jul 22 '05 #17

P: n/a
> Was the default constructor ever called by the implementation? It
shouldn't be -- that was my point.

Jonathan

tom_usenet gave the the complete sytax and instructions on how
to use <placement new>. It worked perfectly on two of my
three compilers. It did not work on the antique compiler that
I am developing std::vector for. I need another solution because
this is the compiler where it needs to work. Pre-template
AT&T 2.0 Borland Turbo C++ 1.0, circa 1990.
Jul 22 '05 #18

P: n/a

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:WB*********************@bgtnsc05-news.ops.worldnet.att.net...
Was the default constructor ever called by the implementation? It
shouldn't be -- that was my point.

Jonathan

tom_usenet gave the the complete sytax and instructions on how
to use <placement new>. It worked perfectly on two of my
three compilers. It did not work on the antique compiler that
I am developing std::vector for. I need another solution because
this is the compiler where it needs to work. Pre-template
AT&T 2.0 Borland Turbo C++ 1.0, circa 1990.


I looked more carefully at your original post. I see the problem
you're trying to solve is constructing objects on uninitialized
memory. (Sorry I didn't read it more carefully the first time -- I
mostly noticed the unusual syntax.)

Placement new is really the only portable way to do this. :(. Since
you're developing for a specific broken compiler, though, anything
which works, based on knowledge of the implementation, is fair game.

I tend to agree with Francis Glassborow, though. I don't think there's
much point trying to use C++ with such an old compiler.

Jonathan
Jul 22 '05 #19

P: n/a
On Fri, 27 Feb 2004 00:52:31 GMT, "Peter Olcott"
<ol****@worldnet.att.net> wrote:
You need #include <new> at the top (or possibly <new.h> for
pre-standard compilers). Then it should be accepted by any recent
compiler, and certainly MSVC6+, GCC 2.95+, etc.


Okay Great, now two out of three compilers accept this, and
function correctly (invoking the object's copy constructor).

The problem is that I need it to work on ant AT&T 2.0, pre-template,
circa 1990 compiler. My whole purpose is to create std::vector for my
antique compiler.

I want to have a development machine that is reasonably functional
and will fit in my pocket. The second requirement eliminates all
notebook computers and laptops. The first requirement
(when combined with the second requirement) eliminates
most everything besides HP Jornada, 690, 720, and 728.

None of these machines are Intel compatible, thus by themselves will not
run any compiler. When you add PocketDOS, you can now run any
software that runs on an 8088, this includes Borland Turbo C++ 1.0.
It runs on the Jornada's from 2.5 to 7.5 times faster than an 8088.

The major shortcoming of this old compiler is that lack of std::vector,
so I wrote one. Now All I need to do is to find the best way to copy
construct array elements. Do you have any ideas besides <placement new> ?


If you have a copy of the ARM (annotated reference manual), I expect
that will present a pre-standard syntax that placement new replaced.
ARM presents C++ as implemented in CFront 3.0...

I don't have a copy of the book. You could try this:

#include <stdlib.h>
#include <stddef.h>

void* operator new(size_t size, void* ptr)
{
return ptr;
}

void* operator new[](size_t size, void* ptr)
{
return ptr;
}

class Foo{};

int main()
{
void* mem = malloc(sizeof(Foo));
Foo* p = new(mem) Foo();
}

That may or may not work...

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #20

P: n/a
I looked more carefully at your original post. I see the problem
you're trying to solve is constructing objects on uninitialized
memory. (Sorry I didn't read it more carefully the first time -- I
mostly noticed the unusual syntax.)

Placement new is really the only portable way to do this. :(. Since
you're developing for a specific broken compiler, though, anything
which works, based on knowledge of the implementation, is fair game.

I tend to agree with Francis Glassborow, though. I don't think there's
much point trying to use C++ with such an old compiler.

Jonathan

It is my ONLY possible choice within my goal of having a reasonably
functional development machine that will fit in my pocket. ** I have spent
twelve thousand hours working on my softare invention. I would like
to do some of this work without the restriction of staying home, or lugging
around a laptop in a knapsack. In terms of getting some of the basic
ideas needed for my invention worked out, this antique compiler will
suffice. Its major shortcoming is the lack of a std::vector. I have since
created a very efficient std::vector that impements all of the subset of
functionality that I will ever need. http://home.att.net/~olcott/std_vect.html

I guess that I will just have to retain my current method of requiring
all classes that declare their own constructors, and destructors, to
duplicate this functionality in member functions named:
Destruct()
Construct(()
Construct(&)

** The ONLY reasonably functional development machine that will
fit in one's pocket is an HP Jornada 690, 720, or 728. Every other
machine either will not fit in the pocket, or is not reasonably functional.
Having to type with a stylus is not reasonable. The ONLY way that
this machine can be used for C++ development is by adding PocketDOS
(and 8088 emulator), and Borland Turbo C++ 1.0 (Currently freeware).
Jul 22 '05 #21

P: n/a

"Peter Olcott" <ol****@worldnet.att.net> wrote in message news:QZl%b.45171$aH3.1403659@bgtnsc04-
I placed messages in every constructor and destructor of my class,
and had std::vector resize() itself. In both of my compilers the number
of elements constructed was one more than I asked for. This seems to
indicate that the compiler is somehow created an extra temporary
object.


Resize and reserve do different things.
Reserve just allocates space (possibly moving existing objects in the process).
Resize increases the size() of the vector. In addition to copying the existing
elements, it also needs to fill the elements above the existing size with it's second
argument (which is by default a default constructed object, but you could pass
in anything).

Resize(10) should therefore be expected to call a default constructor once and
ten copy constructors (to either move an existing element or to copy the default
constructed prototype).

Jul 22 '05 #22

P: n/a
> Resize and reserve do different things.
Reserve just allocates space (possibly moving existing objects in the process). It is this "possibly moving objects in the process" that I need to be able
to have a copy constructor for an array element. With modern compilers
this can be easily accomplished with <placement new>. This feature is
not available on AT&T 2.0, which is my required development platform.
Got any good ideas on the best way to do this?
Resize increases the size() of the vector. In addition to copying the existing
elements, it also needs to fill the elements above the existing size with it's second
argument (which is by default a default constructed object, but you could pass
in anything).

Resize(10) should therefore be expected to call a default constructor once and
ten copy constructors (to either move an existing element or to copy the default
constructed prototype).


Jul 22 '05 #23

P: n/a
> If you have a copy of the ARM (annotated reference manual), I expect
that will present a pre-standard syntax that placement new replaced.
ARM presents C++ as implemented in CFront 3.0...

I don't have a copy of the book. You could try this:

#include <stdlib.h>
#include <stddef.h>

void* operator new(size_t size, void* ptr)
{
return ptr;
}

void* operator new[](size_t size, void* ptr)
{
return ptr;
}

class Foo{};

int main()
{
void* mem = malloc(sizeof(Foo));
Foo* p = new(mem) Foo();
}

That may or may not work...

Below is the "official" work-around. I also included the usenet URL where this material
was found. I could not get this work-around to work. Can you see why this would not
make the syntax that you provided function correctly?

//######### ######### ######### ######### ######### ######### ######### #########
//######### Bug report version 1.01.6 and earlier starts here #########
//######### ######### ######### ######### ######### ######### ######### #########
//
// Severity: Missing header file
// Workaround: Provided below
// Versions: Occurs in both TC++ 1.00 and 1.01
// Fixed in: Fixed in 2.0

// There is no <new.h>. I constructed the following work-alike. It should go
// into your standard include directory (where <iostream.h> is, for example):
// new.h
// Author: Dr. Marshall Cline/ECE Dept/Clarkson Univ/Potsdam,NY 13676
// Email: cl***@sun.soe.clarkson.edu
// Phone: Voice: 315-268-6511; Fax: 315-268-7600
// Copyright: The Author releases this to the Public Domain, 9-July-90.
// Date: 9-July-1990
// Please include these acknowledgements when distributing this file
#ifndef __NEW_H
#define __NEW_H
#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned size_t;
#endif
void* operator new(size_t size, void* ptr);
// _new_handler is a ptr to a parameterless function returning void
extern void (*_new_handler)();
void (*set_new_handler(void (*replacement_handler)()))();
#endif __NEW_H

// Borland says:
// NEW.H is NOT defined by AT&T 2.0 C++ specs. The contents
// were in C++ specs 1.2 and is making a comeback in the
// upcoming ANSI C++ spec. We do have the underlying code, ie
// _new_handler support, but it is not documented. It would be
// nicer to use a typedef for the void function pointer, for the
// above work-around.
//
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// http://groups.google.com/groups?selm...n.edu&oe=UTF-8
Jul 22 '05 #24

P: n/a
> #include <stdlib.h>
#include <stddef.h>

void* operator new(size_t size, void* ptr)
{
return ptr;
}

void* operator new[](size_t size, void* ptr)
{
return ptr;
}

class Foo{};

int main()
{
void* mem = malloc(sizeof(Foo));
Foo* p = new(mem) Foo();
}


Preliminary indications are that this idea works.
I am still working through the details. Thanks for
your help.
Jul 22 '05 #25

P: n/a
> void* operator new(size_t size, void* ptr)
{
return ptr;
}


This has become my <new> header file. This succinct advice, along with
the <placement new> syntax and instructions has amounted to more help
than everyone else put together. Since I was also able to stumble across the
Ye Olde syntax for delete[], (which is delete [int]) I will be able to adapt
my current std::vector (for pre-template compilers) so that it has identical
usage syntax and requirements the STL versions.

//
//
// A Turbo C++ 1.0 Workaround that permits <placement new>
// provided by "tom_usenet" <to********@hotmail.com> from
// the comp.lang.c++ newsgroup.
//
//
#pragma warn -par
void* operator new(size_t size, void* ptr)
{
return ptr;
}

Thanks again Tom for the excellent help !
Jul 22 '05 #26

P: n/a

"Peter Olcott" <ol****@worldnet.att.net> wrote in message news:ul*********************@bgtnsc04-news.ops.worldnet.att.net...
Resize and reserve do different things.
Reserve just allocates space (possibly moving existing objects in the process). It is this "possibly moving objects in the process" that I need to be able
to have a copy constructor for an array element.


It's one of the reasons. The other reason is to copy in the "fill" object
on resize. This avoids:
1. requiring a default constructor for the contained object.
2. creating an object that's just going to be copied over (a slight efficiency)
With modern compilers
this can be easily accomplished with <placement new>. This feature is
not available on AT&T 2.0, which is my required development platform.
Got any good ideas on the best way to do this?


Well, if you're willing to put some limitations on std::vector, for example that
the object must be default constructable, you could just default construct the
new allocation, and then use assignment.

Jul 22 '05 #27

This discussion thread is closed

Replies have been disabled for this discussion.