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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:06:39+00:00 2026-05-27T00:06:39+00:00

Consider this code class Foo { private: Bar bar; //note: no reference public: Foo(Bar&

  • 0

Consider this code

class Foo {
private:
    Bar bar; //note: no reference

public:
   Foo(Bar& b) : bar(b) { }
};

Will Bar get copy-constructed?

  • 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-27T00:06:39+00:00Added an answer on May 27, 2026 at 12:06 am

    That depends on the signatures of Bar‘s public constructors, either explicitly or implicitly defined.

    To start with, the C++ standard allows for implicit conversion of references as long as the only difference in the underlying type is that the destination type is at least as cv-qualified than the source type, using the partial ordering defined in this table (C++11, §3.9.3/4):

    no cv-qualifier < const
    no cv-qualifier < volatile
    no cv-qualifier < const volatile
    const          < const volatile
    volatile    < const volatile

    So, taking that into account as well as §12.8/2:

    A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments.

    if Bar has a constructor with any of the following signatures:

    Bar(Bar&);
    Bar(Bar const&);
    Bar(Bar volatile&);
    Bar(Bar const volatile&);
    

    then yes, b will be copy-constructed into Foo::bar.


    EDIT: This was incorrect, I was thinking of operator= and the details of qualifying as a move-assignment operator.

    Note that it’s possible to have an eligible constructor that is not a copy constructor:

    Bar(Bar);
    

    This will work (read: compile), but it is not technically a copy constructor.

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

Sidebar

Related Questions

Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
Consider the following example code: class Foo { }; class Bar : public Foo
Consider this class: public class Foo { // Fields private string _bar; // Properties
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following short code snippet. namespace B { public class Foo { public
Consider this: class Foo{ void func1(){ /*func1 code*/ } void func2(){ /*func2 code*/ }
Consider this code: template <typename T> class String { public: ... String(T* initStr) {
Consider this code: class DatabaseCommands { @Privilege(ADMIN) public void delete(Something thingToDelete, User currentUser) {

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.