Cant think of title.

Why can I do this:
1
2
3
4
5
6
7
8
struct myClass {
int a;
int b;
myClass(int &A, int &B) {
a = A;
b = B;
}
};

But not this?
1
2
3
4
5
6
7
8
struct myClass {
int a;
int b;
myClass(int &A, &B) { //Here goes error
a = A;
b = B;
}
};
Last edited on
&B doesn't name a type, it's a syntax error. It just says B is a reference, but it doesn't say what type of reference.
Every function parameter needs to have its own type, it's simply a different syntax than variable declarations.
FWIW, this annoys me too.
Variable and function declarations do look similar; both can have comma separated lists. What is in the list?

One can declare more than one variable in one statement:
int A, B, C;
The statement has essentially one typename and then a comma separated list of identifiers.

However, one cannot do list of lists:
int A, B, float C;

Function declaration has a comma separated list of argument declarations, and identifier is optional within each argument declaration:
1
2
void foo( int A, int B, float C ); // ok
void foo( int, int B, float ); // ok 


Now lets assume that you could declare multiple arguments of same type. The identifier being optional leads to ...
1
2
void foo( int A, int B ); // ok
void foo( int , ); // what? 

and with multiple types:
void foo( int A, B, float C ); // ???
That has two nested comma separated lists: { {int,int} , float }

It is understandable that we don't want to parse such syntax.
Topic archived. No new replies allowed.