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

  • SEARCH
  • Home
  • 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 8319505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:20:50+00:00 2026-06-08T22:20:50+00:00

Please consider the following snippet: class Pin { public: Pin() { } }; class

  • 0

Please consider the following snippet:

class Pin
{
public:
    Pin()
    {
    }
};

class Connection
{
    Pin& _from;
    Pin& _to;

public:
    Connection(Pin& from, Pin& to)
        : _from(from), _to(to)
    {
    }
};

class Device
{
    Pin _a, _b;
    Connection _int_conn;

public:
    Device()
        : _int_conn(_a, _b)
    {
    }
};

Now I’d like to define operator= so that Device can be copied one to another:

int main()
{
    Device a, b;

    a = b;
}

Of course, the default operator= doesn’t suffice because the Connection class holds two references. For this particular operation, it is however desirable that the Device copies all other properties and leaves the references intact (since they are expected to always point to the ‘parent’ pins).

On the other hand, the Connection class can be also used by user, and then the two pins can belong to any other class. In this case, although copying other properties may be beneficial, it seems a bit incorrect to use operator= for that. In other words:

int main()
{
    Pin a, b, c, d;
    Connection y(a, b), z(c, d);

    y = z;
}

to copy non-reference members seems wrong to me.

Moreover, if I ever decided to change references to pointers, I’d probably want operator= to copy them as well.

Am I right here? What is the best course of action? I think about defining an additional method like:

void Connection::copy_properties(Connection& rhs)

or just copying them in Device::operator=.

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-06-08T22:20:51+00:00Added an answer on June 8, 2026 at 10:20 pm

    [the references] are expected to always point to the ‘parent’ pins.

    Is this an invariant of Connection or of Device? If the latter, consider this version of Connection:

    class Connection
    {
        Pin* _from;
        Pin* _to;
    
    public:
        Connection(Pin& from, Pin& to)
            : _from(&from), _to(&to)
        {}
    };
    

    That is to say, this version has somewhat more obvious copy semantics. Then Device::operator= now has to make sure to not break the Device invariant (e.g. assign whatever needs assigning while leaving _int_conn unchanged). And in fact making _int_conn a const member can enforce that; but that’s only as useful as the const interface of Connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following snippet from an implementation of the Interpreter pattern: public override
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Please consider the following simple use case: public class Foo { public virtual int
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc
Please consider the following: class base{ base(); ~base(); }: class derived : public base{
Please consider the following: public class MyObject { public bool B; public string Txt;
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Please consider the following: class CMyClass { public: CMyClass() { printf( "Constructor\n" ); }
Please consider following sample @Entity public class Abc { @Id private Long id; @Unindexed

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.