Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:16:40+00:00 2026-05-11T10:16:40+00:00

It’s time for my first question now. How do you cross assignments operators between

  • 0

It’s time for my first question now. How do you cross assignments 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 searched for how to forward declare a member function like:

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

But I didn’t find anything. And I don’t want to make the classes all-out friends with each other. How do I do this?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:16:40+00:00Added an answer on May 11, 2026 at 10:16 am

    The reason for the compiler error is a circular dependency. Each of your operator=() functions require knowledge of the operator=() function inside the other class, so no matter which order you define your classes in, there will always be an error.

    Here is one way to sort it out. It isn’t 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 may also be able to solve this problem with inheritance.

    edit: here is an example using inheritance. This will work if the copying procedure only needs access to some common data shared by both A and B, which would seem likely if the = operator is to have any meaning at all.

    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; } }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 295k
  • Answers 295k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd choose AJAX Control Tool Kit + PageMethods (avoid UpdatePanel,… May 13, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer Character 147 is U+0093 SET TRANSMIT STATE. Like all the… May 13, 2026 at 6:56 pm
  • Editorial Team
    Editorial Team added an answer No, there is not. Your idea is generally how that… May 13, 2026 at 6:56 pm

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I want use html5's new tag to play a wav file (currently only supported
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.