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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:34:40+00:00 2026-05-30T00:34:40+00:00

I have the following problem at hand. There is a class, Foo and another

  • 0

I have the following problem at hand. There is a class, Foo and another one called Bar which is a member of class Foo.

class Bar{
private:
    some stuff;
public:
    Bar();
    some other_stuff;
};
class Foo{
private:
    Bar bar;
public:
    Foo(); // Initialize the bar object here in the constructor
    void doSomethingWithBar();
};

Now both classes are huge! For some application, I need to make changes to both Foo and Bar and so there are Foo_Derived and Bar_Derived classes.

class Bar_Derived: public Bar{
private:
    some NEW_stuff;
public:
    Bar_Derived();
    some New_other_stuff;
};
class Foo_Derived: public Foo{
private:
    Bar_Derived bar;
public:
    Foo_Derived(); 
    void doSomethingWithBar();
};

As far as I understand inheritance, once Foo_Derived() is called, the base class constructor for Foo is also called which initializes the bar object in the base class (of type Bar. What should I do so that the bar object in the derived class is used instead? (of type Bar_Derived). I only need the base class (Foo) to initialize the base part of the bar object and do the rest in the Foo_Derived class myself. Is this even possible?

Sorry if the question is not clear. I’m just really confused myself!

  • 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-30T00:34:42+00:00Added an answer on May 30, 2026 at 12:34 am

    The base class initializer only does initialize the base part of the object, since that’s all it knows about. A class initializer should only initialize those things that always need to be initialized for all members of that class.

    In the constructor for Foo_Derived, you can use an initializer list to specify which Bar_Derived constructor to invoke. The Bar_Derived constructor can use any Bar constructor it wishes. If you don’t have a Bar_Derived constructor that constructs Foo_Derived::bar the way you want it, you should write one or rethink how you have your hierarchy set up.

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

Sidebar

Related Questions

I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem. If I query values with a keyfigure which is
I'm having some problems with the following code: private class ClientPluginLoader : MarshalByRefObject {
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I open the dialog, open the SIP keyboard to
I have the following problem: We need to find the next august. I other

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.