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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:56:40+00:00 2026-06-09T17:56:40+00:00

I have a C++03 application with a class that holds a single instance of

  • 0

I have a C++03 application with a class that holds a single instance of a type where I am trying to convert between a holder of a derived type to a holder of a base type. For example:

class B { public: virtual ~B() { }; };

class A : public B { };

template< typename T >
class Container
{
public:
    explicit Container( T* obj ) : obj_( obj ) { };
    Container( const Container< T >& ref ) : obj_( ref.obj_ ) { };

    template< typename U > operator Container< U >() { 
        return Container< U >( obj_ );
    };

private:
    T* obj_;
};

This works fine:

int main()
{
    Container< A > a_ref( new A() );
    Container< B > b_ref = a_ref;
    return 0;
}

This gives me the error invalid initialization of reference of type ‘Container<B>&’ from expression of type ‘Container<A>’:

void Foo( Container< B >& cb ) { }

int main()
{
    Container< A > a_ref( new A() );
    Foo( a_ref );
    return 0;
}

This gives me the error error: invalid initialization of non-const reference of type ‘Container<B>&’ from a temporary of type ‘Container<B>’L

int main()
{
    Container< A > a_ref( new A() );
    Foo( static_cast< Container< B > >( a_ref ) );
    return 0;
}

How can I pass a Container< A > type to a function that expects a Container< B > type? Do I need to make a copy of the object first?

  • 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-09T17:56:42+00:00Added an answer on June 9, 2026 at 5:56 pm

    Container<A> and Container<B>are totally different class.

    You can have something like container_cast<T>(U) that converts a Container<A> to Container<B>

    container_cast will construct another Container<T> by taking the obj_ Container<U> and casting that obj_ to T*

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

Sidebar

Related Questions

We have a class that holds configuration information for the application. It used to
I have a class in a web application I am working on that holds
In a C# Winforms (3.5) application I have added a class that contains many
I have a class that extends Application. The class is fairly extensive. When the
The Application class in System.Windows.Forms have a some properties that can be quite useful.
I have a logger class that is used all across my application, both in
I have an Android app that has an Application class and I have another
I have written a class that will handle internal logging in my application. Now
I have an android app that plays audio from the application class. I have
I'm working on an application. I have a servlet (writeDataBase.class) that writes some information

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.