Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:54:15+00:00 2026-05-26T06:54:15+00:00

Why can’t a c++ class have same name for a function and a data

  • 0

Why can’t a c++ class have same name for a function and a data member?

class demo{
    public:
        int size();
    private:
        int size;   
};

int main(){
    return 0;
}


C:\Users\S>g++ demo.c
demo.c:5:7: error: declaration of 'int demo::size'
demo.c:3:7: error: conflicts with previous declaration 'int demo::size()'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:54:16+00:00Added an answer on May 26, 2026 at 6:54 am

    Suppose you want to take the address of the member-function size(), then you would write this:

    auto address = &demo::size;
    

    But it could be very well be the address of the member-data size as well. Ambiguous situation. Hence, it is disallowed by the language specification.

    That is not to say that it was impossible for the C++ committee to come up with a solution, but I suppose there is no major gain in doing so. Hence, the Standard simply disallowed it, to keep things simple.

    Also, the difference between member-data and member-function becomes less distinguishable visually if one declares the member function size() as:

    typedef void fun_type();
    
    struct demo
    {
        fun_type size; //It looks like a member-data, but it's a member-function
    };
    
    void demo::size()  //define the member function
    {
      std::cout << "It is crazy!" << std::endl;
    }
    
    int main()
    {
        demo d;
        d.size(); //call the function!
    }
    

    Output:

    It is crazy!

    See the online demo : http://ideone.com/ZjwyJ

    Now if we can implement member functions as explained above, then it becomes too obvious even to the naked eye that you cannot add another member with same name as:

    struct demo
    {
        fun_type size;
        int      size; //error - choose a different name for the member!
    };
    

    Wait That is not entirely correct, as the story is not finished yet. There is something less obvious I need to add here. You can add more than one member with same name:

    typedef void fun_type0();
    typedef void fun_type1(int a);
    typedef void fun_type2(int a, int b);
    
    struct demo
    {
        fun_type0 member;  //ok
        fun_type1 member;  //ok
        fun_type2 member;  //ok
    };
    

    This is completely valid code, as each member is a function of different type, so you can define them as:

    void demo::member()
    {
       std::cout << "member()" << std::endl;
    }
    void demo::member(int a)
    {
       std::cout << "member(" << a << ")" << std::endl;
    }
    void demo::member(int a, int b)
    {
       std::cout << "member(" << a << ", "<< b << ")" << std::endl;
    }
    

    Test code:

    int main()
    {
        demo d;
        d.member();
        d.member(10);
        d.member(200,300);
    }
    

    Output:

    member()
    member(10)
    member(200, 300)
    

    Online Demo : http://ideone.com/OM97Q


    The conclusion…

    You can add members with same name, as long as they’re function of different types. This is enabled by a feature called member-function-overloading (or simple function-overloading)1.

    1. Unfortunately, the language doesn’t provide similar feature, say member-data-overloading, for member data, neither do the language provide cross-member-overloading (that allows member-data and member-function to have the same name — the case in the question).

    So here a question naturally arises: do they not cause ambiguity problem? Yes, they do. But the point to be noted is that C++ committee came up with a solution to solve this ambiguity-problem, because they saw a huge gain in doing so, (in case of function-overloading).

    But the case in the question remains ambiguous, as the committee didn’t come up with a solution, as they didn’t see any huge advantage in doing so (as noted before). Also, when I said “C++ committee came up with solution”, I do NOT mean that the solution has been Standardized, I merely mean that they knew how the compilers can solve it, and how complex the solution would be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we have two main() functions in a C++ program?
Can you cast a List<int> to List<string> somehow? I know I could loop through
I have a jquery bug and I've been looking for hours now, I can't
Can I call a java class method from inside a flash movie?
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) I have a Rails
I have some data like this: 1 2 3 4 5 9 2 6
Can you have submenus with the top level set to checkable in WPF? I
Can someone explain to me what this would do? Module Foo class Bar class_inheritable_accessor
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.