C++ :: Set value 函数在函数内部设置,但后面跟着 Get 函数,设置值消失了
C++ :: Set value function sets inside function but followed by Get function the set value is gone
希望我遗漏了一些非常明显的东西:
尝试使用一些向量作为容器,使用资产 属性 修饰符 (class) 设置资产 (class) 的值。这似乎在函数调用期间有效,但是当在 set 调用之后进行 get 调用以确认时,就好像 set 调用没有发生一样。请参阅下面的代码和输出:
asset.h 文件:
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
AddProperty 工作正常,所以我忽略了它和 .cpp 不相关。只需将 push_back 添加到向量中以添加值,以及一些索引跟踪。
在资产中属性header:
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 header:
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
在 APM .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
然后当我使用这个原型时,会发生以下情况:
来自 main.cpp 示例:
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-------------------
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
如您所见,GET 返回 5.5,SET 声明它更新为 4.4,但随后的 GET 仍显示 5.5。
我可以提供的另一件事来帮助解决这个问题,如果我将 属性 设置为 public 以便我可以直接从 main 访问资产属性以进行测试,它正确更新:
//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;
那个输出是:
3.3
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 3.3
Current value = 3.3
有谁知道为什么SET函数没有粘在上面?在此先感谢您的帮助!
您正在传递资产的副本而不是对资产的引用,因此您实际上只是在更新副本,而不是实际的。您将需要通过引用传递:
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
希望我遗漏了一些非常明显的东西:
尝试使用一些向量作为容器,使用资产 属性 修饰符 (class) 设置资产 (class) 的值。这似乎在函数调用期间有效,但是当在 set 调用之后进行 get 调用以确认时,就好像 set 调用没有发生一样。请参阅下面的代码和输出:
asset.h 文件:
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
AddProperty 工作正常,所以我忽略了它和 .cpp 不相关。只需将 push_back 添加到向量中以添加值,以及一些索引跟踪。
在资产中属性header:
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 header:
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
在 APM .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
然后当我使用这个原型时,会发生以下情况:
来自 main.cpp 示例:
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-------------------
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
如您所见,GET 返回 5.5,SET 声明它更新为 4.4,但随后的 GET 仍显示 5.5。
我可以提供的另一件事来帮助解决这个问题,如果我将 属性 设置为 public 以便我可以直接从 main 访问资产属性以进行测试,它正确更新:
//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;
那个输出是:
3.3
------------GET-------------------
names vector index is 0
double property vector index is 0
Property Name are equal to 3.3
Current value = 3.3
有谁知道为什么SET函数没有粘在上面?在此先感谢您的帮助!
您正在传递资产的副本而不是对资产的引用,因此您实际上只是在更新副本,而不是实际的。您将需要通过引用传递:
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