<C++ Primer Plus 6th edition>
有这么一段:
Class Scope
Chapter 9 discusses global (or file) scope and local (or block) scope. Recall that you can
use a variable with global scope anywhere in the file that contains its definition, whereas a
variable with local scope is local to the block that contains its definition. Function names,
too, can have global scope, but they never have local scope. C++ classes introduce a new
kind of scope: class scope.
Class scope applies to names defined in a class, such as the names of class data members
and class member functions. Items that have class scope are known within the class but
not outside the class.Thus, you can use the same class member names in different classes
without conflict. For example, the shares member of the Stock class is distinct from the
shares member of a JobRide class.Also class scope means you can’t directly access members
of a class from the outside world.This is true even for public function members.That
is, to invoke a public member function, you have to use an object:
Stock sleeper("Exclusive Ore", 100, 0.25); // create object
sleeper.show(); // use object to invoke a member function
show(); // invalid -- can’t call method directly
Similarly, you have to use the scope-resolution operator when you define member
functions:
void Stock::update(double price)
{
...
}
In short, within a class declaration or a member function definition you can use an
unadorned member name (the unqualified name), as when sell() calls the set_tot()
member function.A constructor name is recognized when it is called because its name is
the same as the class name. Otherwise, you must use the direct membership operator (.),
the indirect membership operator (->), or the scope-resolution operator (::), depending
on the context, when you use a class member name.The following code fragment illustrates
how identifiers with class scope can be accessed:
class Ik
{
private:
int fuss; // fuss has class scope
public:
Ik(int f = 9) {fuss = f; } // fuss is in scope
void ViewIk() const; // ViewIk has class scope
};
void Ik::ViewIk() const //Ik:: places ViewIk into Ik scope
{
cout << fuss << endl; // fuss in scope within class methods
}
...
int main()
{
Ik * pik = new Ik;
Ik ee = Ik(8); // constructor in scope because has class name
ee.ViewIk(); // class object brings ViewIk into scope
pik->ViewIk(); // pointer-to-Ik brings ViewIk into scope
...
有这么一段:
Class Scope
Chapter 9 discusses global (or file) scope and local (or block) scope. Recall that you can
use a variable with global scope anywhere in the file that contains its definition, whereas a
variable with local scope is local to the block that contains its definition. Function names,
too, can have global scope, but they never have local scope. C++ classes introduce a new
kind of scope: class scope.
Class scope applies to names defined in a class, such as the names of class data members
and class member functions. Items that have class scope are known within the class but
not outside the class.Thus, you can use the same class member names in different classes
without conflict. For example, the shares member of the Stock class is distinct from the
shares member of a JobRide class.Also class scope means you can’t directly access members
of a class from the outside world.This is true even for public function members.That
is, to invoke a public member function, you have to use an object:
Stock sleeper("Exclusive Ore", 100, 0.25); // create object
sleeper.show(); // use object to invoke a member function
show(); // invalid -- can’t call method directly
Similarly, you have to use the scope-resolution operator when you define member
functions:
void Stock::update(double price)
{
...
}
In short, within a class declaration or a member function definition you can use an
unadorned member name (the unqualified name), as when sell() calls the set_tot()
member function.A constructor name is recognized when it is called because its name is
the same as the class name. Otherwise, you must use the direct membership operator (.),
the indirect membership operator (->), or the scope-resolution operator (::), depending
on the context, when you use a class member name.The following code fragment illustrates
how identifiers with class scope can be accessed:
class Ik
{
private:
int fuss; // fuss has class scope
public:
Ik(int f = 9) {fuss = f; } // fuss is in scope
void ViewIk() const; // ViewIk has class scope
};
void Ik::ViewIk() const //Ik:: places ViewIk into Ik scope
{
cout << fuss << endl; // fuss in scope within class methods
}
...
int main()
{
Ik * pik = new Ik;
Ik ee = Ik(8); // constructor in scope because has class name
ee.ViewIk(); // class object brings ViewIk into scope
pik->ViewIk(); // pointer-to-Ik brings ViewIk into scope
...