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 8006781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:37:33+00:00 2026-06-04T17:37:33+00:00

In C++, if a class has a reference data member the default assignment operator

  • 0

In C++, if a class has a reference data member the default assignment operator is not synthesized by compiler. Why?

  • 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-04T17:37:35+00:00Added an answer on June 4, 2026 at 5:37 pm

    In C++, if a class has a reference data member the default assignment operator is not synthesized by compiler. Why?

    What an copy assignment should do is defined in:

    C++03 Standard 12.8/13:

    Each subobject is assigned in the manner appropriate to its type:

    • if the subobject is of class type, the copy assignment operator for the class is used (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);

    • if the subobject is an array, each element is assigned, in the manner appropriate to the element type;

    • if the subobject is of scalar type, the built-in assignment operator is used.

    In short It implies that each of the member should assigned in an appropriate manner
    which raises the question,
    What should be the behavior for assignment of a reference member in class?
    Consider the following about references:

    1. References are inherently non assignable, they keep referring the same referrant to which they were initialized[Ref 1].
    2. By virtue of #1 assigning to an reference doesn’t reassign the reference, it changes the value of the referrant which is non-intuitive behavior.

    There is no default correct behavior to be enforced here but a rather situational one.So the C++ Standard mandates that designer of the class is in best position to determine this behavior and hence the decision that default assignment operator should not be synthesized by compiler if a class has a reference data member.

    This decision is specified in:
    C++03 Standard 12.8/12:

    An implicitly-declared copy assignment operator is implicitly defined when an object of its class type is assigned a value of its class type or a value of a class type derived from its class type. A program is ill formed if the class for which a copy assignment operator is implicitly defined has:
    …….
    — a nonstatic data member of reference type, or
    …….


    [Ref 1]
    C++03 Standard 8.5.3/2:

    A reference cannot be changed to refer to another object after initialization. Note that initialization of a reference is treated very differently from assignment to it. Argument passing (5.2.2) and function value return (6.6.3) are initializations.

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

Sidebar

Related Questions

The below code is a web service response format.This class has two reference variables
the basic idea is that you have some class that has a reference type
I have a class which has a private attribute which is a reference to
so I've made a class in c++ which has 2 reference type members: class
The new Android 2.1 SDK (version 7) has a new class called SignalStrength: http://developer.android.com/reference/android/telephony/SignalStrength.html
C++11, §9/7: A standard-layout class is a class that: has no non-static data members
I'm working with a class for which the new operator has been made private,
I have an Activity class which has an attribute that references an AsyncTask instance
A class has a method to update its instance properties from a given Object.
My class has a pointer vector: ptr_vector<Class> vec; And in some setup method adds

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.