473,663 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function object, this and constructor

This similar situation will explain somewhat, what I want to do:

class X {
public:
X() : map_(*this) {}

bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { ... }

private:
map<int, int, Xmap_;
};

Why do I want this? Because the goodStuff function is already there
and I don't want to write it twice. This piece of code doesn't work as
one would expect. I also tried doing things like

class X {
public:
X() : map_( ptr_fun(&X::stu ff) ) {}

bool stuff(int a, int b) const { return goodStuff(a) <
goodStuff(b); }

private:
map< int, int, binary_function <int, int, bool map_;
};

but this works neither. Is there a way to do this thing. The X objects
are a pain to construct (lots of big stuff in 'em) so I don't want to
generate them anymore than I have to. I guess that second could be
made to work if the goodStuff would be static, but it ain't.

This isn't a stopper problem, I know a way around this (problem-
specific), but it stinks. I want to know how to do this "the way it
should be done".

Jul 8 '07 #1
5 1537
vulpes wrote:
This similar situation will explain somewhat, what I want to do:

class X {
public:
X() : map_(*this) {}

bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { ... }

private:
map<int, int, Xmap_;
};
So you construct the map<int,intvari able from the X class.
Take a look here what are map constructors expecting:
http://www.cppreference.com/cppmap/m...structors.html
>
Why do I want this? Because the goodStuff function is already there
and I don't want to write it twice. This piece of code doesn't work as
one would expect. I also tried doing things like

class X {
public:
X() : map_( ptr_fun(&X::stu ff) ) {}

bool stuff(int a, int b) const { return goodStuff(a) <
goodStuff(b); }

private:
map< int, int, binary_function <int, int, bool map_;
};
What is ptr_fun?
Here is the same as the previous. You want to construct the map<int,int>
object from ptr_fun (whatever that is)

>
but this works neither. Is there a way to do this thing. The X objects
are a pain to construct (lots of big stuff in 'em) so I don't want to
generate them anymore than I have to. I guess that second could be
made to work if the goodStuff would be static, but it ain't.

This isn't a stopper problem, I know a way around this (problem-
specific), but it stinks. I want to know how to do this "the way it
should be done".
You message was not clear enough for me to understand what exactly you
want to do.
Why your solution stinks?
Jul 9 '07 #2
On Jul 9, 1:11 am, vulpes <ntv...@luukku. comwrote:
This similar situation will explain somewhat, what I want to do:
class X {
public:
X() : map_(*this) {}
bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { ... }
private:
map<int, int, Xmap_;
};
Why do I want this? Because the goodStuff function is already there
and I don't want to write it twice. This piece of code doesn't work as
one would expect.
What would one expect? It's undefined behavior. (I expect that
it will not compile with most implementations . I can't think of
any reasonable implementation of map where the class itself
doesn't contain an instance of the comparator. So what you've
written is that X contains a map which contains an X...)
I also tried doing things like
class X {
public:
X() : map_( ptr_fun(&X::stu ff) ) {}
bool stuff(int a, int b) const { return goodStuff(a) <
goodStuff(b); }
private:
map< int, int, binary_function <int, int, bool map_;
};
but this works neither. Is there a way to do this thing.
The obvious (but not necessarily good) solution is to replace
the map with a pointer to the map. Alternatively, just about
any form of indirection in the comparator should work:

class X ;
class XCmp
{
public:
XCmp( X const* owner ) ;
bool operator()( int, int ) const ;

private:
X const* myOwner ;
} ;

class X
{
// ...
private:
std::map< int, int, XCmp >
myMap ;
} ;
The X objects
are a pain to construct (lots of big stuff in 'em) so I don't want to
generate them anymore than I have to.
Then you almost certainly don't want to use one directly as
comparator. The actual map uses a *copy* of the object it is
passed in the constructor (or a default constructed instance, if
it isn't passed a comparator).
I guess that second could be
made to work if the goodStuff would be static, but it ain't.
You could probably work out something using boost::bind. The
basic problem is that you have a function with three arguments
(the X and the two ints), and map will want to call it with two,
so you need to use a functional object which contains a pointer
to (or a reference to) the X. And you can't do it with the
standard binders, because the (current) version of the standard
doesn't support functional objects with more than two
parameters.
This isn't a stopper problem, I know a way around this
(problem- specific), but it stinks. I want to know how to do
this "the way it should be done".
"The way it should be done" probably depends on the context, but
most likely involves a comparator object with a pointer to (or a
reference to) the owning X.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 9 '07 #3
vulpes wrote:
This similar situation will explain somewhat, what I want to do:

class X {
public:
X() : map_(*this) {}

bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { ... }

private:
map<int, int, Xmap_;
};
#include <map>
using namespace std;

class X {
public:
X(){}

bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { return a; }
};

class Y
{
public:
Y():map_(x){}
private:
map<int, int, Xmap_;
X x;
};

int main()
{
Y x;
}

Is this good enough for your use?
Jul 9 '07 #4
On Jul 9, 1:06 pm, anon <a...@no.nowrot e:
class X {
public:
X(){}

bool operator() (int a, int b) { return goodStuff(a) <
goodStuff(b); }
int goodStuff(int a) { return a; }
};

class Y
{
public:
Y() : map_(x){}

private:
map<int, int, Xmap_;
X x;
};

int main()
{
Y x;
}

Is this good enough for your use?
Well it's fine otherwise, but I'd rather let the class X be the main
character in the plot. Here's why: The class X is actually one of
several concretizations to an interface class and I use the class X
exclusively through that interface. It would be an unnecessary
complication to create a whole facade in front of X. Therefore I
wouldn't use your solution, it's like an inverse Adapter. But at least
there seems to be a slew of ways to do it, if you just keep pressing
the issue.

Thanks for your help. If you want, I'll post my final decision and
code here when I get to it (This evening European time I'd wager).
Someone may benefit from it later on.

Jul 9 '07 #5

anon <an**@no.nowrot e in message...
>
What is ptr_fun?
Here is the same as the previous. You want to construct the map<int,int>
object from ptr_fun (whatever that is)
[ from STL docs ]

- Description -
Ptr_fun takes a function pointer as its argument and returns a function
pointer adaptor, a type of function object. It is actually two different
functions, not one (that is, the name ptr_fun is overloaded). If its
argument is of type Result (*)(Arg) then ptr_fun creates a
pointer_to_unar y_function, and if its argument is of type Result (*)(Arg1,
Arg2) then ptr_fun creates a pointer_to_bina ry_function.

- Definition -
Defined in the standard header <functional>, and in the nonstandard
backward-compatibility header <function.h>.

--
Bob R
POVrookie
Jul 10 '07 #6

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

Similar topics

12
3278
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global function { MFVec res = z1; res += z2 return res; // WHY???
5
2863
by: bipod.rafique | last post by:
Hello All, I am little confused about the constructor function. Say I have a class like the following class test{ int a; public: test(){}
2
6062
by: Lateralus | last post by:
headers/vector3.h: In member function ‘Vector3 Vector3::operator*(Scalar)’: headers/vector3.h:13: error: no matching function for call to ‘Vector3::Vector3(Vector3)’ ‘Vector3::Vector3(Vector3)’ headers/vector3.h:10: note: candidates are: Vector3::Vector3(Vector3&) I'm a little confused as to what my problem is. I get this error for any method which calls the constructor.
6
1692
by: Alexis Nikichine | last post by:
Hello, Today, I have a function: function f() { } and am looking for a way of distinguishing, from inside f, whether it
41
2547
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum += arguments;
17
2011
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find it to be quite handy. Any thoughts? <script type="text/javascript"> function map(func) { var i,j,o; var results = ;
26
2135
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
28
4307
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
5
2233
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
9
23746
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number of objects using the given constructor. The objects should all inherit from a base class. It's not possible to pass actual objects, since it's not given on beforehand, how many should be created.
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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
8858
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...
1
6186
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
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
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.