迭代一个类的成员变量 [英] Iterate through member variables of a class

查看:71
本文介绍了迭代一个类的成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法迭代不同名称的成员变量,

当然,从一个while循环中的第一个逐个循环,例如?

谢谢!

解决方案

" bst" <峰; bs *** @ import.com>写道...

有没有办法迭代不同名称的成员变量,当然,从一个while循环中的第一个逐个遍历,例如?




No.


" bst" <峰; bs *** @ import.com>在留言中写道

新闻:9W ********************* @ bgtnsc04-news.ops.worldnet.att.net ...

有没有办法迭代不同名称的成员变量,当然,从一个while循环中的第一个逐个遍历?
谢谢!




我可能会做一些愚蠢的事情,但你的意思是这样吗?

即使它出了问题(并且可能有),至少我会

当每个人都纠正我时学到一些东西:)


有一件事我知道'是

类中的公共成员变量是错误的,但我留下了这些变量以便我可以随便使用它(而且我不会想到任何其他的b $ b使这个设置工作的方法)。


如果它们有效,可以将类改为模板。


#include< iostream>


使用std :: cout;


class R eadMe {


public:


ReadMe(int num1,int num2,int num3,int num4){

var1 = num1;


var2 = num2;


var3 = num3;


var4 = num4;


}


int var1;


int var2;


int var3;


int var4;


};


类阅读器{


public:


Reader(ReadMe& readFromThis):target(readFromThis){}

void printTargetVars()const;


ReadMe&目标;


};


void Reader :: printTargetVars()const {


int * arr [4] = {& target.var1,& target.var2,& target.var3,& target.var4};


int i = 0;


while(i< 4){


cout<< * arr [i]<< \ n;


++我;


}


}


int main(){


自述文件readThisOne(1,2,3,4);


ReadMe readThisTwo(5,6,7,8);


读者读者1(readThisOne);


读者读者2(readThisTwo);


reader1.printTargetVars();


reader2.printTargetVars();


返回0 ;


}


// mike tyndall


" Stephen Tyndall" < SW ******* @ hotmail.com>在消息中写道

新闻:Hv ******************** @ comcast.com ...

" ; BST" <峰; bs *** @ import.com>在消息中写道
新闻:9W ********************* @ bgtnsc04-news.ops.worldnet.att.net ...

有没有办法迭代不同名称的成员变量,例如
,从一个while循环中的第一个变量中逐个进行,例如?
谢谢!
我可能正在做一些愚蠢的事情,但你的意思是这样吗?
即使有什么问题(也可能有),至少我会在什么时候学到一些东西每个人都纠正我:)

有一件事我知道'错误的是两个类中的公共成员变量,但我把它们留下来以便我可以随便使用它(并且我无法想到任何其他方式来使这个设置工作。)



[snip] class Reader {

public:

读者(ReadMe& readFromThis):target(readFromThis){}

void printTargetVars()const;

ReadMe&目标;

};

void Reader :: printTargetVars()const {

int * arr [4] = {& target.var1 ,& target.var2,& target.var3,& target.var4};

int i = 0;

while(i< 4){

cout<< * arr [i]<< " \ n";

++ i;

}



[snip]

除了公共成员和一些可以制作的
的小调整外,对我来说还行。另一种选择是放弃个别会员,从一开始就使用

数组。


-

David Hilsee


Is there a way to iterate through member variables of different names, of
course, one by one from the very first one in a while loop, for example?
Thanks!

解决方案

"bst" <bs***@import.com> wrote...

Is there a way to iterate through member variables of different names, of
course, one by one from the very first one in a while loop, for example?



No.


"bst" <bs***@import.com> wrote in message
news:9W*********************@bgtnsc04-news.ops.worldnet.att.net...

Is there a way to iterate through member variables of different names, of
course, one by one from the very first one in a while loop, for example?
Thanks!



I''m probably doing something stupid, but did you mean something like this?
Even if something''s wrong with it (and there probably is), at least I''ll
learn something when everyone corrects me :)

One thing I do know that''s wrong is the public member variables in both
classes, but I left those in so that I could fool around with it (and I
can''t think of any other way to make this setup work).

Provided they work, the classes could be altered to be templates instead.

#include <iostream>

using std::cout;

class ReadMe {

public:

ReadMe(int num1, int num2, int num3, int num4) {

var1 = num1;

var2 = num2;

var3 = num3;

var4 = num4;

}

int var1;

int var2;

int var3;

int var4;

};

class Reader {

public:

Reader(ReadMe& readFromThis) : target(readFromThis) { }

void printTargetVars() const;

ReadMe& target;

};

void Reader::printTargetVars() const {

int* arr[4] = { &target.var1, &target.var2, &target.var3, &target.var4 };

int i = 0;

while(i < 4) {

cout << *arr[i] << "\n";

++i;

}

}

int main() {

ReadMe readThisOne(1,2,3,4);

ReadMe readThisTwo(5,6,7,8);

Reader reader1(readThisOne);

Reader reader2(readThisTwo);

reader1.printTargetVars();

reader2.printTargetVars();

return 0;

}

//mike tyndall


"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:Hv********************@comcast.com...

"bst" <bs***@import.com> wrote in message
news:9W*********************@bgtnsc04-news.ops.worldnet.att.net...

Is there a way to iterate through member variables of different names, of course, one by one from the very first one in a while loop, for example?
Thanks!
I''m probably doing something stupid, but did you mean something like this?
Even if something''s wrong with it (and there probably is), at least I''ll
learn something when everyone corrects me :)

One thing I do know that''s wrong is the public member variables in both
classes, but I left those in so that I could fool around with it (and I
can''t think of any other way to make this setup work).


[snip] class Reader {

public:

Reader(ReadMe& readFromThis) : target(readFromThis) { }

void printTargetVars() const;

ReadMe& target;

};

void Reader::printTargetVars() const {

int* arr[4] = { &target.var1, &target.var2, &target.var3, &target.var4 };

int i = 0;

while(i < 4) {

cout << *arr[i] << "\n";

++i;

}


[snip]

Looks OK to me, except for the public members and a few minor tweaks that
could be made. Another option is to forego individual members and just use
an array from the start.

--
David Hilsee


这篇关于迭代一个类的成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆