希望我错过了一些非常明显的东西:
尝试使用某些向量作为容器,使用资产属性修饰符(类)设置资产(类)的值。这似乎在函数调用期间有效,但是当使用get调用来跟随set调用进行确认时,就好像没有发生set调用一样。请参阅下面的代码和输出:
asset.h文件:
//selectedOffice: '',
selectedProductType: [],
officeList: [
{
code: 'Blue Office',
jira: true
},
{
code: 'Red Office',
jira: false
}
...snip...
],
productList: [
{
type: 'comics',
url: 'www.comicsurl.com'
},
{
type: 'videos',
url: 'www.videosurl.com'
}
....snip...
]
AddProperty正常工作,所以我省略了它,并且.cpp不相关。只是将push_back插入向量中以添加值,以及一些索引跟踪。
在资产属性标题中:
namespace assets {
class Asset
{
friend class AssetPropertyModifier;
std::vector<properties::AssetPropertyDouble> asset_property_double_;
enum property_id_ { ZERO, USHORT, FLOAT, INT, UINT, DOUBLE, VEC3, STRING,
BOOL};
public:
void AddProperty(std::string property_name, double property_value);
};//class Asset
}//namespace assets
这是APM标题:
namepsace properties {
struct AssetPropertyDouble
{
private:
friend class AssetPropertyModifier;
double real_number_value_;
public:
AssetPropertyDouble(double d) : real_number_value_(d) {}
};//struct AssetPropertyDouble
}//namespace proeprties
在APM .cpp文件中:
namespace assets {
class Asset;
class AssetPropertyModifier
{
int LookUpVectorIndex(Asset a, int type , int index);
int LookUpNameIndex(Asset a, std::string s);
public:
double GetPropertyValue(Asset a, std::string property_name,
double not_used_just_an_overloader);
void SetPropertyValue(Asset a, std::string property_name,
double new_double_value);
};//class AssetPropertyModifier
}//namespace assets
然后,当我使用这个原型时,我会发生以下事情:
来自main.cpp示例:
namespace assets {
//LookUpNameIndex definition
//LookUpVectorIndex defintion
double AssetPropertyModifier::GetPropertyValue(Asset a,
std::string property_name,
double not_used_just_an_overloader)
{
//Using Verbose output to debug:
std::cout << "------------GET-------------------" << std::endl;
int name_index = LookUpNameIndex(a, property_name);
std::cout << "names vector index is " << name_index << std::endl;
int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index);
std::cout << "double property vector index is " << double_index <<
std::endl;
double property_value =
a.asset_property_double_.at(double_index).real_number_value_;
std::cout << property_name << " are equal to " << property_value <<
std::endl;
return property_value;
}
void AssetPropertyModifier::SetPropertyValue(Asset a,
std::string property_name,
double new_double_value)
{
//Using Verbose output to debug:
std::cout << "------------SET-------------------" << std::endl;
int name_index = LookUpNameIndex(a, property_name);
std::cout << "names vector index is " << name_index << std::endl;
int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index);
std::cout << "double vector index is " << double_index << std::endl;
a.asset_property_double_.at(double_index).real_number_value_ =
new_double_value;
std::cout << property_name << " was changed to " <<
a.asset_property_double_.at(double_index).real_number_value_
<< std::endl;
}//SetPropertyValue
}//namespace assets
这是结果输出:
std::string some_name = "Property Name";
double some_initial_value = 5.5;
double some_new_value = 4.4;
assets::AssetPropertyModifier apm;
assets::Asset my_asset;
main {
my_asset.AddProperty(some_name, some_initial_value);
std::cout << "Current value = " << apm.GetPropertyValue(my_asset,
some_name , _DOUBLE_)
<< std::endl;
apm.SetPropertyValue(my_asset, some_name , some_new_value );
std::cout << "Current value = " << apm.GetPropertyValue(my_asset,
some_name , _DOUBLE_)
<< std::endl;
}
正如你在上面看到的那样,GET返回5.5并且SET声明它更新为4.4但是后面的GET仍然显示5.5。
我可以提供的另一件事来帮助解决这个问题,如果我将该属性设置为公共,以便我可以直接从main访问Asset属性以进行测试,它会正确更新:
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 5.5
Current value = 5.5
------------SET-------------------
names vector index is 0
double vector index is 0
Property Name was changed to 4.4
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 5.5
Current value = 5.5
该输出是:
//hard SET with public access to the vector and the property struct value
my_asset.asset_property_double_.at(0).real_number_value_ = 3.3;
//hard GET with public access:
std::cout << my_asset.asset_property_double_.at(0).real_number_value_ <<
std::endl;
//GET through APM but with public values still enabled:
std::cout << "Current value = " << apm.GetPropertyValue(my_asset, some_name,
_DOUBLE_)
<< std::endl;
有谁知道为什么SET功能没有坚持上面?在此先感谢您的帮助!
答案 0 :(得分:1)
您传递的是资产的副本,而不是对资产的引用,因此您实际上只是更新副本,而不是实际的副本。您需要通过引用传递:
void AssetPropertyModifier::SetPropertyValue(Asset& a, //<----added '&'
std::string property_name,
double new_double_value)
{
//Using Verbose output to debug:
std::cout << "------------SET-------------------" << std::endl;
int name_index = LookUpNameIndex(a, property_name);
std::cout << "names vector index is " << name_index << std::endl;
int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index);
std::cout << "double vector index is " << double_index << std::endl;
a.asset_property_double_.at(double_index).real_number_value_ =
new_double_value;
std::cout << property_name << " was changed to " <<
a.asset_property_double_.at(double_index).real_number_value_
<< std::endl;
}//SetPropertyValue