asdf wrote:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.
#include<iostream>
using namespace std;
class A{
int x;
public:
int get(x) {return 1;};
The compiler in this context is unable to establish a signature for the
member function. Any parameter specified must therefore have a type in
order to construct the signature. Remember that parameters are objects
from the outside world insofar as the instance of the class is
concerned. get(...) doesn't need to pass x as a parameter since it is
already part of the object.
};
int main()
{
return 0;
}
However, there is a syntax error : identifier 'x'
what's the reason? the private member cannot be the parameter of the
public function?
Presumably, your goal is to use get(...) to "get" x's value all you
need to do is return the private integer. In order to protect x from
being modified through side-effects, get() should be constant.
class A
{
int x;
public:
A(int n = 1) : x(n) { } // or A() : x(1) { }
int get() const { return x; }
};
And we haven't touched copy ctors nor assignment operators.