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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:30:33+00:00 2026-06-12T16:30:33+00:00

I have a huge C++ code base I’m working with. I want to introduce

  • 0

I have a huge C++ code base I’m working with.

I want to introduce a new subclass, and use it in a variety of different contexts, however my Java trained mind is having trouble with this in C++.

I wish to minimize code changes. So, my main class of interest (BAR below) has a member variable of class FOO. I wish to make a subclass of FOO called FOOSUBCLASS. In java this is trivial. Objects are stored by reference by default. This code does not do that (as seen below) can I massage this code, without changing interfaces (and without introducing references) and still make my application work?

class FOO {
};

class FOOSUBCLASS : FOO {
public:
    FOOSUBCLASS(const int id) {_id = id;}
private:
    int _id;
};

class BAR {
public:
    BAR(const FOO foo) { _foo = foo;}
private:
    FOO _foo;
};

Below, is my main:

FOOSUBCLASS fsc(1);
BAR b(fsc);

But this doesn’t compile in VS2005. It says:

‘type cast’ : conversion from ‘FOOSUBCLASS *’ to ‘const FOO &’ exists, but is inaccessible

Get same compiler error if I add the obvious other constructor to BAR

BAR(const FOOSUBCLASS foo) { _foo = foo;}

My understanding of C++ is that it will copy the data from FOOSUBCLASS into an object of class FOO. (using the assignment operator, or a class overridden version of it) But in my case, I have additional member variables in FOOSUBCLASS (and some overridden member functions) so I just don’t want it to do that. I want my member variable _foo to truly be of type FOOSUBCLASS in some contexts, and FOO in others. Is this even possible?

Thanks for your thoughts.

  • 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-12T16:30:35+00:00Added an answer on June 12, 2026 at 4:30 pm

    You can’t do it the way you’ve written it now, sorry. This is called “slicing”, and more info about it can be found here: What is object slicing?

    If you want your _foo member to be polymorphic, you’d need to use a pointer or a reference. Be aware of lifetime management when you do things like this of course.

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

Sidebar

Related Questions

I am working on a C++ project which has a huge code base and
We have a huge code base, where methods with too many local variables alone
I have inherited an existing code base where the features are as follows: huge
I have huge code base and I am having difficulty managing it. There are
We have a huge code base and we suspect that there are quite a
I am operating a huge code base and want to monitor a value of
We have a huge (old legacy java) code-base, where many files (around 5k) have
I have a huge code-gen'd EDMX and the designer opened in about 2 seconds
I have a huge string (22000+ characters) of encoded text. The code is consisted
I have a huge checkbox list with different levels and I would like to

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.