Reloading "=" for private element



  • class A
    {
    private:
      int x;
    };
    

    We need to overload the operator. = for xeven though it's a standard type. KAC?



  • You can only overload operators for user types: classes, associations, transfers. You can't overload the transfer operator for whole numbers.

    Or more accurate (C++ standard, 13.5 Overloaded operators )

    6 An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

    For example, the assignee ' s operator may be overloaded from your example when the class object is assigned a calculated value.

    class A
    {
    private:
        int x;
    public:
        A & operator =( int x )
        {
            A::x = x;
            return *this;
        }
    };
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2