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()'
Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.
Constructor is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. Constructor in C++ has the same name as class or structure. Hence the correct answer is Constructor.
You cant because if you have example(), 'example' is a pointer to that function. This is the right answer. There are function pointers, which are variables. But also, all function names are treated as const function pointers!
Case matters: alpha, Alpha, and ALPHA are different names. Variables and functions have separate name spaces, which means a variable and a function can have the same name, such as value and value(), and Mata will not confuse them.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With