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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:35:44+00:00 2026-05-17T02:35:44+00:00

There is a C code, which we are trying to convert into C++ code.

  • 0

There is a C code, which we are trying to convert into C++ code. There were two inheritance hierarchies, which were managed by C through switch..case. That has been converted to use virtual functions in C++. Now at one point these two hierarchies have to get related. For example class A relates to A1, class B relates to B1 …. where A,B,C falls in hierarchy1 and A1, B1, C1 falls in hierarchy2.
In C code, this is achieved by storing the tag field(or type field used in switch..case) of hierarchy 2 in A, B, C. Can someone guide me on how to achieve this C++ cleanly?

In C

enum NodeTag1
{
  A_Node
  , B_Node
  ....  
};

enum NodeTag2
{
   A1_Node
   , B1_Node
   ....
};

struct Node
{
   NodeTag1 tag1;
};

struct A
{
     NodeTag1 tag1;
     NodeTag2 tag2;
     ..other members...
};

struct B, C all look the same.

Now assume there are 10 functions(f1, f2..) which use NodeTag1 to do a switch.. case
In C++

class Node {  //10 virtual functions (f1,f2 ..)};
class A : public Node { // 10 virtual function( f1, f2.. ) };
class B : public Node { // 10 virtual function( f1, f2.. ) };

Now there is a function like this, that gets called from instances of hierarchy 1.

int decide( NodeTag2 tag2 )
{
   switch(tag2)
   {
      ...
   }
}

How can i achieve this in C++?

Thanks,
Gokul.

  • 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-05-17T02:35:45+00:00Added an answer on May 17, 2026 at 2:35 am

    If you dont want to create objects at run time. You can use the following method. with only one time object creation.
    As you dont need to access members of the classes, single object will do your work;

    class A
    {
    
    public:
    
     virtual f()
     {
      printf("A");
     }
    
     static A* behaveLikeA();
    
     static A* behaveLikeB();
    };
    
    class B : public A
    {
    
    public:
    
     virtual f()
     {
      printf("B");
     }
    };
    
    A* A::behaveLikeA()
    {
     static A ag;
     return &ag;
    }
    
    A* A::behaveLikeB()
    {
     static B bg;
     return &bg;
    };
    
    
    int main(int argc, char* argv[])
    {
    
     A* b = A::behaveLikeB();
     b->f();
    
     b = A::behaveLikeA();
     b->f();
     return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some Haskell code in which there are multiple data types,
i'm using DbSimple, but there is some code which i could write into another
Is there a tool available which will convert source code in Perl to source
I am trying to convert a decimal colour code from a Flash Application into
I was trying to convert some C# code to Vb.net but there was a
I am trying to convert a BinaryTree which is made of Nodes into a
There is a sample code which works in a multithreading environment: void CSampleClass::Stop(void) {
I have code which needs to do something like this There is a list
There Currently is a local debate as to which code is more readability We
In our code base there is a function which should return either an object

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.