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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:54:29+00:00 2026-06-12T22:54:29+00:00

Consider this example code: #include <iostream> class base { public: base() { std::cout <<

  • 0

Consider this example code:

#include <iostream>

class base {
    public:
    base() {
        std::cout << "base constructed" << std::endl;
    }
    base(const base & source) {
        std::cout << "base copy-constructed" << std::endl;
    }
};

class derived : public base {
    public:
    derived() {
        std::cout << "derived constructed" << std::endl;
    }
    derived(const derived &) = delete;
    derived(const base & source) : base(source) {
        std::cout << "derived copy-constructed from base" << std::endl;
    }
};

int main() {
    derived a;
    base b(a);
    derived c(a);

    return 0;
}

Why is it that the call to base::base(const base &) is okay, but the call to derived::derived(const base &) is not? Both expect a base reference, and both are given a derived reference. It’s my understanding that derived ‘is a’ base.

Why does the compiler insist on using derived::derived(const derived &) when it has no problem using base::base(const base &) when supplied with a reference to an object of type derived?

  • 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-12T22:54:30+00:00Added an answer on June 12, 2026 at 10:54 pm

    Apparently ‘deleting’ one of the default things doesn’t have the effect of actually deleting it exactly. Some grisly ghoulish vestige of the once proud default copy constructor is left lying around to pop up and tell you in a sepulchral voice “I’m deeeeaaaaaad!“.

    This is not an altogether surprising fact to me, though I was unaware of it in specific before you asked this question. I could not quote you the relevant section of the standard (and I’m certain that the relevant section makes no mention of ghouls, though it should). And I’m also fairly certain there is some reason why this is the case that turns out to be perfectly sensible once you follow some incredibly convoluted story about some horrible case that would work in a terrible way if it weren’t.

    And, unfortunately for you, if there’s something lying around that’s a better match than a conversion to a base class, it’s what will be used. For example, in this code:

    #include <iostream>
    
    class A {
    };
    
    class B {
     public:
       void foo(const A &) { ::std::cerr << "B::foo(const A &) called!\n"; }
       void foo(const B &) { ::std::cerr << "B::foo(const B &) called!\n"; }
    };
    
    int main()
    {
        B b;
        A &ar = b;
        b.foo(b);
        b.foo(ar);
    };
    

    will result in this output:

    B::foo(const B &) called!
    B::foo(const A &) called!
    

    Which is exactly as you’d expect. The compiler does not treat the situation as ambiguous. And if you made void foo(const B &) into a private or protected member, then the compiler would still match it in preference to the other, and tell you you tried to access something that had an access specifier that said you couldn’t.

    Think of setting something to be ‘deleted’ as simply declaring it with a special access specifier that’s even more restricted than private.

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

Sidebar

Related Questions

Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
Consider this code: #include <vector> void Example() { std::vector<TCHAR*> list; TCHAR* pLine = new
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider following example code: #include <iostream> #include <inttypes.h> using namespace std; int f(uint32_t i)
Consider this example code: public class A<T> { public static T TheT { get;
This is a follow-up question to this one . Consider this example: #include <iostream>
Consider this simple example - public class Person { private String name; private Date

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.