[英]C++ recursive pointer function calls
I currently have a base class Expr
and some derivatives like AddExpr
, MultExpr
and NumExpr
. 我目前有一个基类
Expr
和一些派生类,例如AddExpr
, MultExpr
和NumExpr
。 I am trying to overload a general print()
function that recursively calls the print()
function of its members which are pointers to other Expr
objects. 我试图重载一般的
print()
函数,该函数递归调用其成员的print()
函数,这些成员是指向其他Expr
对象的指针。
//Addition Expression
AddExpr::AddExpr( Expr* l, Expr* r ) {
left = l;
right = r;
}
string AddExpr::print() {
string s;
stringstream out;
out << "(+" << left->print() << right->print() << ")";
s = out.str();
}
NumExpr::NumExpr( string n ) {
number = atoi( n.c_str() );
}
string NumExpr::print() {
string s;
stringstream out;
out << number;
s = out.str();
return s;
}
So ultimately I want the add expression to print out a ( number + number ) if the left and right pointers are numbers, ( expression + number ), ( expression + expression ), etc. by recursively calling the print()
function. 所以最终我希望通过递归调用
print()
函数,如果左右两个指针是数字,(表达式+数字),(表达式+表达式)等,则add表达式可以打印出(数字+数字)。
However I don't think I'm approaching this correctly as I am getting jumbled output :( I am not too familiar with pass by pointer and pass by reference, however every post that I have gone through with similar question are not quite relevant to my situation. 但是我不认为我正在正确处理这个问题,因为我得到的是混乱的输出:(我不太熟悉通过指针传递和通过引用传递,但是我经历过类似问题的每篇文章都与我的情况。
Here's a debug sample for calling the functions: 这是用于调用函数的调试示例:
NumExpr* left = new NumExpr("1");
cout << left->print() << endl;
NumExpr* right = new NumExpr("2");
cout << right->print() << endl;
AddExpr* temp = new AddExpr( left, right );
cout << temp->print() << endl;
Expr* temp2 = temp;
cout << temp2->print() << endl;
This will print out 1 and 2 but has the problem on the AddExpr. 这将打印出1和2,但是在AddExpr上有问题。
Any help is appreciated! 任何帮助表示赞赏!
EDIT: the header for my expression classes: 编辑:我的表达式类的标题:
class Expr {
public:
virtual string name();
virtual string print();
};
class AddExpr : public Expr {
public:
AddExpr(Expr* l, Expr* r);
string print();
private:
Expr* left;
Expr* right;
};
class NumExpr : public Expr {
public:
NumExpr( string v );
string print();
private:
int number;
};
AddExpr :: print()不返回任何值。
I see two problems with your AddExpr::print() method: 我发现您的AddExpr :: print()方法存在两个问题:
It doesn't actually return anything. 它实际上不返回任何东西。 You should have at least gotten a compiler warning, if not an error, for this.
为此,您至少应该得到一个编译器警告,即使不是错误。
It doesn't put any space between left
and right
. 它不把之间的任何空间,
left
和right
。
Try this: 尝试这个:
string AddExpr::print() {
string s;
stringstream out;
out << "(+" << left->print() << " " << right->print() << ")";
s = out.str();
return s;
}
Though actually, it would be better to do the following -- no need to create an explicit string variable: 尽管实际上,最好执行以下操作-无需创建显式的字符串变量:
string AddExpr::print() {
stringstream out;
out << "(+" << left->print() << " " << right->print() << ")";
return out.str();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.