Storing class names in a string?

Started by
26 comments, last by stodge 21 years, 6 months ago
Ok, just to take this a step further and really delve into your brains, what about the following addition.

How could I do something similar but with the method; that is how could I tell the client which method to call on the object? I''m thinking of passing an object ID to the client which will then grab the instance of this object. It will then need to call the appropriate method as dictated by the server.

Many thanks!


http://www.stodge.net - the powerhouse in personal commentary
---------------------http://www.stodge.net
Advertisement
quote:Original post by stodge
I''m thinking of passing an object ID to the client which will then grab the instance of this object. It will then need to call the appropriate method as dictated by the server.


You can pass around a pointer to the member function. IF the member function is virtual, then it is really only an ID for the method, which can be passed from one machine to another (so long as the object layout is the same, that the apps have been compiled with the same compiler & so on). Then, you can call it with object.*member_pointer(); or object_pointer->*member_pointer().

If this doesn''t work, you can keep a static member function pointer table for the object class in the class (and, possibly, register it along with the builder function, or provide a virtual BaseObject method that returns a pointer to the table). Then use the ID as an offset into that table to get the pointer, and call it as above (that''s what the previous method does, only it directly uses the object''s VTBL).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:You can pass around a pointer to the member function.


You''re kidding! I would never have imagined that that was possible. Is that also possible cross platform or not?

Thanks


http://www.stodge.net - the powerhouse in personal commentary
---------------------http://www.stodge.net
quote:Original post by stodge
You're kidding! I would never have imagined that that was possible. Is that also possible cross platform or not?


No, you have to ensure that the object layout is exactly the same. That implies using the same compiler on both sides and therefore precludes having different architectures on both sides. Cross-compilers might work, but I would not bet on it.

And it's only possible for virtual member functions, as such a pointer is in fact an offset into the virtual function table (on implementations that use a vtbl... that is most of them.). However, in general, you cannot pass a pointer from an address space (process) to another. That includes saving them, and so on (that's also why member functions of objects that are in a DLL must be virtual).

quote:From Stroustrup's "The C++ Programming Language, 3rd ed", p420
Because a pointer to a virtual member is a kind of offset, it does not depend on a object's location in memory. A pointer to a virtual member can therefore safely be passed between different address spaces as long as the same object layout is used in both. Like pointers to ordinary functions, pointers to non-virtual member functions cannot be exchanged between address spaces.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 4, 2002 4:08:20 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ah I see. Thanks. I don''t plan on trying with the same compiler, it would be GCC 2.9 and VC6. Still that''s interesting.


http://www.stodge.net - the powerhouse in personal commentary
---------------------http://www.stodge.net
quote:Original post by stodge
Ah I see. Thanks. I don''t plan on trying with the same compiler, it would be GCC 2.9 and VC6. Still that''s interesting.


Get gcc 3.2, it''s much better. Especially if your version is older than 2.95.6
Incidentally, if you use cygwin, you can ask it to install gcc 3.2 instead of 2.95. You could try to use it to compile your code on the windows side and see if the object layout is the same as on *nix. (Use the no-cygwin option if you want to write Win32 code and not be bound by the GPL).

Yeah, it''s interesting, that''s why I like C++ ... and the lesser-known (to most people) parts of it

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I can see what you mean; the potential here is quite exciting for SAOB: stodge's autonomous object model. rofl


http://www.stodge.net - the powerhouse in personal commentary

Edit: major mistake there!

[edited by - stodge on October 4, 2002 4:57:06 PM]
---------------------http://www.stodge.net
If you're interested in that kind of things, I could suggest you buy "Inside the C++ Object Model", by Lippman, which explains how C++ usually implements its objects and stuff at the binary level (though no standard exists for that). It's very enlightening (and once you've read it, you'll never ask again "Why can't I cast a pointer-to-member into a function pointer". (I already told you why for virtual functions : it's not a pointer )).

edit: fixed misleading statement

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 4, 2002 5:05:10 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement