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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:59:09+00:00 2026-06-06T14:59:09+00:00

What the difference between A a1(5); and A a2 = A(5) ? Both of

  • 0

What the difference between A a1(5); and A a2 = A(5) ? Both of the works, but I really want know the difference between them, because I used method 2 in one of my project and I suffered from a bug which is fixed after I change to method 1. Thanks in advance!

class A {
public:
    int val;
    A() : val(0) {}
    A(int newVal) : val(newVal) {}
};

int main()
{
    A a1(5);  // method 1
    A a2 = A(5); // method 2
}
  • 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-06T14:59:11+00:00Added an answer on June 6, 2026 at 2:59 pm
    A a1(5);  // method 1
    A a2 = A(5); // method 2
    

    The first one is called direct initialization, the second one is called copy-initialization.

    The second one will NOT compile if you make the copy-constructor inaccessible or/and don’t define it as:

    class A {
    public:
        int val;
        A() : val(0) {}
        A(int newVal) : val(newVal) {}
    private:
        A(A const&);  //the second one will not compile
    };
    

    Now the second one will not compile. Note that it will not compile in both cases:

    • If the copy-constructor is defined, but is inaccessible (either it is private or protected).
    • If the copy-constructor is declared, but not defined.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between sizeof(3.0) and sizeof(3.0f) I was expecting both of them
What is the difference between System.Web.Cache and HTTPContext.Curent.Cache? In which cases both are used?
What's the difference between window.location and document.location? Should both of them reference the same
What's the difference between console.log(process.cwd()) and console.log(__dirname); I've seen both used in similar contexts.
Sometimes $('.classname') and $.find('.classname') gives different result. Wondering what is the difference between both.
What is the difference between objectForKey and valueForKey ? I looked both up in
What is the difference between the id and name attributes? They both seem to
What's the difference between std::string and std::basic_string ? And why are both needed?
What is the difference between Semantic-markup and Semantic Web ? Are both same concepts?
What is the difference between JS_NewPropertyIterator() and JS_Enumerate() ? Both seems to achieve the

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.