an aspect about class

could the name of a member variable be the same with a member function
for example
1
2
3
4
5
6
class example
{
  double value;
  public:
     double value(){return value;} 
}
what does your compiler tell you when you try to compile it?
Even if this is legal you should never do it.
OK,thanks!
A typical idiom is to prefix private variables with something. I tend to use "f" because of my Pascal background. A lot of C++ code tends towards "m". But there is no standard.
1
2
3
4
5
6
class example
{
  double f_value;
  public:
    double value() { return f_value; }
};

Hope this helps.
Another idiom when there is just one value that you can retrieve is to use either just get() or a conversion operator.

1
2
3
4
5
6
7
8
9
class example
{
    double value;
public:
    operator double() {return value;}
};

example x;
double f = x;


thanks!
Topic archived. No new replies allowed.