Intersection of assignment operators?

This is the time for my first question. How do you intersect assignment operators between two classes?

class B;

class A {
public:
A &operator = ( const B &b );
friend B &B::operator = ( const A &a ); //compiler error
};

class B {
public:
B &operator = ( const A &a );
friend A &A::operator = ( const B &b );
};

I was looking for how to forward a member function declaration, for example:

class B;
B &B::operator = ( const A &a ); //error

But I didn’t find anything. And I do not want classes to be close friends with each other. How to do it?

+3
source share
4 answers

The cause of the compiler error is circular dependency. Each of your operator = () functions requires knowledge of the operator = () function inside another class, so no matter what order you define your classes in, there will always be an error.

Here is one way to figure it out. It is not very elegant, but it will do what you want:

class A;
class B;

A & set_equal(A & a, const B & b);
B & set_equal(B & a, const A & a);

class A
{
    private:
        int x;
    public:
        A & operator=(const B & b) { return set_equal(*this, b); }
        friend B & set_equal(B & b, const A & a);
        friend A & set_equal(A & a, const B & b);
};

class B
{
    private:
        int y;
    public:
        B & operator=(const A & a) { return set_equal(*this, a); }
        friend A & set_equal(A & a, const B & b);
        friend B & set_equal(B & b, const A & a);
};

A & set_equal(A & a, const B & b) { a.x = b.y; return a; }
B & set_equal(B & b, const A & a) { b.y = a.x; return b; }

You can also solve this problem with inheritance.

edit: . , , A, B, , = - .

class A;
class B;

class common
{
    protected:
        int x;
        void copyFrom(const common & c) { x = c.x; }
};

class A : public common
{
    public:
        A & operator=(const common & c) { copyFrom(c); return *this; }
};

class B : public common
{
    public:
        B & operator=(const common & c) { copyFrom(c); return *this; }
};
+1

- -. , , , , ( - ), , , , . : = , - :

class B;

class A {
public:
  A& operator = ( const B &b );
  friend B& do_operator_equals ( B& b, const A& b);
};

class B {
public:
  B &operator = ( const A &a );
  friend A& A::operator = ( const B &b );
  friend B& do_operator_equals ( B& b, const A& a);
};

A& A::operator= (const B& b) {
   // the actual code to copy a B into an A
   return *this;
}

B& B::operator= (const A& a) {
   return do_operator_equals(*this, a);
}

B& do_operator_equals(B& b, const A& a) {
  // the actual code to copy an A into a B
  return b;
}

: A B , oops. .

+2

Why do you want them to be friends first?
It seems impossible as you write it. The solution would probably be to not have them as friends at all.

0
source

You do not send to declare a member function, you send to declare a class.

class B;  // DECLARE class B but don't define its contents

class A { // ... uses B as above
};

class B { // ... now you define the class.
};

See the C ++ 39 FAQ section .

-3
source

Source: https://habr.com/ru/post/1704978/


All Articles