Why can't members and methods have the same name?

C

cppaddict

Hi,

The following class containing a member and a method with the same
name will not compile:

class Test {
private:
bool x;
public:
Test() : x(true) {};
bool x() const {return x;}
};

I am not advocating writing code like the above, but I was puzzled by
the compiler's reason for rejecting it: It said there were multiple
declarations of Test::x.

Since methods and members can always be distinguished syntactically
when they are used, it seems strange that they cannot have the same
name. Just curious why this is?

thanks for any explanations,
cpp
 
J

JKop

cppaddict posted:
Hi,

The following class containing a member and a method with the same
name will not compile:

class Test {
private:
bool x;
public:
Test() : x(true) {};
bool x() const {return x;}
};

I am not advocating writing code like the above, but I was puzzled by
the compiler's reason for rejecting it: It said there were multiple
declarations of Test::x.

Since methods and members can always be distinguished syntactically
when they are used, it seems strange that they cannot have the same
name. Just curious why this is?

thanks for any explanations,
cpp


int morang(int prot);

int main(void)
{
int (*pMorang)(int) = morang;
}



When you write a function's name without parenthesis, you've got a pointer
to the function.


-JKop
 
V

Victor Bazarov

cppaddict said:
The following class containing a member and a method with the same
name will not compile:

class Test {
private:
bool x;
public:
Test() : x(true) {};
bool x() const {return x;}
};

I am not advocating writing code like the above, but I was puzzled by
the compiler's reason for rejecting it: It said there were multiple
declarations of Test::x.

Since methods and members can always be distinguished syntactically
when they are used, it seems strange that they cannot have the same
name. Just curious why this is?

Are they? Take a look:

struct Functor {
void operator() { 42; }
};

struct Test {
Functor x;
void x() {}
};

int main() {
Test t;
t.x();
}

Now, when I do 't.x()', is that invoking the member _function_ x,
or is it calling the operator() of the _data_ member x?

V
 
M

Martin Dickopp

cppaddict said:
The following class containing a member and a method with the same
name will not compile:

class Test {
private:
bool x;
public:
Test() : x(true) {};
bool x() const {return x;}
};

I am not advocating writing code like the above, but I was puzzled by
the compiler's reason for rejecting it: It said there were multiple
declarations of Test::x.

Since methods and members can always be distinguished syntactically
when they are used, it seems strange that they cannot have the same
name. Just curious why this is?

Are you sure they can always be distiguished syntactically? Please
consider the example below. What should its output be?


#include <iostream>

int foo ()
{
return 1;
}

class Test {
public:
Test () : x (foo) {}
int (*x) ();
int x () const { return 2; }
};

int main ()
{
Test t;
std::cout << t.x () << '\n';
}


Martin
 
B

Bill Seurer

cppaddict said:
Since methods and members can always be distinguished syntactically
when they are used, it seems strange that they cannot have the same
name. Just curious why this is?

&Test::x

which x is it?
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

cppaddict said:
The following class containing a member and a method with the same
name will not compile:

class Test {
private:
bool x;
public:
Test() : x(true) {};
bool x() const {return x;}
};

I am not advocating writing code like the above, but I was puzzled by
the compiler's reason for rejecting it: It said there were multiple
declarations of Test::x.

Since methods and members can always be distinguished syntactically
when they are used, it seems strange that they cannot have the same
name. Just curious why this is?

They can't always be distinguished.

class F {
public:
void operator () () { }
};

class Test {
F f;
void f ();
public:
Test ()
{
f (); // What f ?
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top