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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:23:08+00:00 2026-05-29T16:23:08+00:00

I have these classes: first: class C { public: C(const C& c):_s(c._s){} c():_s(){} string

  • 0

I have these classes:

first:

 class C 
 {
     public:
     C(const C& c):_s(c._s){}
     c():_s(""){}

     string _s;
 }

second:

class C2: public C
{
    public:
    C2(const C2 & c2):_i(c2.i){}
    C2():_i(0){}

    int _i;
}

main:

int main()
{
    C2 c2;
    C2._s="hello";
    c2._i=42;
    C2 c3(c2);
    cout<<c3._s<<" "<<c3._i<<endl;
}

and the output is 42. My question is, why is the output 42? A base constructor is always called before the derived constructors, so this line:

C2 c3(c2);

should call C‘s copy constructor and should copy “hello”, meaning the output should be hello. What am I missing out here?

  • 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-29T16:23:10+00:00Added an answer on May 29, 2026 at 4:23 pm

    The reason is

    C2(const C2 & c2):_i(c2.i){}
    

    doesn’t initialize _s to the values stored in c2 because the default (parameterless) constructor of base class is called unless specified otherwise and so _s is also initialized with its default constructor, not copy constructor.

    Specifically in your case C2::(const C2&) invokes C::C() and that invokes string::string().

    You have to explicitly call the right base constructor:

    C2(const C2 & c2) :
      C( c2 ),
      _i(c2.i)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
I have these classes. class Author{ Person person } class Person{ String lastName String
I have these classes in C# (.NET Framework 3.5) described below: public class Base
Concerning data binding I have these classes: public class Foo : List<Bar> { public
I have these two classes public class Person { } public class Company {
Let's say I have these classes: public class BaseMapClass<T1, T2> { protected T1 Thing1;
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
I have these C++ classes: class sales{ private: float dollar[3]; public: void getData(float f1,float
I need some help in my application. I have these classes: Ajuda.class : public
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived

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.