473,386 Members | 1,630 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,386 software developers and data experts.

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

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
6 1131
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
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
* 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
"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: techy techno | last post by:
Hiii Hello.. I just wanted to know if anyone can tell me how can I give my website visitors the feature of "FRIENDLY PRINTING" through IE. I would definitely like to give a feature like...
5
by: scsharma | last post by:
Hi, I am using .Net on Windows XP Professional. I am facing a unique problem with visual studio where intellisense feature is getting turned off. If i close IDE and reopen my solution the...
18
by: Michael B Allen | last post by:
Is it considered a bad idea to use a C99 only feature? It has been almost 6 years right? Specifically I'm interested in variadic macros. All of my code is C89 (or less) except for my debugging...
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
7
by: phal | last post by:
Hi I think there are many different browsers to browse to the Internet, how can I write the javascript to identify different browser and display according to the users. Some browser disable the...
3
by: Ronald S. Cook | last post by:
I was told that if calling lots of records from the database (let's say 100,000), that the GridView's paging feature would automatically "handle" everything. But the 100,000 records are still...
12
by: =?Utf-8?B?RGFyYSBQ?= | last post by:
Would like to know from the crowd that why do we require a Partial Class. By having such feature aren't we going out of the scope of Entity Concept. Help me to understand in what context this...
20
by: Luke R | last post by:
One thing i used to use alot in vb6 was that tiny little button in the bottom left of the code window which allowed you to view either the whole code file, or just the function/sub you are currenly...
0
by: Glynn | last post by:
Hi, Every open of Access 2000 database now causes "Installing Microsoft Graph Feature"... Tools/References has MS Graph10.0 feature checked... MS Office 10.0 feature checked... Have Office...
4
by: Jean-Fabrice RABAUTE | last post by:
Hi, I am happy to announce the release of the Companion.JS v0.2, an alpha version to a Javascript debugger for IE : http://www.my-debugbar.com/wiki/CompanionJS/HomePage The main new feature...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.