Everything2
Near Matches
Ignore Exact
Full Text
Everything2

C++: objects with no type

created by ariels

(thing) by ariels (5.5 d) (print)   ?   (I like it!) 1 C! Mon Apr 16 2001 at 8:20:16

C++ is an object oriented language and is strongly typed. This means that every value (rvalue, not just lvalue) has a type. That's how the language knows what you mean to do on the value.

Classes are not objects in C++, but that's OK, because they're never values either. Still, some things in C++ just don't have a type! The example below comes from Andrei Alexandrescu's book Modern C++ Design, and involves pointers to member functions.

OK, so lets say I have this code (no joking, this really is C++!):

Object o,*oo;     // Some objects
int (Object::*ptr_to_o_func)(double);
              // 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_func)(3.14) + (oo->*ptr_to_o_func)(2.72)
  << endl;
Apart from the atrocious syntax (especially the weird constructs ::*, .* and ->*), this is fundamentally weird.

Note that o.*ptr_to_o_func and oo->*ptr_to_o_func are functors: they're objects for which operator()(double) is defined. So what's their type? Well, they don't have one! They're strange closures which bind together an object and a pointer to one of its member functions. If they had a type, you could store this "frozen function" in a variable of that type. But that would be too weird. So these two values have no type! Which doesn't prevent the compiler from type-checking their use, of course (you cannot pass oo->*ptr_to_o_func anything except a double, and you'll receive an int in return).

So much for strong typing...


printable version
chaos

->* functor The C++ Programming Language Freakshow WARNING: Noders May Not Be What They Seem to Be
C++: brackets to make an object not a function lvalue classless addressing Degrees of Pornography
rvalue function pointer pointer to member function class object
&gt;?= pointer to member Closure French Equatorial Africa
Searching E2 using mouse gestures in Mozilla double Visual Basic Object Properties ::
Croatian Breen Syntax id
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Look at this mess the Death Borg made!
Ice cream
So you think you're Bruce Lee
Poets' Corner
This freaking obsession with really abysmal porn
smoked salmon
The Amityville Horror
Bait al-Hikmah
The Contents Of Lincoln's Pockets
monster truck
Humorous Writings of E2
Benoit Mandelbrot
Memento
A New Student and Her Father
New Writeups
Lord Brawl
Dr. Horrible's Sing-Along Blog(review)
a8ksh4
regret(idea)
Heisenberg
Editor Log: July 2008(log)
sam512
halfway homes, catacombs, twilight zones(fiction)
Timeshredder
The Texas UFO Crash of 1897(event)
Heitah
The Dark Knight(review)
ignis_glaciesque
Uppsala(place)
ignis_glaciesque
diffusion of responsibility(idea)
TheOrientalAfrican
The Soft Meadow of my Childhood(event)
BookReader
The Dragon Slayers(fiction)
kohlcass
religiously fashionable(review)
Pavlovna
waulking song(thing)
tentative
Stick Man(poetry)
Ereneta
The Fight with the Snapping Turtle: Or, the American St. George(poetry)
sitaraika
Fog and fire(personal)
Everything 2 is brought to you by the letter C and The Everything Development Company