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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:43:32+00:00 2026-05-28T02:43:32+00:00

Am I doing this right? This is a highly simplified version of my code:

  • 0

Am I doing this right? This is a highly simplified version of my code:

class Logger {
   public:
      Logger(std::ostream) { /*...*/}
};

class Driver {
public:
   Driver() : m_logger(std::cout) {}
   Driver(Logger& logger) : m_logger(logger) {}
private
   Logger m_logger;
};

So my class Driver has a member of type Logger. When I call the argument-less constructor Driver(), the instance of Driver creates its own instance of Logger using std::cout.

When calling Driver(Logger) the instance shall use an already existing instance of Logger passed as a reference.

The above code compiles using g++. Although I understand what happens when calling Driver(), I don’t get what happens when calling Driver(Logger). Logger has no constructor which accepts a reference on Logger as argument (“copy constructor”). So what is executed when calling Driver(Logger)?

  • 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-28T02:43:33+00:00Added an answer on May 28, 2026 at 2:43 am

    A trivial copy constructor for Logger is synthesised for you, unless you declare one yourself.

    This is much the same as how a trivial default constructor is synthesised for you (if you don’t declare a default user-defined constructor).


    [C++11: 12.8/7]: If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor
    is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor. Thus, for the class definition

    struct X {
       X(const X&, int);
    };
    

    a copy constructor is implicitly-declared. If the user-declared constructor is later defined as

    X::X(const X& x, int i =0) { /* ... */ }
    

    then any use of X’s copy constructor is ill-formed because of the ambiguity; no diagnostic is required.

    [C++11: 12.8/8]: The implicitly-declared copy constructor for a class X will have the form

    X::X(const X&)
    

    if

    • each direct or virtual base class B of X has a copy constructor whose first parameter is of type const B& or const volatile B&, and
    • for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a copy constructor whose first parameter is of type const M& or const volatile M&.

    Otherwise, the implicitly-declared copy constructor will have the form

    X::X(X&)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently doing something like this in some code I'm working on right now:
I'm doing this right now in my code to see if a file name
Am I doing this right? I went to look at some old PHP code
I think I’m doing this right? I have this code which starts looking for
Im not sure if I'm doing this right. I have an action that I
Right now I'm doing this: SELECT * FROM messages WHERE location_id = 7 AND
Right now I'm doing something like this: RewriteRule ^/?logout(/)?$ logout.php RewriteRule ^/?config(/)?$ config.php I
What is the most efficient way to do this? Right now I am doing
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
I'm just curious if I'm doing this right. I'm coding a private section of

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.