我有一个包含指向多个类的指针的向量。类定义如下:
class trackerSocket{
public:
~trackerSocket();
int trackerInitialize(string address);
int trackerSend(string getParams);
int trackerRecv();
be_node *responseDict;
bool working;
unsigned int interval;
string trackerid;
unsigned int complete;
unsigned int incomplete;
be_dict *peersDict;
string peersBinary;
bool dictionary;
private:
string address;
string port;
string protocol;
string page;
SOCKET ConnectSocket;
int parseAnnounce(string announce);
int parseTrackerResponse(string response);
};
向量由这一行声明:
vector<trackerSocket*> trackers;
并使用以下行将类添加到向量中:
trackerSocket *temptracker = new trackerSocket();
//Initialize values in temptracker structure here (omitted)
trackers.push_back(temptracker);
//Reset temptracker
temptracker = new trackerSocket();
//Initialize values in temptracker structure here (omitted)
trackers.push_back(temptracker);
//Repeat
如何访问向量中每个类的工作变量?即使我知道某些类已将其设置为 true,以下代码也根本无法打印。
for(i = 0; i < trackers.size(); i++){
if(trackers[i]->working){
printf("Working.\n");
}
}
谢谢你的帮助 :)