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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:27:23+00:00 2026-05-30T03:27:23+00:00

Does the overloading of the assignment operator propagate to an initializer list? For example,

  • 0

Does the overloading of the assignment operator propagate to an initializer list?

For example, suppose a class:

class MyClass {
    private:
        std::string m_myString; //std::string overloads operator =
    public:
        MyClass(std::string myString);
}

And a constructor:

MyClass::MyClass(std::string myString)
 : m_myString(myString)
{
}

Will the initializer list work out the assignment operator overload on std::string? And if not, is there a workaround?

Particularly for GCC.

  • 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-30T03:27:24+00:00Added an answer on May 30, 2026 at 3:27 am

    I think what you are missing is the difference between assignment and initialization.

    Lets look at a simple example with a fundamental type:

    int a = 10; // Initialization
    a = 1; // Assignment
    

    The above example is simple and not difficult to understand. However, when you get into user-defined types, it is not as simple because objects are constructed.

    For example, lets look at std::string

    std::string s1("String1"); // Initialization (constructs s1 using constructor)
    std::string s2 = s1; // Initialization (constructs s2 using copy constructor)
    std::string s3(s2); // Initialization (constructs s3 using copy constructor)
    
    s1 = s2; // Assigns s2 to s1 using assignment operator
    

    The key thing here is operator= means different things in different contexts. It all depends on what is on the left hand side.

      std::string s1 = "Hello"; // Lhs has std::string s1, so this is initialization
      s1 = "Bob"; // Lhs has only s1, so this is assignment
    

    And initializer lists do initialization only (hence the name initializer list).

    MyClass::MyClass(std::string myString)
     : m_myString(myString) // Initialization
    {
    }
    

    Just be aware, when you call operator= in the body of the constructor, you are now doing assignment and not initialization.

    MyClass::MyClass(std::string myString)
    {
        // m_myString(myString); <-- Error: trying to call like a function
        m_myString = myString; // Okay, but this is assignment not initialization
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just got a seg fault in overloading the assignment operator for a class
I have a container class that is templatized. I am overloading the assignment operator
Does anyone have a summary of boilerplate declarations for C++ operator overloading? A one
Java does not have concept of operator overloading. Still + operator behaves as addition
If overloading the dereferencing operator ( operator* ), does the member selection operator (
I am currently working on overloading the == operator for my linked list. I
I am working on an assignment that introduces me to operator overloading. I have
Java can't do operator overloading, but + works okay for String and Integer and
This is a naive example I have coded to study C++ operator overloading. When
C++ allows overloading operator new - both global and per-class - usual operator new

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.