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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:47:59+00:00 2026-05-21T10:47:59+00:00

I have a design-related question in C++. I’m building a tree for a HW

  • 0

I have a design-related question in C++.

I’m building a tree for a HW assignment.
The tree implementation is pretty straightforward. Basically it’s a templated class

template <typename TYPE, typename KEY>
class Tree
{
public:
void Insert(TYPE& node);
void Remove(TYPE& node);
TYPE& Find (KEY key);
    //etc.
};

So far is just background.
Now, later when I use Tree, I have an Employee class that I want to have 2 trees of, but once use ID as the key and in the other use Salary as the key, but I don’t want to duplicate the data.
Obviously I need to implement 2 different comparison functions. My first attempt was to do something like this:


class Employee
{
public:
int ID;
float Salary;
};

enum COMPARE_RESULT { LESS_THAN = -1, EVEN = 0, GREATER_THAN = 1 } template class IComparable { public: virtual COMPARE_RESULT Compare (const T& other) const = 0; };

class EmployeeCompareByID : public Employee, public IComparable { public: Compare (const T& other) const { //Compare IDs and return result } };

class EmployeeCompareBySalary : public Employee, public IComparable { public: Compare (const T& other) const { //Compare Salaries and return result } };

typedef union { Employee employeeData; EmployeeCompareByID employeeCompareByID; EmployeeCompareBySalary employeeCompareBySalary; }EmployeeUnion;

//finally the main would do something like this: int main() { //first tree key is by ID Tree empTreeByID; //second tree key is by salary Tree empTreeBySalary;

EmployeeUnion emp;
emp.employeeData.ID = 1;
emp.employeeData.Salary = 1000.11;
empTreeByID.Insert(emp.employeeCompareByID);
empTreeBySalary.Insert(emp.employeeCompareBySlary);
//the same emp is referenced in both trees. Each by it's relevant member in union

}

but this approach fails because my Employee class has a constructor, copy constructor and operator overloading so defining a union on Employee is impossible.
Furthermore, this approach requires the Tree implementation to do a static_cast on the TYPE
template-argument to IComparable which seems fine to me.

Another possible solution is to pass a function-pointer to the Tree constructor, with a pointer to the appropriate Compare function for that instance, but this seems to me an inelegant solution, and possibly quite messy and hard to debug later.

Googling on the limitations of using unions with classes, I found a post suggesting to solve a problem similar to mine using Conversion operator overloading but didn't expand much.

I'm sure this is a fairly common problem that has a common design solution, and I just don't know it.

Any thoughts or comments are appreciated

  • 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-21T10:47:59+00:00Added an answer on May 21, 2026 at 10:47 am

    The actual problem in your code is that you try to place the comparision and the employee data structure to the same object. They should be different objects like this:

    template<class T> class ICompare {
      virtual bool Compare(const T &a, const T &b) const=0;
    };
    

    and then obviously you need to modify the Tree constructor:

    template<class T> class Tree {
       Tree(const ICompare<T> &c) : c(c) { }
       ...
        const ICompare<T> &c;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question related to a template class implementation design. A template AT
I have a design problem: Question class: have a question and answer (String) TextQuestion
I am working in asp.net MVC2 application and I have question related to design
I have a question related to database design. The database that I'm working with
I have a question related to C++ design. What are some criteria that you
I have a question related to design patterns, I am developing an MVC app
i have a question that is more design and architecture related. I am coming
I am using c3P0 in my system. I have design question related to changing
I have a design and object structuring related question. Here is the problem statement:
My question is related class design. It is really hard to explain everything here

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.