Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;_C++_Pointers_Widget - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,pointers,widget,C++,Pointers,Widget,我不确定你称之为什么,但我想做的是首先记录在不同时间实例化的三个相同类型的小部件的内存地址 为了识别每个小部件,我想使用我为每个实例记录的内存位置,例如0x。。。。。。。等等,以再次找到小部件并识别该小部件的特征 我只是不知道如何使用内存位置并通过将其分配给指针来引用该内存位置?有人知道怎么做吗 我用int向量记录内存位置 vector<int> myvector; // to show that I have recorded three memory addresses I p

我不确定你称之为什么,但我想做的是首先记录在不同时间实例化的三个相同类型的小部件的内存地址

为了识别每个小部件,我想使用我为每个实例记录的内存位置,例如0x。。。。。。。等等,以再次找到小部件并识别该小部件的特征

我只是不知道如何使用内存位置并通过将其分配给指针来引用该内存位置?有人知道怎么做吗

我用int向量记录内存位置

vector<int> myvector;

// to show that I have recorded three memory addresses I print them out as integers.

for(int i = 0; i < myvector.size(); i++)
{
    cout << myvector[i] <<endl;
}

// then I want to use their location to identify characteristics of each widget.  

for(int i = 0; i < myvector.size(); i++)
{
    Widget_Type *tpe = myvector[i];

    // now identify the x and y value of each widget.
    cout << "x value is: " << tpe->x() << endl;
    cout << "y value is: " << tpe->y() << endl;

    //thats it?
}
向量myvector;
//为了显示我记录了三个内存地址,我将它们打印成整数。
对于(int i=0;icout正如评论中已经指出的,将指针存储为整数变量是个坏主意

只需使用
WidgetType*
作为向量值类型

您的代码将如下所示:

// use WidgetType* instead of int
vector<WidgetType*> myvector;

// print out the pointer values == memory addresses of the pointer
for(int i = 0; i < myvector.size(); i++)
{
  cout << myvector[i] <<endl;
}

// access your widgets with your stored pointers
for(int i = 0; i < myvector.size(); i++)
{
  cout << "x value is: " << myvector[i]->x() << endl;
  cout << "y value is: " << myvector[i]->y() << endl;
}
//that's it
//使用WidgetType*而不是int
向量myvector;
//打印出指针值==指针的内存地址
对于(int i=0;i不能使用
int
存储内存地址(指针)吗!使用指针存储指针。谢谢,但这不是问题;)在这种情况下,我别无选择。我特别需要知道如何使用该内存位置字符串?然后想一想:在64位平台上,类型
int
通常仍然是四个字节,其中指针是八个字节。如何在内存中存储八个字节的指针四字节类型?好吧,假设我不使用int。这是我的方法?我还可以将地址记录为什么?int可以转换成我想要使用它的上下文中使用的东西吗?有没有可行的方法可以使用或不使用整数?