[英]Passing a class object pointer to default constructor (C++ Subroutines)
I am working on an assignment that involves making a class called Sub that will represent subroutines. 我正在做一个涉及创建名为Sub的类的工作,该类将代表子例程。 This class will include a constructor with four arguments: name (a string), a pointer to its static parent object , number of arguments, and number of variables.
此类将包含一个带有四个参数的构造函数:名称(字符串),指向其静态父对象的指针,参数数量和变量数量。
If the static parent pointer is NULL then the object does not have a parent. 如果静态父指针为NULL,则该对象没有父。 I am stuck on how I can pass a pointer as one of the arguments to the constructor especially if I want the pointer to be NULL in order to represent the first subroutine.
我一直在坚持如何将指针作为构造函数的参数之一传递,特别是如果我希望指针为NULL以表示第一个子例程时,尤其如此。 This is what I have so far:
这是我到目前为止的内容:
class Sub
{
public:
Sub(); //constructor
Sub::Sub(string sName, Sub &sParent, int sNumberofArguments, int sNumberofLocalVariables); // default constructor
private:
string name;
Sub * parent;
int numberOfArguments;
int numberOfLocalVariables;
};
... ...
Sub::Sub(string sName, Sub &sParent, int sNumberofArguments, int sNumberofLocalVariables)
{
name = sName;
parent = &sParent;
numberOfArguments = sNumberofArguments;
numberOfLocalVariables = sNumberofLocalVariables;
}
... ...
int main(){
Sub first("first", NULL, 4, 5); //ERROR
Sub second("second", first, 3, 6);
}
让您的构造函数使用Sub *
而不是Sub &
,或者使其他构造函数不使用Sub &
并在内部将parent设置为nullptr。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.