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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:10:37+00:00 2026-06-04T15:10:37+00:00

I have a problem with the following piece of code (it is a very

  • 0

I have a problem with the following piece of code (it is a very simplified example that reproduce the error in my program) :

#include <iostream>

using namespace std;

template<class T> class CBase
{
    public:
        template <class T2> CBase(const T2 &x) : _var(x) {;}
        template <class T2> CBase (const CBase<T2> &x) {_var = x.var();}
        ~CBase() {;}
        T var() const {return _var;}
    protected:
        T _var;
};

template<class T> class CDerived : public CBase<T>
{
    public:
        template <class T2> CDerived(const T2 &x) : CBase<T>(x) {;}
        template <class T2> CDerived (const CBase<T2> &x) : CBase<T>(x) {;}
        ~CDerived() {;}
};

int main()
{
    CBase<double> bd(3);
    CBase<int> bi(bd); // <- No problem
    CDerived<double> dd1(3);
    CDerived<double> dd2(dd1);
    CDerived<int> di(dd1); // <- The problem is here
    return 0;
}

And the error is the following :

error: cannot convert 'const CDerived<double>' to 'int' in initialization

How to solve that ? (with a preference for modifications in the base class and not in the derived class, and if possible no use of virtuality)

Thank you very much

EDIT :
If I replace the concerned line with : CDerived<int> di(CBase<int>(CBase<double>(dd1))); it works but it is not very practical…

EDIT : Seems to be solved by that :

template <class T2> CDerived(const CDerived<T2> &x) : CBase<T>(static_cast<const CBase<T2>&>(x)) {;}
  • 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-04T15:10:38+00:00Added an answer on June 4, 2026 at 3:10 pm
    CDerived<int> di(dd1); // <- The problem is here
    

    This invokes the first constructor of CDerived, and so T2 is inferred as CDerived<double> which is the type of dd1. Then, dd1 becomes x in the constructor; x which is CDerived<double>, gets passed to the base class constructor which accepts int (which is the value of the type argument T to CDerived class template). Hence the error, as CDerived<double> cannot be converted into int. Note that T of CBase is int.

    See it as:

    CDerived<int> di(dd1); // <- The problem is here
              ^       ^
              |       |
              |       this helps compiler to deduce T2 as double
              |
              this is T of the CDerived as well as of CBase
    

    If you want to make your code work, then do this:

    1. First derive publicly instead of privately.
    2. Add another constructor taking CDerived<T2> as parameter.

    So you need to so this:

    template<class T> class CDerived : public CBase<T>  //derived publicly
    {
        public:
            template <class T2> CDerived(const T2 &x) : CBase<T>(x) {;}
    
            //add this constructor
            template <class T2> CDerived(const CDerived<T2> &x) : CBase<T>(x.var()) {;}
    
            template <class T2> CDerived (const CBase<T2> &x) : CBase<T>(x) {;}
            ~CDerived() {;}
    };
    

    It should work now : online demo

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

Sidebar

Related Questions

I have the following piece of code in C++: #include <iostream> #include <fstream> #include
Well my problem is the following. I have a piece of code that runs
I have the following piece of code - jsfiddle example The problem i'm having
I have the following piece of code and the problem is that the callback
I have the following piece of code (that may be invoked multiple times or
I have the following piece of code. See the two comments for the problem.
I have the following piece of code: if (SqlConnection.State == ConnectionState.Open) { using (SqlDataAdapter
I have the following, very easy to reproduce problem: I'm creating a xaml application
I have the following piece of code: while current is not problem.getStartState(): print Current:
I have the following piece of code, as an example dec_proxy attempts to reverse

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.