Topic: Passing Objects as parameters


Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/02/04
Raw View
James Kuyper wrote in message <36B7A889.5724DCCD@wizard.net>...
>Rob wrote:
>>
>>    Is it possible in C++ to pass a generic object as a parameter to a
>> method. Using JAVA, it is possible to code :
>>
>>                     void MyMethod(Object new_object)
>>
>> and any type of object can be passed to function since all objects are
>> descended (inherited) from the master Object class.
>
>C++ doesn't have a master Object class. The closest you can get is to
>use 'void *', and MyMethod() would have to have some way of identifying
>at least a base class of the actual type, before it could use any
>virtual methods or RTTI.

Hey, that's not too happy a solution.
Why not having an Object polymorphic base class and deriving your objects
from it appropriately? Or you can use templates if you statically know what
the story is.

The "genericity" of Java is only because everyone derives from Object, which
you can simulate in C++. BTW, it doesn't fulfill even remotely my definition
of genericity.

Andrei
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Rob" <odin@valhalla.net>
Date: 1999/02/02
Raw View

   Is it possible in C++ to pass a generic object as a parameter to a
method. Using JAVA, it is possible to code :

                    void MyMethod(Object new_object)

and any type of object can be passed to function since all objects are
descended (inherited) from the master Object class.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Edward Diener <eddielee@abraxis.com>
Date: 1999/02/03
Raw View
C++ has no single hierarchy of classes and, therefore, no class that
represents a base class for all other classes. Therefore the concept of a
generic object does not exist in C++. There is no master "object" class in
C++. The best you can do is:

void MyMethod(void * anypointer);

but then you must know what are the possibilities to which your pointer
points and you must cast accordingly to some base and then use polymorphism
or dynamic_cast to accomplish what you want. This is a real kludge in C++ and
I don't recommend that type of C++ programming to anyone. Perhaps the
Smalltalk and Java model, among others, of a single base class for all
classes is not such a great concept anyway ( he said rhetorically ) <g>.

Rob wrote:

>    Is it possible in C++ to pass a generic object as a parameter to a
> method. Using JAVA, it is possible to code :
>
>                     void MyMethod(Object new_object)
>
> and any type of object can be passed to function since all objects are
> descended (inherited) from the master Object class.
>



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1999/02/03
Raw View
Rob wrote:
>
>    Is it possible in C++ to pass a generic object as a parameter to a
> method. Using JAVA, it is possible to code :
>
>                     void MyMethod(Object new_object)
>
> and any type of object can be passed to function since all objects are
> descended (inherited) from the master Object class.

C++ doesn't have a master Object class. The closest you can get is to
use 'void *', and MyMethod() would have to have some way of identifying
at least a base class of the actual type, before it could use any
virtual methods or RTTI.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]