我已经通过以下程序成功编写了一个单向链表: 头文件是:
#ifndef SLL_H_
#define SLL_H_
#include <iostream>
class node {
protected:
public:
int key;
node *next;
node();
~node();
};
class SLL : public node{
private:
node *Head = NULL;
int SLL_SIZE = 0;
public:
//Constructor
SLL();
//SLL(int n);
//Destructor
~SLL();
//Modifiers
void Push_Front(int a);
void Push_Back(SLL A,int b);
void Traverse();
//Access function
int SLL_size();
int Get(node* p);
//Iterator
node* Begin();
node* End();
//void Search(int a);
};
#endif
SLL.cpp
#include "SLL.h"
#include <iostream>
using namespace std;
node::node(){
cout << "Empty constructor of node is being called;" << endl;
}
node::~node(){
cout << "Empty destructor of node is being called;" << endl;
}
SLL::SLL():node(){
cout << "Empty constructor of SLL is being called;" << endl;
}
SLL::~SLL(){
cout << "Empty destructor of SLL is being called." << endl;
}
//Insert element at the front of the list
void SLL::Push_Front(int k){
node *temp = new node [1];
temp->key = k;
temp->next = Head;
Head = temp;
SLL_SIZE = SLL_SIZE + 1;
}
//Insert element at the end of the list
void SLL::Push_Back(SLL A, int m){
node *temp1 = A.End();
node *temp2 = new node [1];
temp2->key = m;
temp1->next = temp2;
temp2->next = NULL;
SLL_SIZE = SLL_SIZE + 1;
}
//Insert element at a given position
//Return the number of elements in the linked list
int SLL::SLL_size(){
return SLL_SIZE;
}
//Traverse the list (print the list)
void SLL::Traverse(){
node *temp;
temp = Head;
while(temp!=NULL){
cout << temp->key << " ";
temp = temp->next;
}
cout << endl;
}
//Get key given pionter
int SLL::Get(node* pt){
if(pt!=NULL){
node* temp = pt;
return temp->key;
}
else {
cout << "Null pointer points to nowhere!" << endl;
return 0;
}
}
//Return the pointer at the beginning of the list
node* SLL::Begin(){
return Head;
}
//Return the pointer at the end of the list
node* SLL::End(){
node* temp = Head;
while(temp->next!=NULL){
temp = temp->next;
}
return temp;
}
主要.cpp
#include <iostream>
#include "SLL.h"
using namespace std;
int main()
{
SLL A;
A.Push_Front(1);
A.Push_Front(2);
A.Push_Front(5);
A.Push_Front(6);
A.Push_Back(A,3);
A.Traverse();
cout << A.SLL_size() << endl;
cout << A.Get(A.Begin()) << endl;
cout << A.Get(A.End()) << endl;
return 0;
}
例如,一个错误是:
SLL.h||In member function 'void SLL::Push_Front(int)':|
SLL.h|7|error: 'int node::key' is protected|
SLL.cpp|25|error: within this context|
SLL.h|8|error: 'node* node::next' is protected|
SLL.cpp|26|error: within this context|
SLL.h||In member function 'void SLL::Push_Back(SLL, int)':|
SLL.h|7|error: 'int node::key' is protected|
SLL.cpp|35|error: within this context|
SLL.h|8|error: 'node* node::next' is protected|
LL.cpp|36|error: within this context|
SLL.h|8|error: 'node* node::next' is protected|
SLL.cpp|37|error: within this context|
使用 key 和 next 的其他成员函数出现类似错误。
这个程序现在运行良好。但是,在我移动 node
类中的两行之后,int key; node *next;
在 protected
下,然后它给我错误,例如“node::key is protected”。
首先,请不要怪我做了一些愚蠢的事情 :P 。我知道如果我为节点 struct
那么生活会容易得多。我正在尝试实践继承并了解 protected 。这就是为什么。
根据定义,派生类可以访问 protected 成员,对吗?我不知道我哪里做错了。
希望你能帮帮我。谢谢!
最佳答案
protected
关键字允许继承类查看protected
成员。这意味着继承类的实例可以看到自己的 protected 成员,以及同一继承类的其他实例的 protected 成员。它不会通过指向父类类型的指针来扩展此访问,因为该访问不安全。
让我们把它具体化。考虑以下示例:
class A
{
protected:
int a_int;
};
class B : public A
{
public:
int good()
{
return a_int; // OK: Protected member of this instance
}
int bad( A *a_ptr )
{
return a_ptr->a_int; // BAD: Can't access the protected member
// through a pointer to the parent class type.
}
int also_good( B *b_ptr )
{
return b_ptr->a_int; // OK: Pointer to the same class type as this
// class is safe.
}
};
您的代码中的错误看起来像第二种情况。那为什么第二种情况不合法,而第三种情况可以呢?
第二种情况是非法的,因为编译器不知道A*
指向的对象的实际类型。它可以是A
的任何 后代,甚至可能无法转换为B*
。因此,不能保证 protected
子句扩展的访问是安全的或有意义的。例如,假设您有
class C : public A { ... };
class D : public C { ... };
并且您将 C*
或 D*
传递到上面的方法 bad()
中。 B
应该能够看到暴露给 C
的 protected 成员似乎不合理,因为 C
和 B
没有直接关系。 D
也是如此。
但是,在第三种情况下,编译器肯定知道它有一个指向 B
或从 B
派生的类的指针,因此它知道访问扩展通过 protected
关键字是安全且有意义的。我的意思是, protected 字段的管理方式与 B
期望的方式相同。事实上,如果没有这种访问权限,您将很难编写涉及 B
有道理吗?
如果您仍然不相信:假设我创建了两个并行类,它们都继承自 node
:
// plain singly linked list
class normal_sll : public node { };
// singly linked list that stores all of its elements negated
class negative_sll : public node { };
当然,这是一个人为的例子,但请耐心等待。因为这两个类都派生自 node
,所以您可以通过 node *
传递任一类。因此,您可以将 negative_sll
的实例传递给 normal_sll
,反之亦然。
不过,C++ 的访问控制会阻止任一类通过该node *
查看 protected 字段。这很好,因为 negative_sll
管理它们的方式与 normal_sll
不同。
但是,您不能通过 normal_sll*
传递 negative_sll
的实例,反之亦然。所以,如果您在 normal_sll
的方法之一中有一个 normal_sll*
,您就知道访问 protected 成员是安全的。
当然,这是一个人为的例子。我相信你能想到一个更好的。有道理吗?
现在您可以使B
成为A
的 friend
并覆盖此控件。但是,这会让 B
看到 A
的 private 成员,完全绕过了 protected
概念。更好的解决方案是重写您的 SLL
代码,以便将 SLL*
而不是 node*
传递给它的方法。
关于c++ - 如何在单链表中正确使用protected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590001/