Topic: can't this be a feature in C++?
Author: bgaloma@yahoo.com (Paul)
Date: Mon, 6 Sep 2004 06:29:57 GMT Raw View
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.
thanks Howard
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: bgaloma@yahoo.com (Paul)
Date: Mon, 6 Sep 2004 15:23:55 GMT Raw View
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...
class 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.
thanks Howard
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Thorsten Ottosen" <nesotto@cs.auc.dk>
Date: Mon, 6 Sep 2004 20:50:27 GMT Raw View
"Paul" <bgaloma@yahoo.com> wrote in message news:d8b1e3e6.0409051905.659554f0@posting.google.com...
| 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++.
|
| 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,
The new boost.assign library (will be in the soon-to-be-release version of boost, see www.boost.org) allows you pretty much what you
want, for example,
std::vector<A> v = list_of<A>(5)(5,10)(20);
| or just
| because of its complex to implement???
There has to be compelling reasons for adding this to the standard. I don't think it will be hard to implement.
A library solution has these benefits:
1. no change to the language
2. its more flexible and can allow for stuff like
std::vector<A> v = list_of<A>(5).repeat( 10, A(20) )(5,10)(20)
3. most funcionality comes for free if the containers just have a constructor that takes
two iterators.
br
Thorsten
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]