473,606 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design question - way to specify what attribute of an object to process in a generic function

I was wondering if anyone had design advice on this. . . . I am
doing some mathematical operations looking at different metrics for
data (in objects) I have captured. The object class has several data
attributes, say: metric1, metric2, . . . metricN. These are different
metrics on which I am running statistics.

There are a number of operations I have to do on these objects for
each metric separately: sort, group (i.e., group a list of objects
based on a metric), etc. I understand the very basics of C++ template
functions, overloading, etc. However, I cannot figure out a way to
design a generic function to sort by each single attribute. So, I end
up with functions like: sort_by_metric1 , sort_by_metric2 , . . .
sort_by_metricN .

Is there a way to avoid so many nearly-duplicative functions? For
example, is there a way in the function call to tell the function what
metric I want to sort by? This would allow me to use one, generic
function vice a bunch of nearly-duplicative ones.

I hope this is clear. If not, please let me know, and I will
attempt to clarify.

Thanks, Alan

Jun 16 '07 #1
5 1377
Alan wrote:
....
Is there a way to avoid so many nearly-duplicative functions?
You can use pointer to member as a parameter to your comparison function
for sorting.

Look up "pointer to member" in your fav C++ book - if you have further
questions, post again.

BTW - pointer to member may be a template parameter OR a value
parameter. It probably makes little difference.

Another wat is to code up different "getter" functions that get
different metrics and use those functions in your sort parameters.
Jun 17 '07 #2
Alan wrote:
I was wondering if anyone had design advice on this. . . . I am
doing some mathematical operations looking at different metrics for
data (in objects) I have captured. The object class has several data
attributes, say: metric1, metric2, . . . metricN. These are different
metrics on which I am running statistics.
Something like

#include <vector>

struct object {
double metric1, metric2 ... metricN;
}

....
std::vector<obj ectdata;
.... insert objects into data.
There are a number of operations I have to do on these objects for
each metric separately: sort, group (i.e., group a list of objects
based on a metric), etc. I understand the very basics of C++ template
functions, overloading, etc. However, I cannot figure out a way to
design a generic function to sort by each single attribute. So, I end
up with functions like: sort_by_metric1 , sort_by_metric2 , . . .
sort_by_metricN .
#include <algorithm>
#include <functional>

class less_metric1 : std::binary_fun ction<object, object, bool>{
public:
return_type operator()(cons t object& a, const object& b) {
return a.metric1 < b.metric1;
}
}

....
std::sort(data. begin(), data.end(), less_metric1()) ;
Is there a way to avoid so many nearly-duplicative functions? For
example, is there a way in the function call to tell the function what
metric I want to sort by? This would allow me to use one, generic
function vice a bunch of nearly-duplicative ones.
This is the purpose of templates.

--
rbh
Jun 17 '07 #3
Thank you. This was very helpful. Alan

Jun 17 '07 #4
On Jun 16, 10:33 pm, Alan <jalantho...@ve rizon.netwrote:
Thank you. This was very helpful. Alan
Robert,
I do not really understand what the line

return_type operator()(cons t object& a, const object& b)

does in your definition of the binary function. Can you explain?

I think I understand the rest now. Thank you for the suggestion.

Alan

Jun 18 '07 #5
Never mind. I found some information and discovered on another
thread that I should use "result_typ e."

Thanks again, Alan

Jun 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
4825
by: grz02 | last post by:
Hi, Im an experienced database+software designer and developer, but, unfortunately, anything to do with web-programming and web-systems designs is still a pretty new area to me... (been working mostly with "legacy" environments the last 10 years) So I am writing this, hoping to get some useful advise and feedback... I have done some pretty trivial, small websites with html/PHP,
4
1738
by: Danimal | last post by:
I have been using PHP for a long time... since it was called PHP/FI. I have a programming design question: Let say I have this class: class attrib { var $lenght; var $type; ... }
9
2452
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking the wrong questions about the wrong 'topic' (polymorphism) I have posted this new question. Please don't see this as a spurious attempt to repost :-) As mentioned previously, there is a very real problem I am trying to solve, but I have reduced...
2
3706
by: Anders | last post by:
We need to create a lookup structure that can contain a flexible amount of attributes. We have discussed different design solutions for lookup-functionality, and come up with this suggestion: The "lookup" structure will be defined by three tables. The "Element" table, the "Attribute" table and the "Value" table. I will specify with an example:
6
3816
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the handler? For example, let's say that we have to implement the ISO 7-layer protocol, or something like an Allen-Bradley master-slave protocol. At the lowest layer we might only need to top and tail the data being transported, such as DLE STX DLE...
7
2353
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a WinForms interface and then need to port that to a web app. I am kinda stuck on a design issue and need some suggestions / direction. Basically I have a business layer that I want to use to process any dataentry logic (row focus changes, data...
37
1500
by: Gregory Guthrie | last post by:
I am comparing Python to a few other scripting languages, and used a simple anagrams program as a sample. I was surprised ast a few python features that did not work as I would expect/wish; which caused less compact/expressive program styles that I wanted - reverting to a FORTRAN like series of assignments. For example, - why is len() not a member function of strings? Instead one says len(w).
3
2723
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this OLE Object (load a picture from a given path). Something like this I would expect: _reports.crImage local_Report = new _reports.crImage();
3
1294
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The current non-generic implementation is functional, but not as clean as I would like. Although not sure, I believe my problems stem from lacking support of co- and contravariance in C# (which I'm desperately hoping will make it in the next version)....
0
7962
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8443
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3945
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1309
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.