Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我可以在赋值运算符内部调用构造函数吗?_C++_Assignment Operator - Fatal编程技术网

C++ 我可以在赋值运算符内部调用构造函数吗?

C++ 我可以在赋值运算符内部调用构造函数吗?,c++,assignment-operator,C++,Assignment Operator,我可以在赋值运算符中调用对象的构造函数吗 我有这个密码 class ActiveArea { public: ActiveArea(const ActiveArea& active_area) : m_name(active_area.GetName()), m_boundary(active_area.GetBoundary()), m_day_music(active_area.GetDayMusic()), m_night_music

我可以在赋值运算符中调用对象的构造函数吗

我有这个密码

class ActiveArea
{
    public:
        ActiveArea(const ActiveArea& active_area) : m_name(active_area.GetName()),
    m_boundary(active_area.GetBoundary()),
    m_day_music(active_area.GetDayMusic()),
    m_night_music(active_area.GetNightMusic()),
    m_custom_script(active_area.GetCustomScript()),
    m_hp_regen(active_area.GetHPRegen()),
    m_mp_regen(active_area.GetMPRegen()),
    m_pvp_ability(active_area.GetPVPAbility()),
    m_is_ladianes_suffle(active_area.IsLadianesSuffle()),
    m_is_no_pvp(active_area.IsNoPVP()),
    m_is_no_stamina(active_area.IsNoStamina()),
    m_is_no_booth(active_area.IsNoBooth()),
    m_is_under_siege(active_area.IsUnderSiege()),
    m_is_no_minimap(active_area.IsNoMiniMap()),
    m_is_no_attack(active_area.IsNoAttack()),
    m_can_teleport_from(active_area.CanTeleportFrom()),
    m_can_teleport_to(active_area.CanTeleportTo()),
    m_can_login_to(active_area.CanLoginTo()),
    m_min_level_required(active_area.GetMinLevelRequired()),
    m_max_level_required(active_area.GetMaxLevelRequired())
{

};

ActiveArea &operator=(const ActiveArea &rhs)
{
    return ActiveArea(rhs);
}
private:
    const std::string m_name;
    const Boundary* m_boundary;
    const std::string m_day_music;
    const std::string m_night_music;
    const std::string m_custom_script;
    const int m_hp_regen;
    const int m_mp_regen;
    const std::string m_pvp_ability;
    const bool m_is_ladianes_suffle;
    const bool m_is_no_pvp;
    const bool m_is_no_stamina;
    const bool m_is_no_booth;
    const bool m_is_under_siege;
    const bool m_is_no_minimap;
    const bool m_is_no_attack;
    const bool m_can_teleport_from;
    const bool m_can_teleport_to;
    const bool m_can_login_to;
    const int m_min_level_required;
    const int m_max_level_required;
};
但是当我试图用这个编译我的程序时,我得到了一堆警告,说“返回局部变量或临时变量的地址”

由于我将警告视为错误,所以我想让它正常工作

我基本上希望能够做到这一点

ActiveArea area;
ActiveArea area2;
area = area2;

警告来自这一行:

    return ActiveArea(rhs);
在重载运算符内部。此行的作用是创建一个临时实例,并返回其地址。这正是警告所说的

我可以在赋值运算符中调用对象的构造函数吗

是的,在这方面没有限制。但是,必须确保赋值运算符的语义是强制的。对你来说,这个接线员

ActiveArea &operator=(const ActiveArea &rhs)
可以修改调用它的对象,使其状态等同于
rhs
,并返回对该对象的引用。您的示例不满足这两个条件中的任何一个,而且还返回了对本地对象的引用。使用该引用将是未定义的行为。通常,您需要设置对象的状态,然后
返回*this

ActiveArea &operator=(const ActiveArea &rhs)
{
  // set the state of this object using rhs
  ...
  return *this;
}
使用构造函数的一个有效示例是在复制和交换习惯用法中使用复制构造函数:

请注意,复制可以通过将参数从引用更改为值隐式完成

ActiveArea &operator=(ActiveArea rhs)
{
  swap(rhs); // assume swap is a member function
  return *this;
}

最后,请注意,您的特定类中充满了
const
数据成员。这意味着赋值在这种情况下没有任何意义,因为真正的赋值会修改对象的状态,而您不能修改
const
数据成员。

想想赋值操作符的角色是什么。它应该修改指定给的对象。它通常返回一个对该对象的引用。你认为赋值运算符(一步一步)会做什么?不是它的地址,而是对它的引用。语义很重要。你的陈述是错误的。它会让初学者感到困惑。@juanchopanza-1。警告上写着地址,请去和编译器争论。2.引用使用地址来传输“引用”,作为返回值,保存临时实例的变量的地址被使用引用可以使用地址作为实现细节。但是返回类型是一个引用。该函数不返回地址。返回引用和返回地址在C++中都有不同的含义。我不认为混淆术语有什么意义。为了使用swap,像m_name这样的变量不是必须是常量吗@juanchopanza@Ricky抢手货为了拥有任何类型的合理赋值运算符,数据成员都不应为常量。我加了一张便条。有道理,谢谢!回答得很好。老实说,这是我在这里遇到的最好的/信息量最大的一个。现在效果很好。
ActiveArea &operator=(ActiveArea rhs)
{
  swap(rhs); // assume swap is a member function
  return *this;
}