jianmin jin
December 3rd, 2004, 01:19 AM
class A
{
int test;
void test();
}
//can't compile,why?
{
int test;
void test();
}
//can't compile,why?
|
Click to See Complete Forum and Search --> : why can't class data member has the same name with function member jianmin jin December 3rd, 2004, 01:19 AM class A { int test; void test(); } //can't compile,why? Paul McKenzie December 3rd, 2004, 01:44 AM class A { int test; void test(); } //can't compile,why?Because it's against the rules of C++. Regards, Paul McKenzie Kheun December 3rd, 2004, 01:50 AM The parameters of the function must be different for function overloading. walkinginwater December 3rd, 2004, 03:31 AM i think Khun's answer is reasonalble, when you come to the problem of defining opreator for the class, you will see that it is important to keep the datamember and function member diferrent, because you can make a default parameter for the function member! NMTop40 December 3rd, 2004, 07:50 AM If it were allowed it would not be recommended anyway. class MyClass { int m; public: int m() const { return m; } void m( int x ) { m=x; } MyClass ( int x ) : m(x) { } }; If this were legal, what would m(x) in the constructor do? We know it will set member 'm' to value 'x' but would it be an initialisation or a function call? Ajay Vijay December 3rd, 2004, 08:02 AM Error in this code:class MyClass { int n; public: int m() const { return n; } void m( int x ) { n=x; } MyClass ( int x ) : m(x) { } };Error:49) : error C2436: 'm' : member function or nested class in constructor initializer listPlease do note that I renamed m variable with n, just for testing this code! HighCommander4 December 3rd, 2004, 11:42 AM class A { int test; void test(); } //can't compile,why? If that were allowed, what would A::test evaluate to? The integer defined by "int test" or a function pointer pointing to the function declares as "void test()"? Ajay Vijay December 4th, 2004, 12:26 AM Because "&" - AddressOf operator will result to ambiguity. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |