N
Nosophorus
Hi!
I have coded a class and I would like to use default parameters in its
constructor argument list. So, let's suppose my class is:
class Class1 {
public:
Class1(int);
~Class1();
void print();
private:
/*SOME CODE HERE*/
};
Let's define the constructor as follows:
Class1::Class1(int dummy = 0)
{
/*SOME CODE HERE*/
}
Then, when I define an object of that class and call its constructor
with an empty list of arguments ( I mean "Class1 MyObj1()" ), the
compiler does not complain -- I should state that I call the
constructor using an empty argument list because I have in my mind
that, since the constructor has default parameter in its argument
list, when the constructor sees an empty list of arguments, it will
use the default parameter instead.
BUT, if I try to call a member function of that class (say
"MyObj1.print()" ), the compiler says the function is of non-class
type!
HOWEVER, if I define the object this way:
Class1 MyObj1;
The code works well! The constructor uses its default parameters and
the object defined can call any member function.
What are the differences between the two definitions above?
Defining an object through "Class1 MyObj1()" mean that a "void" value
is being passed to the constructor (which, in this case, requires an
"int" value)? What is the meaning of "Class1 MyObj1" ?
NEVERTHELESS, when I tried a different constructor argument list using
"void" instead of any other value, I got the same errors as cited
above when calling member functions!
class Class1 {
public:
Class1(void) {}; //OR Class1() {};
~Class1();
void print();
private:
/*SOME CODE HERE*/
};
Class1 MyObj1(); //OR Class1 MyObj1(void);
MyObj1.print(); <-- Gives an error! ('print' in 'MyObj1', which is of
non-class type Class1)
Sorry for the long question, but I hope I made me clear.
I appreciate any comments and further help.
Thank You!
Marcelo de Brito
I have coded a class and I would like to use default parameters in its
constructor argument list. So, let's suppose my class is:
class Class1 {
public:
Class1(int);
~Class1();
void print();
private:
/*SOME CODE HERE*/
};
Let's define the constructor as follows:
Class1::Class1(int dummy = 0)
{
/*SOME CODE HERE*/
}
Then, when I define an object of that class and call its constructor
with an empty list of arguments ( I mean "Class1 MyObj1()" ), the
compiler does not complain -- I should state that I call the
constructor using an empty argument list because I have in my mind
that, since the constructor has default parameter in its argument
list, when the constructor sees an empty list of arguments, it will
use the default parameter instead.
BUT, if I try to call a member function of that class (say
"MyObj1.print()" ), the compiler says the function is of non-class
type!
HOWEVER, if I define the object this way:
Class1 MyObj1;
The code works well! The constructor uses its default parameters and
the object defined can call any member function.
What are the differences between the two definitions above?
Defining an object through "Class1 MyObj1()" mean that a "void" value
is being passed to the constructor (which, in this case, requires an
"int" value)? What is the meaning of "Class1 MyObj1" ?
NEVERTHELESS, when I tried a different constructor argument list using
"void" instead of any other value, I got the same errors as cited
above when calling member functions!
class Class1 {
public:
Class1(void) {}; //OR Class1() {};
~Class1();
void print();
private:
/*SOME CODE HERE*/
};
Class1 MyObj1(); //OR Class1 MyObj1(void);
MyObj1.print(); <-- Gives an error! ('print' in 'MyObj1', which is of
non-class type Class1)
Sorry for the long question, but I hope I made me clear.
I appreciate any comments and further help.
Thank You!
Marcelo de Brito