简体   繁体   中英

Can I call a Constructor inside of an assignment operator?

Can I call a constructor of an object inside of an assignment operator....

I have this code....

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;
};

But when i try to compile my program using this I get a bunch of warnings, saying "Returning address of local variable or temporary".

Since I treat warnings as errors I would like to get this working...

I essentially want to be able to do this...

ActiveArea area;
ActiveArea area2;
area = area2;

Can I call a constructor of an object inside of an assignment operator?

Yes, there are no restrictions in that regard. However, you must make sure that the semantics of the assignment operator are enforced. In your case, this operator

ActiveArea &operator=(const ActiveArea &rhs)

could be expected to modify the object it is invoked on such that its state is equivalent to that of rhs , and to return a reference to that object. Your example doesn't satisfy either of those criteria, and furthermore, returns a reference to a local object. To use that reference would be undefined behaviour. Typically, you'd set the state of your object, then return *this .

ActiveArea &operator=(const ActiveArea &rhs)
{
  // set the state of this object using rhs
  ...
  return *this;
}

An valid example of using a constructor would be to use the copy constructor in the copy and swap idiom :

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

Note that the copying can done implicitly by changing the parameter from reference to value

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

Finally, note that your particular class is full of const data members. That means assignment doesn't really make any sense in this case, because a real assignment would modify the state of an object and you can't modify const data members.

The warning comes from this line:

    return ActiveArea(rhs);

Inside your overloaded operator. What this line does is create a temporary instance, and return it's address. That's exactly what the warning says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM