Problem initializing class with literal array!

  • Thread starter Pedro Miguel Carvalho
  • Start date
P

Pedro Miguel Carvalho

Greetings.

I have a class that I would like to initialize with a literal array but it
is not accepted by the compiler. It works if I use a array variable but not
a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V; //
OK
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error
test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

}
 
J

JKop

Pedro Miguel Carvalho posted:
Greetings.

I have a class that I would like to initialize with a literal array
but it is not accepted by the compiler. It works if I use a array
variable but not a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V;
//
OK
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error


....some-one's confused!

To achieve that syntax, you'd need something like:


struct test
{
float a;
float b;
float c;
float d;
...
};

test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error


struct test
{
float blah[10];
};



I can see this discussion getting pretty tedious ( member variables,
aggregates, arrays decaying to pointers ) :-(


-JKop



-JKop
 
J

John Harrison

Pedro Miguel Carvalho said:
Greetings.

I have a class that I would like to initialize with a literal array but it
is not accepted by the compiler. It works if I use a array variable but not
a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V; //
OK
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error
test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

}

There are no array literals in C++, { 0.0, 1.0 ... } is an initialiser that
can only be used in certain contexts, it is not an array literal.

It is not possible to have arrays as parameters in C++.
test( const float T[10] ) {

This code does not mean your constructor has an array for a parameter. The
compiler converts your code to this.

test( const float* T ) {

Both these reasons mean that your code cannot work.
test T1 = V;

This is the best you can do.

john
 
V

Victor Bazarov

Pedro said:
I have a class that I would like to initialize with a literal array but it
is not accepted by the compiler. It works if I use a array variable but not
a literal array. Can someone shed some light on this.

class test {
public:
test( const float T[10] ) {

This is essentially

test(float const * T)

.. In function argument lists arrays decay into pointers.
}
};

int main( void ) {

float V[10] = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};

test T1 = V; //
OK

It's OK because 'V' (the name of the array) has also decayed into
a pointer to the first element, so what you have is

test T1(test(&V[0]));

which is fine because 'test's constructor expects a pointer to const
float, and &V[0] is a pointer to float.
test T2 = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; // Syntax error
test T3( {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0} ); // Syntax error

}

Well, what can I tell you? The first of the two syntaxes is reserved
for initialising _aggregates_. T2 is not an aggregate. That's why you
get an error in the first case. The second case presumes the existence
of some kind of "array literal". There are no array literals in C++.

So, you're stuck with the solution you've already found.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top