C++ 如何正确引用类属性的值
c++ how to correctly reference a class attribute's value
我不知道为什么会发生这种情况,但似乎代码正在"失去"属性的值。我的课程定义为(在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:在完成时,永远不要忘记每个new
的delete
。
我确实将您的代码放入一个文件中,添加并在顶部使用并使用了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
您要寻找的是吗?
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 初始化类C++私有属性(值/引用/静态)
- C++ 如何正确引用类属性的值
- C++对象共享属性 - 使用指针或引用
- 具有仅在某些情况下引用外部变量的属性的类
- Const 引用字段作为类中的只读属性C++
- C++引用基类中的子类指针属性
- 返回泛型函数中对象的引用(具有指针或非指针属性)
- 如何在C++中将结构属性转换为指针引用
- 索引属性如何返回引用
- spirit::qi :将继承的属性引用传递给 phoenix::function
- 提升精神 如何将本地引用作为属性传递
- 使用一个 GL ELEMENT_ARRAY_BUFFER引用 0 中的每个属性
- c++中引用类型的属性
- 如何引用的形式和改变属性(高度和宽度)从一个函数在Qt
- QML 和C++属性 - 引用错误:找不到变量
- 在c++中是否有一个很好的理由在不改变属性的情况下引用它?