这里是接口C++私有变量范围的规则
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;
};
这里是它是类stackType的一部分延伸stackADT
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原因就不允许这样做。
*不会允许这个* - 你能更具体地说明这是什么吗?你的代码也声明'stackADT',但使用'stackType'。这是什么? –
我说这就是接口, 模板 类stackType:公共stackADT { 私人...... 它延伸 –
c3cris
我明白了。发布SSCCE(http://sscce.org)是一个不错的主意,以便我们掌握整个情况。就目前而言,你的问题并没有描述'stackADT'和'stackType'之间的关系,这使得它变得混乱。 –