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

why can't this be a feature in C++?

P: n/a
Hi,

Often I come across situatation where I need to create array of
objects with parameterized constructors, there are alternative ways to
do this, but why can't it be a standard feature in C++.
some thing like this...
claas A
{
public:
A(){}
A(int v):_x(v),_y(v){}
A(int v, int w):_x(v),_y(w){}
private:
int _x;
int _y;
};

int main()
{
A *p1 = new A[3](5); //all objects have same value
A *p2 = new A[3]((5),(5,10),(20)); //objects have different values
return 0;
}

Is there any reason for not having some thing like this, or just
because of its complex to implement???

tia.
-Paul.
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Paul wrote:
Hi,

Often I come across situatation where I need to create array of
objects with parameterized constructors, there are alternative ways to
do this, but why can't it be a standard feature in C++.
some thing like this...
claas A
{
public:
A(){}
A(int v):_x(v),_y(v){}
A(int v, int w):_x(v),_y(w){}
private:
int _x;
int _y;
};

int main()
{
A *p1 = new A[3](5); //all objects have same value
A *p2 = new A[3]((5),(5,10),(20)); //objects have different values


Thank God, C++ does not have the second syntax.
The above code might be seeemingly readable for an array containing 3
elements. If we were to initialize array of 100 elements say, imagine
how the syntax is going to be.
Arrays are supposed to be homogeneous data types. And if anyone
wants to initialize the objects as mentioned above, it means something
is wrong with the design.

--
Karthik.
Jul 22 '05 #2

P: n/a
Paul wrote:

int main()
{
A *p1 = new A[3](5); //all objects have same value
A *p2 = new A[3]((5),(5,10),(20)); //objects have different values
return 0;
}

Is there any reason for not having some thing like this, or just
because of its complex to implement???


Array with elements initialised with a non-default constructor was
considered and probably still is implemented by the gcc compiler but
finally the idea was given up to the concept of fill constructor in the
sequence types in the STL library. Check any STL documentation for
further details.

It is used like:

std::vector<int> v(5,3); // vector of 5 ints filled with the 3 value.
or
std::vector<int>* vp = new std::vector<int> (5,3);
Regards,
Janusz
Jul 22 '05 #3

P: n/a
* Paul:

Often I come across situatation where I need to create array of
objects with parameterized constructors, there are alternative ways to
do this, but why can't it be a standard feature in C++.
some thing like this...
claas A
{
public:
A(){}
A(int v):_x(v),_y(v){}
A(int v, int w):_x(v),_y(w){}
private:
int _x;
int _y;
};

int main()
{
A *p1 = new A[3](5); //all objects have same value
A *p2 = new A[3]((5),(5,10),(20)); //objects have different values
return 0;
}

Is there any reason for not having some thing like this, or just
because of its complex to implement???


There's no compelling reason to complicate the language since you control
the A class.

One case (i) is when you know the values at compile time, then you can
simply use an ordinary array initializer:

A arr[] = { A(5), A(5,10), A(20) };

A second case (ii) is the first in your program where all objects have the
same value, which is what std::vector provides you with:

std::vector<A> arr( 3, A(5) );

Of course that solution (ii.a) means that A-objects must be copyable, but
presumably if they're to be in an array they are. If not and the array size
is fixed at compile time then you can (ii.b) employ the case (i) solution,
although it's a bit tedious. If A-objects are non-copyable and the array
size isn't known at compile time, then the best you can do in practice,
except implementing a placement-new based thing la std::vector,
which is too horrible to complement, is (ii.c) to use an array or vector of
(possibly smart) pointers.

A third case (iii) is when you don't know the values at compile time and
don't know the array size at compile time. Then if A-objects are copyable
you can use std::vector, e.g. by (iii.a) giving it a "smart iterator". This
assumes that an object carrying the constructor arguments can be "light",
whereas the A-object constructed from those arguments will be "heavy":

#include <iostream>
#include <vector>

class A
{
public:
class Arg
{
public:
Arg(): _v( 0 ), _w( 0 ) {}
Arg( int v ): _v( v ), _w( v ) {}
Arg( int v, int w ): _v( v ), _w( w ) {}

int v() const { return _v; }
int w() const { return _w; }
private:
int _v, _w;
};

A( Arg const& data = Arg() ): _x( data.v() ), _y( data.w() ) {}

int x() const { return _x; }
int y() const { return _y; }
private:
int _x, _y;
};

int main()
{

std::vector<A::Arg> args;
// The following is assumed to be some dynamic thing.
args.push_back( A::Arg( 5 ) );
args.push_back( A::Arg( 5, 10 ) );
args.push_back( A::Arg( 20 ) );

std::vector<A> arr( args.begin(), args.end() );

for( size_t i = 0; i < arr.size(); ++i )
{
std::cout << "( " << arr[i].x() << ", " << arr[i].y() << " )\n";
}
}

If A-objects are non-copyable then we're into case (iii.b), which I'm too
lazy to write anything about right here. :-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

P: n/a
"Paul" <bg*****@yahoo.com> wrote in message news:d8**************************@posting.google.c om...
| Hi,
|
| Often I come across situatation where I need to create array of
| objects with parameterized constructors, there are alternative ways to
| do this, but why can't it be a standard feature in C++.
| some thing like this...

| A *p2 = new A[3]((5),(5,10),(20)); //objects have different values
| return 0;

Two comments:

1. don't use built-in arrays in C++, go for std::vector<T> or boost::array<T,size> in that order

2. given either choice in (1), consider a new assign library in the next revision (due very soon) of boost (www.boost.org)

vector<A> vec = boost::assign::list_of< A >(5)(5,10)(20);

br

Thorsten
Jul 22 '05 #5

P: n/a
Karthik Kumar <ka*****************@yahoo.com> wrote in message news:<4136e696$1@darkstar>...
Paul wrote:
Hi,

Often I come across situatation where I need to create array of
objects with parameterized constructors, there are alternative ways to
do this, but why can't it be a standard feature in C++.
some thing like this...
claas A
{
public:
A(){}
A(int v):_x(v),_y(v){}
A(int v, int w):_x(v),_y(w){}
private:
int _x;
int _y;
};

int main()
{
A *p1 = new A[3](5); //all objects have same value
A *p2 = new A[3]((5),(5,10),(20)); //objects have different values
Thank God, C++ does not have the second syntax.
The above code might be seeemingly readable for an array containing 3
elements. If we were to initialize array of 100 elements say, imagine
how the syntax is going to be.

then probably you also don't like this syntax too...
struct S{
int a;
int b;
int c;
};

S s[3] = {{1,2,3},{2,3,4},{5,6,7}};
why don't you send a proposal to remove this...
well its the way you look, glass is full or empty.
Arrays are supposed to be homogeneous data types. And if anyone
Dude you need to read some book, there is a difference between type
and value, also read state of the object.
wants to initialize the objects as mentioned above, it means something
is wrong with the design.

which design??

-Paul
Jul 22 '05 #6

P: n/a

"Paul" <bg*****@yahoo.com> wrote in message
news:d8**************************@posting.google.c om...
Karthik Kumar <ka*****************@yahoo.com> wrote in message news:<4136e696$1@darkstar>...
Paul wrote:
Hi,

Often I come across situatation where I need to create array of
objects with parameterized constructors, there are alternative ways to
do this, but why can't it be a standard feature in C++.
some thing like this...
claas A
{
public:
A(){}
A(int v):_x(v),_y(v){}
A(int v, int w):_x(v),_y(w){}
private:
int _x;
int _y;
};

int main()
{
A *p1 = new A[3](5); //all objects have same value
A *p2 = new A[3]((5),(5,10),(20)); //objects have different values


Thank God, C++ does not have the second syntax.
The above code might be seeemingly readable for an array containing 3
elements. If we were to initialize array of 100 elements say, imagine
how the syntax is going to be.

then probably you also don't like this syntax too...
struct S{
int a;
int b;
int c;
};

S s[3] = {{1,2,3},{2,3,4},{5,6,7}};


That's quite a bit different. That doesn't call different constructors for
different items in the array. Your example did. (I think that that might
have confused Karthik, making it look like you had different object types in
that array, when really you were just making use of different constructors
for the same object type.)
why don't you send a proposal to remove this...
well its the way you look, glass is full or empty.
Arrays are supposed to be homogeneous data types. And if anyone


Dude you need to read some book, there is a difference between type
and value, also read state of the object.
wants to initialize the objects as mentioned above, it means something
is wrong with the design.

which design??


Probably the design of any program that requires such a construct...?

I've seen it asked so many times ("can I use other than the default
constructor when initializing an array in a single statment?"), that it's
obviously something that programmers would find useful, even if it were
confusing to some.

But this group generally doesn't discuss *why* the standard is a certain
way. There's another group that discusses the standard, but I'm not sure of
the name, (perhaps comp.std.C++ or something similar?).

-Howard
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.