为了教学目的,我一直在尝试实现自己的链表类.
我在Iterator声明中指定了"List"类作为朋友,但它似乎没有编译.
这些是我使用过的3个类的接口:
Node.h:
#define null (Node<T> *) 0
template <class T>
class Node {
public:
T content;
Node<T>* next;
Node<T>* prev;
Node (const T& _content) :
content(_content),
next(null),
prev(null)
{}
};
Run Code Online (Sandbox Code Playgroud)
Iterator.h:
#include "Node.h"
template <class T>
class Iterator {
private:
Node<T>* current;
Iterator (Node<T> *);
public:
bool isDone () const;
bool hasNext () const;
bool hasPrevious () const;
void stepForward ();
void stepBackwards ();
T& currentElement () const;
friend class List<T>;
};
Run Code Online (Sandbox Code Playgroud)
List.h
#include <stdexcept>
#include "Iterator.h"
template …Run Code Online (Sandbox Code Playgroud) 假设我有这个程序:
class Foo {
public:
unsigned int bar () {
static unsigned int counter = 0;
return counter++;
}
};
int main ()
{
Foo a;
Foo b;
}
Run Code Online (Sandbox Code Playgroud)
(当然这个例子没有任何意义,因为我显然将"counter"声明为私有属性,但它只是为了说明问题).
我想知道C++在这种情况下的行为:bar()方法中的变量"counter"对于每个实例都是一样的吗?
出于学术目的,我必须编写一个绘制用户输入表达式的应用程序,如:f(x)= 1 - exp(3 ^(5*ln(cosx))+ x)
我选择编写解析器的方法是使用Shunting-Yard算法转换RPN中的表达式,将原始函数如"cos"视为一元运算符.这意味着上面写的函数将被转换为一系列令牌,如:
1, x, cos, ln, 5, *,3, ^, exp, -
Run Code Online (Sandbox Code Playgroud)
问题是绘制我必须要评估它的函数很多次,因此对每个输入值应用堆栈评估算法将是非常低效的.我怎么解决这个问题?我是否必须忘记RPN的想法?
而不是做
#include "MyClass.cpp"
Run Code Online (Sandbox Code Playgroud)
我想要做
#include "MyClass.h"
Run Code Online (Sandbox Code Playgroud)
我在网上看到,不这样做被认为是不好的做法.
我已经定义了一个像这样的"Action"纯抽象类:
class Action {
public:
virtual void execute () = 0;
virtual void revert () = 0;
virtual ~Action () = 0;
};
Run Code Online (Sandbox Code Playgroud)
并表示用户可以使用类执行的每个命令.
对于实际的撤消/重做,我想做这样的事情:
解开
Action a = historyStack.pop();
a.revert();
undoneStack.push(a);
Run Code Online (Sandbox Code Playgroud)
重做
Action a = undoneStack.pop();
a.execute();
historyStack.push(a);
Run Code Online (Sandbox Code Playgroud)
编译器显然不接受这个,因为"Action"是一个无法实现的抽象类.
那么,我是否必须重新设计所有内容,或者是否有解决此问题的简单方法?
c++ ×4
class ×1
coding-style ×1
friend ×1
include ×1
inheritance ×1
math ×1
parsing ×1
performance ×1
rpn ×1
templates ×1
undo-redo ×1