C++ 私有变量作用域规则
C++ private variable scoping rules
这是界面
template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
virtual bool isEmptyStack() const = 0;
virtual bool isFullStack() const = 0;
virtual void push(const Type& newItem) = 0;
virtual Type top() const = 0;
virtual void pop() = 0;
virtual void reverseStack(stackType<Type>& otherStack) = 0;
private:
int maxStackSize;
int stackTop;
Type *list;
};
这是扩展 stackADT
的 class stackType 的一部分的反向堆栈方法
template <class Type>
class stackType : public stackADT<Type>
{
private:
int maxStackSize;
int stackTop;
Type *list;
public:
/***
Other methods ...
**/
void reverseStack(stackType<Type>& otherStack)
{
int count = 0;
otherStack.list = new Type[maxStackSize]; // why does this WORK!!! its private
otherStack.stackTop = 0; // why does this WORK!!! its private
//copy otherStack into this stack.
for (int j = stackTop - 1; j >= 0; j--)
{
otherStack.push(list[j]);
count++;
}
}
这是调用的主循环。
stackType<int> stack1(50);
stackType<int> stack2(50);
stack1.initializeStack();
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.push(4);
stack1.push(5);
stack1.reverseStack(stack2);
所以在 C++ 中这是怎么回事,因为在 Java、PHP、Python(损坏的命名)和其他 OOD 中不允许这样做。
如图所示,无法编译。
重点是函数 reverseStack
应该是 member 方法,所以它应该以:
开头
void stackADT::reverseStack(stackType<Type>& otherStack)
作为成员方法,它可以访问 private
个变量。
我猜你对 private 的实际作用感到困惑,因为这也适用于 Java。
Private 意味着其他 classes(或没有 classes,意思是函数)的实例不能 change/call/invoke/... member/method。这里的重要部分是它说 other classes。相同 class 的实例可以 change/call/invoke/... 私有 members/methods。
这是界面
template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
virtual bool isEmptyStack() const = 0;
virtual bool isFullStack() const = 0;
virtual void push(const Type& newItem) = 0;
virtual Type top() const = 0;
virtual void pop() = 0;
virtual void reverseStack(stackType<Type>& otherStack) = 0;
private:
int maxStackSize;
int stackTop;
Type *list;
};
这是扩展 stackADT
的 class stackType 的一部分的反向堆栈方法template <class Type>
class stackType : public stackADT<Type>
{
private:
int maxStackSize;
int stackTop;
Type *list;
public:
/***
Other methods ...
**/
void reverseStack(stackType<Type>& otherStack)
{
int count = 0;
otherStack.list = new Type[maxStackSize]; // why does this WORK!!! its private
otherStack.stackTop = 0; // why does this WORK!!! its private
//copy otherStack into this stack.
for (int j = stackTop - 1; j >= 0; j--)
{
otherStack.push(list[j]);
count++;
}
}
这是调用的主循环。
stackType<int> stack1(50);
stackType<int> stack2(50);
stack1.initializeStack();
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.push(4);
stack1.push(5);
stack1.reverseStack(stack2);
所以在 C++ 中这是怎么回事,因为在 Java、PHP、Python(损坏的命名)和其他 OOD 中不允许这样做。
如图所示,无法编译。
重点是函数 reverseStack
应该是 member 方法,所以它应该以:
开头
void stackADT::reverseStack(stackType<Type>& otherStack)
作为成员方法,它可以访问 private
个变量。
我猜你对 private 的实际作用感到困惑,因为这也适用于 Java。
Private 意味着其他 classes(或没有 classes,意思是函数)的实例不能 change/call/invoke/... member/method。这里的重要部分是它说 other classes。相同 class 的实例可以 change/call/invoke/... 私有 members/methods。