- 论坛徽章:
- 0
|
20可用积分
下面是friend class的一些介绍,这个我明白。
Friend Classes
C++ provides the friend keyword to do just this. Inside a class, you can indicate that other classes (or simply functions) will have direct access to protected and private members of the class. When granting access to a class, you must specify that the access is granted for a class using the class keyword:
friend class aClass;
Note that friend declarations can go in either the public, private, or protected section of a class--it doesn't matter where they appear. In particular, specifying a friend in the section marked protected doesn't prevent the friend from also accessing private fields. Here is a more concrete example of declaring a friend:
class Node
{
private:
int data;
int key;
// ...
friend class BinaryTree; // class BinaryTree can now access data directly
};
Now, Node does not need to provide any means of accessing the data stored in the tree. The BinaryTree class that will use the data is the only class that will ever need access to the data or key. (The BinaryTree class needs to use the key to order the tree, and it will be the gateway through which other classes can access data stored in any particular node.) Now in the BinaryTree class, you can treat the key and data fields as though they were public:
class BinaryTree
{
private:
Node *root;
int find(int key);
};
int BinaryTree::find(int key)
{
// check root for NULL...
if(root->key == key)
{
// no need to go through an accessor function
return root->data;
}
// perform rest of find
}
我的问题是下面的:
class student_info 是class Core的友员类。
Core.h如下:
#ifndef GUARD_Core_h
#define GUARD_Core_h
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
class Core {
public:
Core(): midterm(0), final(0) { }
Core(std::istream& is) { read(is); }
std::string name() const;
// as defined in 13.1.2/230
virtual std::istream& read(std::istream&);
virtual double grade() const;
virtual ~Core() { }
protected:
// accessible to derived classes
std::istream& read_common(std::istream&);
double midterm, final;
std::vector<double> homework;
virtual Core* clone() const { return new Core(*this); }
private:
// accessible only to `Core'
std::string n;
friend class Student_info;
};
class Grad: public Core {
public:
Grad(): thesis(0) { }
Grad(std::istream& is) { read(is); }
// as defined in 13.1.2/230; Note: `grade' and `read' are `virtual' by inheritance
double grade() const;
std::istream& read(std::istream&);
private:
double thesis;
#ifdef _MSC_VER
Core* clone() const { return new Grad(*this); }
#else
Grad* clone() const { return new Grad(*this); }
#endif
};
bool compare(const Core&, const Core&);
bool compare_Core_ptrs(const Core* cp1, const Core* cp2);
#endif |
student_info.h如下:
#ifndef GUARD_Student_info_h
#define GUARD_Student_info_h
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Core.h"
class Student_info {
public:
// constructors and copy control
Student_info(): cp(0) { }
Student_info(std::istream& is): cp(0) { read(is); }
Student_info(const Student_info&
Student_info& operator=(const Student_info&
~Student_info() { delete cp; }
// operations
std::istream& read(std::istream&
std::string name() const {
if (cp) return cp->name();
else throw std::runtime_error("uninitialized Student"
}
double grade() const {
if (cp) return cp->grade();
else throw std::runtime_error("uninitialized Student"
}
static bool compare(const Student_info& s1,
const Student_info& s2) {
return s1.name() < s2.name();
}
private:
Core* cp; //cp是这个类的私有数据
int i;
};
#endif
student_info.cc如下:
#include <iostream>
#include "Core.h"
#include "Student_info.h"
using std::istream;
istream& Student_info::read(istream& is)
{
delete cp; // delete previous object, if any
char ch;
is >> ch; // get record type
if (ch == 'U') {
cp = new Core(is);
} else {
cp = new Grad(is);
}
return is;
}
Student_info::Student_info(const Student_info& s): cp(0)
{
if (s.cp) cp = s.cp->clone(); //我的问题是cp不是s的private member吗?为什么可以直接访问s.cp阿?这个也是因为友员类的关系吗?我一般看到的友员类好像不是这样的。
int a = s.i;
}
Student_info& Student_info:perator=(const Student_info& s)
{
if (&s != this) {
delete cp;
if (s.cp) ////我的问题是cp不是s的private member吗?为什么可以直接访问s.cp阿?这个也是因为友员类的关系吗?我一般看到的友员类好像不是这样的。
cp = s.cp->clone();
else
cp = 0;
}
return *this;
}
问题在student_info的拷贝构造函数和赋值操作符中,我的问题是cp不是s的private member吗?为什么可以直接访问s.cp阿?这个也是因为友员类的关系吗?我一般看到的友员类好像都是我上面给的二叉树那个例子那样的。
请大虾指点下,多谢!
[ 本帖最后由 susesuse 于 2009-6-6 07:13 编辑 ] |
最佳答案
查看完整内容
类A的private成员(包括成员变量,成员函数,静态变量,静态成员函数)是限制其他类B(包括类B的成员变量,成员函数,静态变量,静态成员函数)或者自由函数对其进行访问。并不能限制类A的成员对其进行访问。Student_info::Student_info(const Student_info& s) 是Student_info的拷贝构造函数,也是Student_info的一个成员函数。它自然可以访问Student_info类的所有成员,即使是private。
|