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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:32:15+00:00 2026-05-22T15:32:15+00:00

I am just experimenting with the references using this code: class A { };

  • 0

I am just experimenting with the references using this code:

class A
{
};

class B
{
public:
    B(A& a): m_a(a){}

    A& m_a;
};

int main()
{
    A a;
    B b(a);
    B b1 = b;
}

I was expecting both B b1 = b; to produce a error. Instead when I compile with VS2008 I just get a warning

warning C4512: ‘B’ : assignment
operator could not be generated

I understand why I am getting this warning. But shouldn’t the compiler generating an error for the B b1 = b; statement too? It is like it generated copy constructor but didn’t generate assignment operator. Aren’t these two inherently linked to one another ? does it makes sense to generate the default implementation for only one of them when the other could not be generated?

  • 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-05-22T15:32:16+00:00Added an answer on May 22, 2026 at 3:32 pm
    warning C4512: 'B' : assignment operator could not be generated
    

    Question 1: Why this warning?
    References can only be initialized once when they are created. You cannot reassign a reference to another same type variable after creation because Reference is just an alias of the type variable for which it was created and will continue to remain so. Attempting to reassign it generates an error.
    Usually, a compiler generates an implicit bit wise assignment operator every class by default but in this case Since the class B has an reference as an member m_a, If the compiler were to generate an implicit assignment operator it would break the fundamental rule that references cannot be reassigned. So the compiler generates this warning to inform you that it could not generate the implicit assignment operator.

    Question 2: But shouldn’t the compiler generating an error for the B b1 = b; statement too?
    The generated warning and this particular operation have no relation at all.
    B b1 = b; invokes the implicit(as rightly pointed out by @AndreyT) copy constructor B::B(const B&). Implicit copy constructor is one of the member functions which a class generates by default. So there is no warning or error for it.

    Question 3: It is like it generated copy constructor but didn’t generate assignment operator. Aren’t these two inherently linked to one another ?
    No they are not related at all. Yes compiler generated a copy constructor but it could not generate a assignment operator for the reason specified in answer to Question 1 above. This is because the member reference m_a could be initialized in the body of the constructor itself. it’s just the initial assignment at time of creation not assignment as in case of =.

    Question 4: Does it makes sense to generate the default implementation for only one of them when the other could not be generated?
    Answer to the 3 Question seems to answer this.

    Just to reiterate the operations being performed in your code example:

    B b(a); invokes the conversion copy constructor B::B(A&)
    B b1 = b; invokes the default copy constructor B::B(const B&)

    Consider the additional scenarios.
    If you had B b1 = a; it would call B::B(A&) and hence no error again.

    But compiler would mark a error if B::B(A&) was declared explicit and would not be allowed for any implicit conversions by acting as a conversion function.

    Check the same here.

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

Sidebar

Related Questions

I've just started experimenting with SDL in C++, and I thought checking for memory
I'm just starting to dive into some basic Android development and have been experimenting
I have been experimenting with geography datatype lately and just love it. But I
I am experimenting with Java Web Start. I have just written a basic JApplet
I'm experimenting with a custom LayoutManager and I don't understand the subtleties of using
Just what the title says, I need to change the password for an existing
Just looking for the first step basic solution here that keeps the honest people
just a quick question: I am a CS undergrad and have only had experience
Just bought a 2.4GHz Intel Core 2 Duo iMac with 2GB of memory and
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.

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.