i was wondering which would be legal and illegal definitions. Im really not clear on how the members in public and private are assigned which is why im probably having trouble determining which definitions are legal.
{
public:
A(){}
A(int x, char y):xx(x), yy(y) {}
// other members
private:
int xx;
char yy;
};
1) A x(2, ‘A’);
2) A x;
3) A x = A(2, ‘A’);
4) A x(1);
5) A x( );
1. ‘A’ is invalid but 'A' is valid (different type of quotes)
2. valid
3. ‘A’ is invalid but 'A' is valid (different type of quotes)
4. invalid, no constructor accepts only one argument
5. valid, but declares a function named x that returns an A (it does not create an object)
@LB for #4, do you mean for this particular example the constructor cant accept one argument? Because i thought a constructor can have one argument such as
class Line
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor
The class you showed in your original post only has two constructors: one which accepts zero arguments, and one which accepts two arguments. If you add a third constructor that accepts a single argument of type int then #4 would be valid.
Note: there may be other constructors implicitly defined by the compiler, but none of them accept an int.