473,569 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Objects w/ no type?!!?

Read this code I found on the Internet:

Object o,*oo; // Some objects
int (Object::*ptr_t o_o_func)(doubl e);
// ptr_to_obj_func is a pointer to a
// member function of Object

oo = getObjectPtr();

ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar ;

std::cout << "Value is " <<
(o.*ptr_to_o_fu nc)(3.14) + (oo->*ptr_to_o_func )(2.72)
<< endl;

o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!

Jul 2 '07 #1
6 1539
On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:

Object o,*oo; // Some objects
int (Object::*ptr_t o_o_func)(doubl e);
// ptr_to_obj_func is a pointer to a
// member function of Object

oo = getObjectPtr();

ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar ;

std::cout << "Value is " <<
(o.*ptr_to_o_fu nc)(3.14) + (oo->*ptr_to_o_func )(2.72)
<< endl;

o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).

I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.

--
Erik Wikström
Jul 2 '07 #2
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-07-02 23:30, Protoman wrote:


Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_t o_o_func)(doubl e);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar ;
std::cout << "Value is " <<
(o.*ptr_to_o_fu nc)(3.14) + (oo->*ptr_to_o_func )(2.72)
<< endl;
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!

o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).

I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Yes, I know but this guy says they have no type;
http://www.everything2.com/index.pl?...th%20no%20type

Jul 2 '07 #3
Protoman wrote:
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia. comwrote:
>On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_t o_o_func)(doubl e);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar ;
std::cout << "Value is " <<
(o.*ptr_to_o_fu nc)(3.14) + (oo->*ptr_to_o_func )(2.72)
<< endl;
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!

o.*ptr_to_o_fu nc and oo->*ptr_to_o_fu nc are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).

I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.

Yes, I know but this guy says they have no type;
http://www.everything2.com/index.pl?...th%20no%20type

This person is either confused, or he is using a different definition of
type than the standard. His argument is that because

o.*ptr_to_o_fun c

cannot be assigned to anything, it has no type. Odd that he has never tried

o.foo,

which has the same "problem". Both o.*ptr_to_o_fun c and o.foo has the same
type: "The expression designates a nonstatic member function" says the
standard in 5.2.5. Such an expression can only be used with the function
call operator.

--
rbh
Jul 2 '07 #4
On 2 Jul, 16:40, Robert Bauck Hamar <roberth+n...@i fi.uio.nowrote:
Protoman wrote:
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_t o_o_func)(doubl e);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar ;
std::cout << "Value is " <<
(o.*ptr_to_o_fu nc)(3.14) + (oo->*ptr_to_o_func )(2.72)
<< endl;
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are functors. Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions? I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are member function calls, and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).
I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.
Yes, I know but this guy says they have no type;

http://www.everything2.com/index.pl?...bjects%20with%...

This person is either confused, or he is using a different definition of
type than the standard. His argument is that because

o.*ptr_to_o_fun c

cannot be assigned to anything, it has no type. Odd that he has never tried

o.foo,

which has the same "problem". Both o.*ptr_to_o_fun c and o.foo has the same
type: "The expression designates a nonstatic member function" says the
standard in 5.2.5. Such an expression can only be used with the function
call operator.

--
rbh- Hide quoted text -

- Show quoted text -
OK. BTW, are C++ functors lke closures in other languages? Like
"frozen" functions?

Jul 3 '07 #5
On Jul 3, 12:16 am, Protoman <Protoman2...@g mail.comwrote:
On 2 Jul, 15:08, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-07-02 23:30, Protoman wrote:
Read this code I found on the Internet:
Object o,*oo; // Some objects
int (Object::*ptr_t o_o_func)(doubl e);
// ptr_to_obj_func is a pointer to a
// member function of Object
oo = getObjectPtr();
ptr_to_o_func = (//some boolean condition) ?
&Object::foo : &Object::bar ;
std::cout << "Value is " <<
(o.*ptr_to_o_fu nc)(3.14) + (oo->*ptr_to_o_func )(2.72)
<< endl;
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are functors.
I'm not sure. Functors are normally objects, not expressions.
you can't assign "o.*ptr_to_o_fu nc" to anything, for example.
Aren't functors
like clsures that bind together an object w/ a pointer to one of it's
member functions?
Again, not necessarily. The usual definition is that they are
objects which can be "called"; objects for which the () operator
is defined.
I heard they don't have a type. What? What if they
did have a type? How can the compile type-check them? Thanks!!!!
o.*ptr_to_o_fun c and oo->*ptr_to_o_fu nc are member function calls,
No they're not. They're "pm-expression"s, according to the
standard. And (again, according to the standard) their type is
"an object or a function of the type specified by the second
operand", here, a function. The resulting expression has the
type "function (double) returning int". And since the
expression isn't an lvalue, you can't take its address. In a
sense, C++ has closures, but only as temporary "objects", like
here.
and
their type (if they can be said to have any) is the type of the return
type of the function calls (in this case int).
That's the type of the results of the () operator, once it's
applied to the above expression.
I'm not sure you can claim that they have a type since they are
statements, you tell the computer to do something, in this case call the
function pointed to by ptr_to_o_func on the obects o and the object
pointed to by oo.
I think they have a type. The standard says that they have a
"function" type.
Yes, I know but this guy says they have no type;http://www.everything2.com/index.pl?...bjects%20with%...
He's wrong. He just doesn't understand C++ typing as it applies
to functions. (But I'll admit that it's not always evident.)

--
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 3 '07 #6
On Jul 3, 9:26 am, Protoman <Protoman2...@g mail.comwrote:
TW, are C++ functors lke closures in other languages? Like
"frozen" functions?
Sort of. You can make something that is very close to a closure (ahem)
with a functor. You can also use them to do something similar to
currying.

struct power {
power( float second ) m_pow( second ) {}
float operator()( float first ) const { return std::powf( first,
m_pow ); };
private:
float m_pow;
};

power now acts like a curried version of std::powf - although beware,
I'm binding the second argument not the first so in a language like
Haskell you'd need to use flip too.

power cube( 3 );
cube( 10 ); // Will return 1,000

Note that the implementation of power uses what could be seen as a
closure. In C++ you don't get automatic closure of arguments within
the same lexical scope. You have to put them into the closing
structure yourself.

All of this is much clearer using Boost.Function and Boost.Lambda
where you can play with functional idioms more easily.

If you want to see this sort of thing in action, consider a function
for executing some script and returning a result. The lambda that will
execute the script has this type:

boost::function < _variant_t ( wstring ) >

This means that it takes a wstring (the source) and produces a
_variant_t (a wrapper for the Windows COM type VARIANT) which is the
result of the script execution.

Now to store a context for dynamic execution of a script I have this:

class Translator { // Creates HTML from some internal format
// snip
boost::function < _variant_t ( wstring ) dynWriter;
};

I initialise this through a function that looks like this (abridged
slightly to make it clearer what is going on from some code for a
multi-threading JavaScript host):

boost::function < _variant_t ( wstring ) embed() {
boost::shared_p tr< Mahlee mahlee( new Mahlee );

return boost::lambda:: bind(
&Mahlee::run ,
boost::lambda:: bind(
&boost::shared_ ptr< FSLib::Process: :Mahlee >::get,
mahlee ),
boost::lambda:: _1 );
}

The first line creates a JavaScript interpreter and puts it into a
shared_ptr. For the lambdas it makes two. The inner one simply creates
a closure around the mahlee object and allows us to get the object
pointed to through the shared_ptr's get member.

The outer lambda then creates a functor object that embeds the
following:

mahlee->run( _1 )

This is stored in the dynWriter member earlier:

dynWriter = embed();

Now dynWriter has become a function that given some JavaScript
executes it and returns the result of the code into a _variant_t:

dynWriter( L"function() { return 'Hello world!'; }()" );

This will return a _variant_t with the string Hello world! in it.

The hardest part doing this in C++ is that you have to be very careful
to think through the full implications of object lifetimes. You can
see a more advanced use that shows the difference between generalised
partial application and currying on the second half of this page from
my web site: http://www.kirit.com/News:/1720702
K

Jul 3 '07 #7

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

Similar topics

49
2848
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another class). Any comments would be appreciated! Thanks! Steve ----------------------------------------------------------------------
100
5183
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
0
17753
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico Mariani, performance architect for the Microsoft® .NET runtime and longtime Microsoft developer, mentioned to Dr. GUI in an e-mail conversation...
14
2489
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic types in objects. But if they are objects why it´s need this conversion? Aren´t objects (basic types) like Java?
161
7757
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
7
8208
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
27
2530
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the Standard is saying that "object is a region of storage", I deduce from that that literal constants aren't objects because they may not be alocated...
28
1583
by: walterbyrd | last post by:
Python seems to have a log of ways to do collections of arbitrary objects: lists, tuples, dictionaries. But what if I want a collection of non-arbitrary objects? A list of records, or something like that?
15
3505
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of...
19
10735
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is there an existing adapter? Thanks, Daniel.
0
7703
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...
0
7618
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...
0
7926
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. ...
0
8138
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...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
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...
0
5223
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...
1
2117
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
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.