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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:23:28+00:00 2026-06-06T09:23:28+00:00

I am upgrading an old application which was written for a specific hardware interface.

  • 0

I am upgrading an old application which was written for a specific hardware interface. I now need to add support for a modern hardware to the existing application.

To do this, I would like to create a class for each hardware type, and assign a variable to one type or the other whenever the user selects which hardware is in their system.

For example:

Class HardwareType1 and Class HardwareType2 both exist having the same member functions.

object HW;  
if (userHwType = 1)  
    // initialize HW as a HardwareType1 class
}  
else{  
    // initialize HW as a HardwareType2 class  
}

Now I can use HW.doSomething() throughout my code without a conditional for hardware type every time I interact with the hardware.

I’m sure this is pretty basic but to be honest I don’t even know what this is called or what terms to search on for this one.

Thanks!

  • 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-06T09:23:29+00:00Added an answer on June 6, 2026 at 9:23 am

    Create an an abstract base class, and derive two concrete classes from it: one implementing type1 and the other implementing type2:

    class Hardware
    {
    public:
        virtual ~Hardware() {};
        virtual void doSomething() = 0;
    };
    
    class Hardware1: public Hardware
    {
    public:
        void doSomething() { // hardware type1 stuff. }
    };
    
    
    class Hardware2: public Hardware
    {
    public:
        void doSomething() { // hardware type2 stuff. }
    };
    

    Then create the necessary instance:

    std::unique_ptr<Hardware> hardware(1 == userHwType ? new Hardware1() : 
                                                         new Hardware2());
    
    hardware->doSomething();
    

    If you compiler does not support C++11 then std::unique_ptr will not be available to you. An alternative smart pointer would boost::scoped_ptr (or boost::shared_ptr).

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

Sidebar

Related Questions

Background: I am upgrading an old Rails 2 application to Rails 3. This application
I'm upgrading an old VB6 application to VB.NET that uses DAO to connect to
I am just upgrading some old code written in Delphi 6 to Delphi XE2.
I'm upgrading an application from an old ejb2 setup to use ejb3's using the
My client has an old Access application written in Access 2003. It's fairly large
I am upgrading an old Rails 1 application to Rails 2, and I'm running
I'm upgrading a very old (10+ years) application to the latest Delphi XE. There
I'm new to javascript and xpages here. I'm upgrading an old notes application to
I'm upgrading an old internal website from .net 2 to 4 using this procedure
We are upgrading/converting several old Access databases to MS-SQL. Many of these databases have

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.