c++ - Can I call a Constructor inside of an assignment operator? -


can call constructor of object inside of assignment operator....

i have 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 try compile program using bunch of warnings, saying "returning address of local variable or temporary".

since treat warnings errors working...

i want able this...

activearea area; activearea area2; area = area2; 

can call constructor of object inside of assignment operator?

yes, there no restrictions in regard. however, must make sure semantics of assignment operator enforced. in case, operator

activearea &operator=(const activearea &rhs) 

could expected modify object invoked on such state equivalent of rhs, , return reference object. example doesn't satisfy either of criteria, , furthermore, returns reference local object. use reference undefined behaviour. typically, you'd set state of object, return *this.

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

an valid example of using constructor use copy constructor in copy , swap idiom:

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

note copying can done implicitly changing parameter reference value

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

finally, note particular class full of const data members. means assignment doesn't make sense in case, because real assignment modify state of object , can't modify const data members.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -