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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:38:16+00:00 2026-06-13T09:38:16+00:00

I am not sure how to ask this, but hopefully someone will understand. Lets

  • 0

I am not sure how to ask this, but hopefully someone will understand. Lets say I have 3 different classes. Class A, Class B and Class C. Class C should take either Class A or Class B as a parameter in the constructor and store it in a private variable.

This is easy with overloaded constructors. My question is how can Class C automagically use the correct class depending on what constructor was used? (Note these 2 classes are similar, but come from different libraries and thus no shared base class). Is this possible with templates? I do not have a lot of experience with templates.

  • 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-13T09:38:17+00:00Added an answer on June 13, 2026 at 9:38 am

    You can do it quite easy with templates:

    class A;
    class B;
    
    template<class AorB>
    class C
    {
    public:
        C(AorB aorb)
            : aorb_(aorb)
            { }
    
    private:
        AorB aorb_;
    };
    

    What this does is that inside the class C the identifier AorB can be used as any other class, in fact it doesn’t even have to be an instance of A or B but can be any class.

    Can be used like this:

    A myA;
    B myB;
    
    C<A> myCWithA(myA);
    C<B> myCWithB(myB);
    

    There is however one thing you have to remember when creating classes using templates: The specification and implementation can no longer be split into separate header and source files. All of the code have to be available in the header file.

    The syntax of the member functions are also a little different.

    Example:

    template<class T>
    class C
    {
    public:
        ...
    
        void someFunction();
    };
    
    template<class T>
    C<T>::someFunction()
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure how to ask this question, but here goes. Say I have: var
Not sure how to ask this, but i'll give it a try: I have
I'm not sure I know how to ask this. say I have a function
I'm not really sure how to ask this question. Suppose I have a class
Sorry but I am not sure how to ask this question but I am
Im not even sure how to ask this question, but i'll give it a
I'm not really sure how to ask this question, but I'll do my best.
Not sure if this is the right spot to ask but I'm having a
I am not sure even how to ask this question. I have a table
Not sure if I should or even can ask this here, but this is

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.