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

c++ how to correctly reference a class attribute's value

本文关键字:属性 引用 何正确 C++      更新时间:2023-10-16

我不知道为什么会发生这种情况,但似乎代码正在"失去"属性的值。我的课程定义为(在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中。我有:

#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 i有:

#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必须通知其list的所有Foo S,即发生值的更新并打印一个MSG。要识别每个Foo,我将此id放置,但它不断打印0。为什么会发生这种情况?我想这一定很简单,但我不习惯C 。预先感谢。

that:

std::list<Foo> foos;

将存储来自类Foo的对象的 。

您需要存储这样的指针,这样:

std::list<Foo*> foos;

ps:在完成时,永远不要忘记每个newdelete

我确实将您的代码放入一个文件中,添加并在顶部使用并使用了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

您要寻找的是吗?