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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:12+00:00 2026-06-13T12:59:12+00:00

So, I have got the following classes and methods: Property : Has a single

  • 0

So, I have got the following classes and methods:

Property: Has a single member of type int (named mTag)

TypedProperty: Inherits from the Property class and adds a member called mValue of type T to it.

PropertyList: A class which Maintains a std::set of Property and has an Add and Print method.

CheckSubset: A method which checks if a std::set is included in another set.

I don’t know how I should implement the CheckSubset method. Because I do not know how to iterate through a set<Property> and access to the template member (mValue). I also tried to use the includes method, which did not work (even if it worked, I would have no idea how it did!). The same problem exists in the PropertyList::Print method, where I do not know what cast should be used.
Any advice on the implementation of CheckSubset and Print methods would be appreciated!

Updated source code (using pointer)

#include <string>
#include <iostream>
#include <set>
#include <algorithm>
#include <tr1/memory>

using namespace std;

/////////////////// Property Class //////////////////////

class Property
{
public:
    Property(){};
    Property(const int tag)
            : mTag(tag) {}
    virtual ~Property() {}
    int mTag;
    bool operator<(const Property &property) const
    {
        return mTag < property.mTag;
    }
};

/////////////////// TypedProperty Class /////////////////

template< typename T >
class TypedProperty : public Property
{
public:
    TypedProperty (const int tag, const T& value)
            : Property(tag), mValue(value){}
    T mValue;
};

/////////////////////////////////////////////////////////

typedef std::tr1::shared_ptr<Property> PropertyPtr;

/////////////////// PropertyList Class /////////////////

class PropertyList
{
public:
    PropertyList(){};
    virtual ~PropertyList(){};
    template <class T>
    void Add(int tag, T value) 
    {
    PropertyPtr ptr(new TypedProperty<T>(tag, value));
        mProperties.insert(ptr);
    }
    void Print()
    {
    for(set<PropertyPtr>::iterator itr = mProperties.begin(); itr != mProperties.end(); itr++)
    {
        cout << ((PropertyPtr)*itr)->mTag << endl; 
        // What should I do to print mValue? I do not know its type
        // what should *itr be cast to? 
    }
    }
    set<PropertyPtr> mProperties;
};

//////////////////// Check Subset ///////////////////////
/*
 * Checks if subset is included in superset 
 */
bool CheckSubset(set<PropertyPtr> &superset, set<PropertyPtr> &subset)
{
    // How can I iterate over superset and subset values while I do not know
    // the type of mValue inside each Property?
    // I also tried the following method which does not seem to work correctly
    return includes(superset.begin(), superset.end(), 
            subset.begin(), subset.end());
}

int main()
{
    PropertyList properties1;
    properties1.Add(1, "hello");
    properties1.Add(2, 12);
    properties1.Add(3, 34);
    properties1.Add(4, "bye");

    properties1.Print();

    PropertyList properties2;
    properties2.Add(1, "hello");
    properties2.Add(3, 34);


    if(CheckSubset(properties1.mProperties, properties2.mProperties)) // should be true
        cout << "properties2 is subset!" << endl;

    PropertyList properties3;
    properties3.Add(1, "hello");
    properties3.Add(4, 1234);

    if(CheckSubset(properties1.mProperties, properties3.mProperties)) // should be false
        cout << "properties3 is subset!" << endl;
}
  • 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-13T12:59:13+00:00Added an answer on June 13, 2026 at 12:59 pm

    For solving the problem in Print method of PropertyList, you could write a Print method for TypedProperty class, which prints its tag and value.

    But about the problem in accessing mValue which you want to do some operations on, I can’t think of a way using normal types and templates to get the mValue without engaging your parent class Property with template type of TypedProperty (which seems undesirable). But you could get the address of mValue and cast it to void* to eliminate the type problem. This way you will face another problem, that you can not point to value of a void* pointer, so you can not work with your pointer in parent level. Therefore, you should write a method (implemented by TypedProperty) that takes a void* pointer and casts it to the type defined in child and perform the desired operation.

    For example in the following code, I assumed you want to check equality of a value in a TypedProperty with another one of the same type (IsEqual method).

    Now you can implement simply CheckSubset using IsEqual (checking two elements would be like: superItr->IsEqual(subItr->GetValue())).

    class Property
    {
        public:
            Property(){};
            Property(const int tag)
                : mTag(tag) {}
            virtual ~Property() {}
            virtual void* GetValue() = 0;
            virtual bool IsEqual(void* value) = 0;
            virtual void Print() = 0;
            int mTag;
            bool operator<(const Property &property) const
            {
                return mTag < property.mTag;
            }
    };
    
    
    template< typename T >
    class TypedProperty : public Property
    {
        public:
            TypedProperty (const int tag, const T& value)
                : Property(tag), mValue(value){}
            void* GetValue()
            {
                return &mValue;
            }
            bool IsEqual(void* value)
            {
                return *((T*)value) == mValue;
            }
            void Print()
            {
                cout << "Tag: " <<  mTag << ", Value: " << mValue << endl;
            }
            T mValue;
    };
    
    typedef std::tr1::shared_ptr<Property> PropertyPtr;
    
    class PropertyList
    {
        public:
            PropertyList(){};
            virtual ~PropertyList(){};
            template <class T>
                void Add(int tag, T value) 
                {
                    PropertyPtr ptr(new TypedProperty<T>(tag, value));
                    mProperties.insert(ptr);
                }
            void Print()
            {
                cout << "-----------" << endl;
                for(set<PropertyPtr>::iterator itr = mProperties.begin(); itr != mProperties.end(); itr++)
                {
                    (*itr)->Print();
                }
            }
            set<PropertyPtr> mProperties;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following class structure, and have plenty of classes like C
Ive got the following problem. I have a model called user which has a
I have the following classes and methods as below: import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import
I have got the following very simple code: function init() { var articleTabs =
I have got the following issue, my function appends code to a string $string
In some measure of progress since my last question I have got the following
I have got a the following problem: I have got multi-step form where in
I'm following the Django-CMS introductory tutorial and have got everything working up to the
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I've got the following tables: magazine_tags news_tags page_tags content_tags faq_tags They all have exactly

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.