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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:10:18+00:00 2026-06-12T19:10:18+00:00

I have a C++ slicing problem. I’m new to C++, so maybe just too

  • 0

I have a C++ slicing problem. I’m new to C++, so maybe just too dumb to realize this can’t be done…. I have tried a variety of workarounds, and my current best approach looks like below. (I need to do something like this to avoid changing a ton of interfaces in a very large legacy code base. Not claiming this is elegant style in any way!!)

Having troubles making this compile. Is the idea sound? Or is the entire approach doomed to failure? The constructors for references appear to be the issue. I’ve read Stroustrup’s “C++ Programming Lanugage” (or at least what I thought were the relevant sections) but it didn’t help.

class FOO {};

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

class BAR {
public:
    BAR(const FOO foo) : _foo(foo), _realFoo(&_foo) { }

BAR(const FOOSUBCLASS foosc) : _foosc(foosc), _realFoo(&_foosc) {} 
private:
    FOO _foo;
    FOOSUBCLASS _foosc;
    FOO& _realFoo;
};

The compiler doesn’t like my _realFoo(&_foo) line. I’d like the reference to _foo, to just be the reference from the member variable in the class. Is that not possible in C++?

Here’s the specific error from VS2005:

'initializing' : cannot convert from 'FOO *' to 'FOO &'
  • 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-12T19:10:19+00:00Added an answer on June 12, 2026 at 7:10 pm

    _foo is a FOO&.
    &_foo is a FOO*.
    The compiler cannot convert from 'FOO *' to 'FOO &'.
    what you want is ..., _realFoo(_foo)

    Unrelated: What you probably actually want is a std::unique_ptr<FOO> member instead. This will be much smaller, and much less error prone.

    The struct as you have it now, contains a full and complete instance of FOO, and a full and complete instance of FOOSUBCLASS, and a reference. At least put the two FOO thingies in a union, so the size of BAR is only slightly bigger than the biggest FOO derivative. That would probably use the same memory as a unique_ptr<FOO>. Unfortunately, unions are a common source of bugs in C and C++.

    Another problem is if someone comes along and writes

    class FOOSOMETHINGELSE : public FOO {
       int buffer[1024];
    };
    

    Then you’ll have to go and find the BAR class and change it and make it even bigger, and recompile all of your code that uses BAR everywhere. Whereas, if you used a unique_ptr<FOO>, then you won’t have to change BAR or recompile anything. So smaller chance of error.

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

Sidebar

Related Questions

I have a problem with a really good JS Gallery, this one: http://jsfiddle.net/roXon/p44af/ I
I have this problem. I have sliding menu (3 levels) and when I several
I have a real annoying problem and I can't seem to find it. Only
Really struggling to understand this problem, any ideas are welcome I have a carousel
Maybe I'm misunderstanding how inheritance works here, but here's my problem: I have a
I have a sliding drawer implemented. But the problem is this: I have a
Problem can be found here: http://rosalind.info/problems/subs/ The question I have has to do with
Hy guys! I have to make a sliding div (left-right) using jquery. This is
have written this little class, which generates a UUID every time an object of
I don't have my Effective C++ with me and this is bugging me so

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.