我想在c++中声明一个指针:
class MyAgent : public Agent
{
private:
int x,y;
int* xPtr;
int *yPtr;
void add();
public:
MyAgent(); // Declaration of constructor
};
MyAgent::MyAgent() : Agent(PT_UDP)
{
xPtr= &x;
yPtr= &y;
bind("x_otcl", &x);
bind("y_otcl", &y);
}
void MyAgent::add()
{
Tcl& tcl = Tcl::instance();
tcl.eval("puts \"From add() \"");
printf("\nx=%d and y = %d\nx+y = %d\n", x,y,x+y);
printf("xPtr=%d\n",*xPtr);
printf("yPtr=%d\n",*yPtr);
}xPtr和yPtr有什么区别?xPtr是指针吗?编译器会忽略这个空间吗?从结果来看,我认为两者都是指针,但我不确定。我总是使用第二个(yPtr)来声明指针。
另外,请告诉我如何在c++和otcl之间绑定指针。
发布于 2014-05-08 06:16:05
xPtr和yPtr都是指向整数的指针。是的,编译器忽略了这个空间。
若要将oTcl变量(x和y)与C++变量(*xPtr和*yPtr)绑定,请使用以下代码。
Tcl& tcl = Tcl::instance();
tcl.eval("set variable_in_tcl");
variable_in_c++ = atoi(tcl.result());正如我所理解的,您正在调用add()函数,它将从oTcl脚本中获取变量xPtr和yPtr值,并打印变量的值。因此,代码如下所示:
class MyAgent : public Agent
{
private:
// Use x and y in tcl script
int* xPtr;
int *yPtr;
void add();
public:
MyAgent(); // Declaration of constructor
};
MyAgent::MyAgent() : Agent(PT_UDP)
{
Tcl& tcl = Tcl::instance();
tcl.eval("set x");
*xPtr = atoi(tcl.result());
tcl.eval("set y");
*yPtr = atoi(tcl.result());
}
void MyAgent::add()
{
printf("\nx=%d and y = %d\nx+y = %d\n", *xPtr,*yPtr,*xPtr+*yPtr);
printf("xPtr=%d\n",*xPtr);
printf("yPtr=%d\n",*yPtr);
}https://stackoverflow.com/questions/23518324
复制相似问题