Skip to main content
gamedev.net
🔒 Locked

Passing paramters

Started by s3202 May 28, 2002 at 1:38 PM 6 replies 400+ views
Original Post
s3202
s3202
I once casted an object pointer to a void pointer, passed that pointer to another function, then casted it back to an object pointer. Why I wanted to do this take much explaining and isn''t important. What I want to know is, is there some danger to doing this that I should be aware of? ---- "Caw, caw, BANG f**k I''m dead!" --The Crow
----Herb M. (s3202)
sjelkjd
sjelkjd
Not if you''re absolutely sure that the pointer actually is an object pointer.

Although an accepted technique in c# and java, this is poor c++.
felonius
felonius
>> Although an accepted technique in c# and java, this is poor c++.

There is no such this as a void pointer in Java. (I don''t know about C#). Java do not have pointers, remember, and references cannot be made to a void.

It is true that this is generally poor OO, but this is still often done in both C and C++. Even in the Kernighan and Ritchie book several of the example use this technique.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
WayfarerX
WayfarerX
quote:
Original post by felonius
>> Although an accepted technique in c# and java, this is poor c++.

There is no such this as a void pointer in Java. (I don''t know about C#). Java do not have pointers, remember, and references cannot be made to a void.


An Object refrence in Java is conceptually similar to a C void pointer, except that it cannot refrence a core datatype.
Down casting is accepted in Java because the VM will not allow you to cast a refrence to a type the object does not implement, it will throw an exception.

C++ has a lot of features that help you use its strong type-safety, don''t just throw them away.

"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
s3202
s3202
That''s why I asked. That''s how I feel about the technique, too. It seems very unlike C++ and it''s steroidal type checking.

----
"Caw, caw, BANG f**k I''m dead!" --The Crow
----Herb M. (s3202)
felonius
felonius
>> An Object refrence in Java is conceptually similar to a C void pointer, except that it cannot refrence a core datatype.

It might be remotely similar but it is not the same. A Java Object reference is basicly the same as had you a pointer to a pointer to a common base class in C++ and you only used dynamic_cast() in C++. dynamic_cast() will return 0 if the cast is illegal. So if an Object reference in Java was the same as void pointer in C/C++ why would the C++ designers add another construction? (i.e. dynamic_cast() and inheritance). And remember, the Java class Object also has methods and fields - unlike the void pointer which is a pointer to something completely unknown.

[edited by - felonius on May 28, 2002 5:41:24 PM]
Jacob Marner, M.Sc.Console Programmer, Deadline Games
sjelkjd
sjelkjd
Object is the moral equivalent of a void*. If you want to talk about functional equivalence, look at boost::any.

When I talked about technique, I meant casting to void*/Object and then casting back. The concept, rather than any specific language's version of it.

I still contend that Object is just like a void*, and is used to make up for the lack of generics.

[edited by - sjelkjd on May 28, 2002 6:29:59 PM]
sjelkjd
sjelkjd
quote:
Original post by felonius
Java do not have pointers, remember, and references cannot be made to a void.



Sure looks like it to me

quote:

It is true that this is generally poor OO, but this is still often done in both C and C++. Even in the Kernighan and Ritchie book several of the example use this technique.


C != C++ !!!!!!!
Get that through your head. K&R is C. It is not c++. I doubt you will find ANY respectable c++ author advocating the use of void*. If you really need something like that, a more appropriate choice is boost::any.


Oh, and for the record, void* is perfectly acceptable C.

[edited by - sjelkjd on May 28, 2002 6:43:44 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.