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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:56:45+00:00 2026-06-18T16:56:45+00:00

Hey this is a really basic question and but I got confused about it.

  • 0

Hey this is a really basic question and but I got confused about it. Say I created an object

MyObject a.

It comes with a copy constructor, so I know I can do this:

MyObject b(a);
But can I do this?

MyObject& b(a);

And if I do this:

MyObject b = a; what is in b? Apology if this question is too fundamental to be bothered posting.

  • 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-18T16:56:46+00:00Added an answer on June 18, 2026 at 4:56 pm

    Doing MyObject& b(a) has nothing to do with the copy constructor. It just creates b which is a reference to the object a. Nothing is copied. Think of b as an alias for the object a. You can use b and a equivalently from then on to refer to the same object.

    MyObject b = a; will use the copy constructor, just as MyObject b(a); would.


    There are two forms of initialisation: T x = a; is known as copy-initialization; T x(a) and T x{a} are known as direct-initialization.

    When T is a reference type, it doesn’t matter which type of initialisation is used. Both have the same effect.

    When T is a class type, we have two possibilities:

    1. If the initialisation is direct-initialization (MyClass b(a);), or, if it is copy-initialization with a being derived from or the same type as T (MyClass b = a;): an applicable constructor of T is chosen to construct the object.

      As you can see, both of your examples fall in this category of class type initialisers.

    2. If the initialisation is any other form of copy-initialization, any user-defined conversion sequence will be considered followed by a direct-initialization. A user-defined conversion sequence is basically any sequence of standard conversions with a single conversion constructor thrown in there.

      If c were of Foo class type and there was a conversion constructor from Foo to MyClass, then MyClass b = c; would be equivalent to MyClass b(MyClass(c));.

    So basically, if the source and destination types are the same, both forms of initialisation are equivalent. If a conversion is required, they are not. A simple example to show this is:

    #include <iostream>
    
    struct Bar { };
    
    struct Foo
    {
      Foo(const Foo& f) { std::cout << "Copy" << std::endl; }
      Foo(const Bar& b) { std::cout << "Convert" << std::endl; }
    };
    
    int main(int argc, const char* argv[])
    {
      Bar b;
      Foo f1(b);
      std::cout << "----" << std::endl;
      Foo f2 = b;
      return 0;
    }
    

    The output for this program (with copy elision disabled) is:

    Convert
    ----
    Convert
    Copy
    

    Of course, there are lots of other types of initialisations too (list initialisation, character arrays, aggregates, etc.).

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

Sidebar

Related Questions

Hey everybody, Im really confused about this problem. Ill try to describe it: The
This might be a really basic css question but I tried to create my
hey guys having this really simple problem but cant seem to figure out have
Hey this might be a simple question, but i have been stumped on it
Hey this is probably a dumb question but i can't seem to find a
I have another SQL/access 2007 question that seems really basic but I'm not sure
hey. friends i dont know this question is really asked before or not ?
hey guys , i know this is a stupid question but i hanged in
Hey guys, I am really messing up with this. I tried this but it
hey sorry for bothering if this is a really simple issue, but I can't

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.