c ++如何正确引用类属性的值

时间:2017-06-23 13:42:35

标签: c++ oop

我不知道为什么会发生这种情况,但似乎代码“丢失”了属性的值。我得到了一个定义为(在Foo.h中)的类:

#ifndef FOO_H
#define FOO_H

using namespace std;
#include <string>

class Foo{
 public:
  Foo(short int id);
  void Foo::DoSomething(std::string someMsg);
 private:
  short int id;
};
#endif /* FOO_H */

在类实现(Foo.cpp)中:

#include "Foo.h"
#include <iostream>

Foo::Foo(short int id) {
 this->id = id;
 cout << "value now: " << this->id << "\n"; // I print to be sure it was set correctly and it prints the right value
}
void Foo::DoSomething(std::string someMsg) {
cout <<  "Foo number: " << std::to_string(this->id) << someMsg;
// when it runs this code called by another class, it always prints 0 to this->id for all objects instantiated
}

然后,我得到另一个班,巴尔。在Bar.h,我有:

#ifndef BAR_H
#define BAR_H
#include <iostream>
using namespace std;
#include <list>
#include "Foo.h"

class Bar {
public:
 Bar();
 void setValor(std::string valor);
 void notifyAllFoos();
 void Bar::registerFoo(Foo *h);
private:
 std::list<Foo> foos;

};
#endif /* BAR_H */

在Bar.cpp中我有:

#include "Bar.h"

void Bar::setValor(std::string valor){
 // do more stuff
 this->notifyAllFoos();
}

void Bar::registerFoo(Foo *h){
   this->foos.push_back(*h);
}

void Bar::notifyAllFoos(){
for(std::list<Foo>::iterator it=this->foos.begin() ; it!=this->foos.end() ; it++){
    it->DoSomething("myMsg");
 }
}

最后,在main.cpp中:

#include <cstdlib>
#include "Bar.h"
#include "Foo.h"
using namespace std;


int main(int argc, char** argv) {
 Bar *b = new Bar();

 b->registerFoo(new Foo(1));
 b->registerFoo(new Foo(2));
 b->registerFoo(new Foo(3));

 b->setValor("Value");
 delete b;
 return 0;
}

基本上,Bar必须通知Foo所有list已发生更新的值并打印消息。为了识别每个Foo我放了id,但它会为所有人保持打印0。 为什么会这样?它一定是非常简单的我猜,但我不习惯c ++。提前谢谢。

2 个答案:

答案 0 :(得分:1)

这:

std::list<Foo> foos;

将存储来自班级Foo副本

您需要存储指针,如下所示:

std::list<Foo*> foos;

PS:完成后,永远不要忘记每个delete new

答案 1 :(得分:0)

我确实把你的代码放到一个文件中,添加了include并在顶部使用并注释掉Bar构造函数的声明。

#include <string>
#include <iostream>

using namespace std;


class Foo{
 public:
  Foo(short int id);
  void Foo::DoSomething(std::string someMsg);
 private:
  short int id;
};

Foo::Foo(short int id) {
 this->id = id;
 cout << "value now: " << this->id << "\n"; // I print to be sure it was set correctly and it prints the right value
}
void Foo::DoSomething(std::string someMsg) {
cout <<  "Foo number: " << std::to_string(this->id) << someMsg;
// when it runs this code called by another class, it always prints 0 to this->id for all objects instantiated
}

#include <list>

class Bar {
public:
 //Bar();
 void setValor(std::string valor);
 void notifyAllFoos();
 void Bar::registerFoo(Foo *h);
private:
 std::list<Foo> foos;

};

void Bar::setValor(std::string valor){
 // do more stuff
 this->notifyAllFoos();
}

void Bar::registerFoo(Foo *h){
   this->foos.push_back(*h);
}

void Bar::notifyAllFoos(){
for(std::list<Foo>::iterator it=this->foos.begin() ; it!=this->foos.end() ; it++){
    it->DoSomething("myMsg");
 }
}

int main () {
Bar *b = new Bar();

b->registerFoo(new Foo(1));
b->registerFoo(new Foo(2));
b->registerFoo(new Foo(3));

b->setValor("Value");

}

它给了我以下输出

value now: 1
value now: 2
value now: 3
Foo number: 1myMsgFoo number: 2myMsgFoo number: 3myMsg

这是你在找什么?