Hi I have a problem, I hope you can help me with.

I have a number, which I need to compare to some values.

I do not wish to go through all the values, and compare them individually, as I there are too many for it to be efficient.

Neither do I want to create an array with the size of the range of numbers, as it can anything from 0 UINT_MAX

So I am hoping that you might have a better solution.

Here is an example of what I have tried, but didn't work,
because I get the values dynamicly

class Mother
{
public:
    Mother(){}
    ~Mother(){}

    template<int msg> inline void HandleMessage(){};
    
    template<> inline void HandleMessage<10>()
    {
        std::cout<<"It works!\n";
    }
};

Recommended Answers

All 6 Replies

Hi I have a problem, I hope you can help me with.

I have a number, which I need to compare to some values.

I do not wish to go through all the values, and compare them individually, as I there are too many for it to be efficient.

Neither do I want to create an array with the size of the range of numbers, as it can anything from 0 UINT_MAX

So I am hoping that you might have a better solution.

Here is an example of what I have tried, but didn't work,
because I get the values dynamicly

class Mother
{
public:
    Mother(){}
    ~Mother(){}

    template<int msg> inline void HandleMessage(){};
    
    template<> inline void HandleMessage<10>()
    {
        std::cout<<"It works!\n";
    }
};

I think you'll have to be a bit more specific about what you're after, what needs to be compared, stuff like that. I don't know what to make of the code snippet, as it has no comparisons. off the top of my head, if you have ordered data, use a binary search so you don't have to compare every element, but again, without having any idea what kind of data you have or what kind of comparisons you need to make, it's hard to speculate.

Well it is an integer, compared to a list of integers.

Well it is an integer, compared to a list of integers.

Any reason a binary search won't work?

Too time consuming. I would rather not do it at all then

And about the code snippet, if I create an instance of Mother,
and call HandleMessage<10> it would print "It works!\n".

But if I call it with any other value it would do nothing,
the reason I am not doing this, is that I don't know the values at compile time.

Too time consuming. I would rather not do it at all then

And about the code snippet, if I create an instance of Mother,
and call HandleMessage<10> it would print "It works!\n".

But if I call it with any other value it would do nothing,
the reason I am not doing this, is that I don't know the values at compile time.

Care to give a little more background?

Ok I am building a GUI Framework, where there is a Widget class,
which can both have mother widgets and be a mother widget.

When some event happens, the widget receives a message, in the form of an integer value(win32).
I want the widgets to be able to say to their children "I want to know when this happens", and then the mother widget's mother to be able do the same.

The alternative to this subscribe behavior, is to send some of the messages to the widget's mother

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.