代码之家  ›  专栏  ›  技术社区  ›  FDRJ

C++::Set value函数在函数内部设置,但后跟Get函数。设置值消失[关闭]

  •  0
  • FDRJ  · 技术社区  · 7 年前

    希望我错过了一些非常明显的东西:

    资产。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_推回向量以添加值,并进行一些索引跟踪。

    namepsace properties {
    struct AssetPropertyDouble
    {
    private:
      friend class AssetPropertyModifier;
      double real_number_value_;
    public:
      AssetPropertyDouble(double d) : real_number_value_(d) {}
    };//struct AssetPropertyDouble
    }//namespace proeprties
    

    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
    

    然后,当我使用这个原型时,我得到了以下结果:

    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
    

    //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
    

    有人知道为什么集合函数不在上面吗?提前感谢您的帮助!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Easton Bornemeier    7 年前

    您传递的是资产的副本,而不是对资产的引用,因此实际上只更新副本,而不是实际的副本。您需要通过引用传递:

    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