CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2002
    Posts
    199

    why can't class data member has the same name with function member

    class A
    {
    int test;
    void test();
    }

    //can't compile,why?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why can't class data member has the same name with function member

    Quote Originally Posted by jianmin jin
    class A
    {
    int test;
    void test();
    }

    //can't compile,why?
    Because it's against the rules of C++.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: why can't class data member has the same name with function member

    The parameters of the function must be different for function overloading.

  4. #4
    Join Date
    Nov 2004
    Posts
    92

    Re: why can't class data member has the same name with function member

    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!

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: why can't class data member has the same name with function member

    If it were allowed it would not be recommended anyway.

    Code:
    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?

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: why can't class data member has the same name with function member

    Error in this code:
    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 list
    Please do note that I renamed m variable with n, just for testing this code!
    Last edited by Ajay Vijay; December 3rd, 2004 at 09:06 AM.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: why can't class data member has the same name with function member

    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()"?

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: why can't class data member has the same name with function member

    Because "&" - AddressOf operator will result to ambiguity.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured