c++链表中关于私有指针成员问题

hero-king 2014-07-16 09:01:12
template<typename T>
class List{
private:
class Node{
public:
T m_data;
Node* m_prev;
Node* m_next;
Node(const T& data,Node* prev = NULL,Node* next = NULL):m_data(data),m_prev(prev),m_next(next){}
friend ostream& operator<<(ostream& os,Node node){
return os<< '[' << node.m_data << ']';
}
};
Node* m_head;
Node* m_tail;
public:
List(void):m_head(NULL), m_tail(NULL){}
~List(void){
clear();
}
List(const List& that):m_head(NULL), m_tail(NULL){
for(Node* node = that.m_head;node;node=node->m_next)
push_back(node->m_data);
}
List& operator=(const List& that){
if(&that != this){
List list(that);
swap(m_head,list.m_head);
swap(m_tail,list.m_tail);
}
return *this
}
T& front(void){
return m_head->m_data;
}
const T& front(void)const{
return const_cast<List*>(this)->front();
}
List& push_front(const T& data){
m_head=new Node(data,NULL,m_head);
if(m_head->m_next){
m_head->m_next->m_prev=m_head;
}else{
m_tail=m_head;
}
return *this;
}
List& pop_front(void){
Node* node=m_head->m_next;
delete m_head;
m_head=node;
if(m_head){
m_head->m_next->m_prev=NULL;
}else{
m_tail=NULL;
}
return *this;
}
T& back(void){
return m_tail->m_data;
}
const T& back(void)const{
return const_cast<List*>(this)->back();
}
List& push_back(const T& data){
m_tail=new Node(data,m_tail,NULL);
if(m_tail->m_prev){
m_tail->m_prev->m_next=m_tail;
}else{
m_head=m_tail;
}
return *this;
}
List& pop_back(void){
Node* node=m_tail->m_prev;
delete m_tail;
m_tail=node;
if(m_tail){
m_tail->m_prev->m_next = m_tail;
}else{
m_head=NULL;
}
return *this;
}
void remove(const T& data){
for(Node* node=m_head,*next;node;node=next){
next=node->m_next;
if(data == node->m_data){
if(node->m_prev){
node->m_prev->m_next=node->m_next;
node->m_next->m_prev=node->m_prev;
}else{
m_head=node->m_next;
}
if(node->m_next){
node->m_next->m_prev=node->m_prev;
node->m_prev->m_next=node->m_next;
}else{
m_tail=node->m_prev;
}
delete node;
}
}
}
void clear(void){
for(Node* next; m_head;m_head=next){
next=m_head->m_next;
delete m_head;
}
m_tail=NULL;
}
bool empty(void)const{
return m_head == m_tail;
}
size_t size(void)const{
int counter=0;
for(Node* node=m_head;node;node=node->next){
counter++;
}
return counter;
}
friend ostream& operator<<(const List& list){
os << *(list.m_head);
return os;
}


};


各位大大:
链表内部私有node为什么可以在外部可以访问?
例如
List(const List& that):m_head(NULL), m_tail(NULL){
for(Node* node = that.m_head;node;node=node->m_next)
push_back(node->m_data);
}
这个拷贝构造函数,不是私有成员一般都不能外部访问?求解
...全文
180 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
KangRoger 2014-07-20
  • 打赏
  • 举报
回复
引用 6 楼 wb136959813 的回复:
[quote=引用 5 楼 KangRoger 的回复:] [quote=引用 4 楼 wb136959813 的回复:] [quote=引用 3 楼 KangRoger 的回复:] 例如 List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } m_head,m_tail是私有的,但是List函数是在类内部,在类的作用域范围内,当然可以访问。 Node结点的成员是public,在外部可以访问。
那问下大侠,如果我 List that; that.m_head;that.m_tail;可以直接这样访问吗?[/quote] 不可以访问。m_head和m_tail是私有变量,只能在类的作用域内使用。[/quote] List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } 那在拷贝构造函数里为什么可以Node* node=that.m_head这样写? 有点不解啊?大侠[/quote] 拷贝构造函数是类的成员函数,在类的作用于范围内啊!
hero-king 2014-07-20
  • 打赏
  • 举报
回复
引用 8 楼 KangRoger 的回复:
[quote=引用 6 楼 wb136959813 的回复:] [quote=引用 5 楼 KangRoger 的回复:] [quote=引用 4 楼 wb136959813 的回复:] [quote=引用 3 楼 KangRoger 的回复:] 例如 List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } m_head,m_tail是私有的,但是List函数是在类内部,在类的作用域范围内,当然可以访问。 Node结点的成员是public,在外部可以访问。
那问下大侠,如果我 List that; that.m_head;that.m_tail;可以直接这样访问吗?[/quote] 不可以访问。m_head和m_tail是私有变量,只能在类的作用域内使用。[/quote] List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } 那在拷贝构造函数里为什么可以Node* node=that.m_head这样写? 有点不解啊?大侠[/quote] 拷贝构造函数是类的成员函数,在类的作用于范围内啊![/quote] soga!明白了! 谢谢大侠解答了!
ningto.com 2014-07-19
  • 打赏
  • 举报
回复
拷贝构造也是在同一个作用域里
hero-king 2014-07-19
  • 打赏
  • 举报
回复
引用 5 楼 KangRoger 的回复:
[quote=引用 4 楼 wb136959813 的回复:] [quote=引用 3 楼 KangRoger 的回复:] 例如 List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } m_head,m_tail是私有的,但是List函数是在类内部,在类的作用域范围内,当然可以访问。 Node结点的成员是public,在外部可以访问。
那问下大侠,如果我 List that; that.m_head;that.m_tail;可以直接这样访问吗?[/quote] 不可以访问。m_head和m_tail是私有变量,只能在类的作用域内使用。[/quote] List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } 那在拷贝构造函数里为什么可以Node* node=that.m_head这样写? 有点不解啊?大侠
KangRoger 2014-07-19
  • 打赏
  • 举报
回复
引用 4 楼 wb136959813 的回复:
[quote=引用 3 楼 KangRoger 的回复:] 例如 List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } m_head,m_tail是私有的,但是List函数是在类内部,在类的作用域范围内,当然可以访问。 Node结点的成员是public,在外部可以访问。
那问下大侠,如果我 List that; that.m_head;that.m_tail;可以直接这样访问吗?[/quote] 不可以访问。m_head和m_tail是私有变量,只能在类的作用域内使用。
hero-king 2014-07-19
  • 打赏
  • 举报
回复
引用 3 楼 KangRoger 的回复:
例如 List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } m_head,m_tail是私有的,但是List函数是在类内部,在类的作用域范围内,当然可以访问。 Node结点的成员是public,在外部可以访问。
那问下大侠,如果我 List that; that.m_head;that.m_tail;可以直接这样访问吗?
KangRoger 2014-07-17
  • 打赏
  • 举报
回复
例如 List(const List& that):m_head(NULL), m_tail(NULL){ for(Node* node = that.m_head;node;node=node->m_next) push_back(node->m_data); } m_head,m_tail是私有的,但是List函数是在类内部,在类的作用域范围内,当然可以访问。 Node结点的成员是public,在外部可以访问。
hero-king 2014-07-16
  • 打赏
  • 举报
回复
that.m_head 是私有的,node节点类是内部私有节点类。 m_head和m_tail都是私有的 应该that是点不出来的吧?这点有些不明白
ma100 2014-07-16
  • 打赏
  • 举报
回复
node->m_data 是公有的啊

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧