Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can i get the class of an object of which i only have the address?

Reply
Thread Tools

Can i get the class of an object of which i only have the address?

 
 
bob the builder
Guest
Posts: n/a
 
      01-17-2008
Can i get the class of an object of which i only have the address?

Lets say i have 3 classes: AClass,BClass,CClass.
i also have an object of each class: aObject, bObject, cObject
And finally i have an integer: integerX.

Now i let let intergerX be the address of one of the objects (aObject,
bObject or cObject) but i dont know which.
Can get the class of the object at the address contained in integerX?
and how?
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      01-17-2008
bob the builder wrote:
> Can i get the class of an object of which i only have the address?
>
> Lets say i have 3 classes: AClass,BClass,CClass.
> i also have an object of each class: aObject, bObject, cObject
> And finally i have an integer: integerX.
>
> Now i let let intergerX be the address of one of the objects (aObject,
> bObject or cObject) but i dont know which.
> Can get the class of the object at the address contained in integerX?
> and how?


No, having only the address, you can not know type of the object at that
address.
 
Reply With Quote
 
 
 
 
bob the builder
Guest
Posts: n/a
 
      01-17-2008
On 17 jan, 12:53, anon <(E-Mail Removed)> wrote:
> bob the builder wrote:
> > Can i get the class of an object of which i only have the address?

>
> > Lets say i have 3 classes: AClass,BClass,CClass.
> > i also have an object of each class: aObject, bObject, cObject
> > And finally i have an integer: integerX.

>
> > Now i let let intergerX be the address of one of the objects (aObject,
> > bObject or cObject) but i dont know which.
> > Can get the class of the object at the address contained in integerX?
> > and how?

>
> No, having only the address, you can not know type of the object at that
> address.


what a pitty... There isnt some kind of dirty hack to do it?
 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-17-2008
On 17 Led, 12:44, bob the builder <(E-Mail Removed)> wrote:
> Can i get the class of an object of which i only have the address?
>
> Lets say i have 3 classes: AClass,BClass,CClass.
> i also have an object of each class: aObject, bObject, cObject
> And finally i have an integer: integerX.
>
> Now i let let intergerX be the address of one of the objects (aObject,
> bObject or cObject) but i dont know which.
> Can get the class of the object at the address contained in integerX?
> and how?


With some modification you can achieved what you need.

If all candidate classes have 1 common parent class, you can try to
dynamic_cast it to derived class type.

class Base { };
class AClass: public Base { };
class BClass: public Base { };

// ...

Base* ptr = ...; // Get the pointer from somewhere
AClass* aptr = dynamic_cast<AClass*>(ptr);
if (aptr != 0)
{
// ptr points to AClass or class derived from AClass
}

Note: If you have hierarchy of classes, you have to check child
classes and then their parents.

Note 2: Storing pointers in integer variables is bad idea. Many
programs are not portable to other platforms, because their author
supposed, that pointer has the same size as integer.

Note 3: In well designed project you will not need any stuff mentioned
above.
 
Reply With Quote
 
bob the builder
Guest
Posts: n/a
 
      01-17-2008
On 17 jan, 14:33, Ondra Holub <(E-Mail Removed)> wrote:
> On 17 Led, 12:44, bob the builder <(E-Mail Removed)> wrote:
>
> > Can i get the class of an object of which i only have the address?

>
> > Lets say i have 3 classes: AClass,BClass,CClass.
> > i also have an object of each class: aObject, bObject, cObject
> > And finally i have an integer: integerX.

>
> > Now i let let intergerX be the address of one of the objects (aObject,
> > bObject or cObject) but i dont know which.
> > Can get the class of the object at the address contained in integerX?
> > and how?

>
> With some modification you can achieved what you need.
>
> If all candidate classes have 1 common parent class, you can try to
> dynamic_cast it to derived class type.
>
> class Base { };
> class AClass: public Base { };
> class BClass: public Base { };
>
> // ...
>
> Base* ptr = ...; // Get the pointer from somewhere
> AClass* aptr = dynamic_cast<AClass*>(ptr);
> if (aptr != 0)
> {
> * * // ptr points to AClass or class derived from AClass
>
> }
>
> Note: If you have hierarchy of classes, you have to check child
> classes and then their parents.
>
> Note 2: Storing pointers in integer variables is bad idea. Many
> programs are not portable to other platforms, because their author
> supposed, that pointer has the same size as integer.
>
> Note 3: In well designed project you will not need any stuff mentioned
> above.


Thank you, i didnt realize Note 2.

About Note 3, it isnt really a project. i only need a dirty hack.
 
Reply With Quote
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      01-17-2008
bob the builder:

> Can i get the class of an object of which i only have the address?
>
> Lets say i have 3 classes: AClass,BClass,CClass.
> i also have an object of each class: aObject, bObject, cObject
> And finally i have an integer: integerX.
>
> Now i let let intergerX be the address of one of the objects (aObject,
> bObject or cObject) but i dont know which.
> Can get the class of the object at the address contained in integerX?
> and how?



Well I've been more of a C programmer for the last few years but here's my
thinking on it:

Virtual functions are achieved via a V-Table. That is, each object has a
hidden pointer in it that points to a static structure that contains
function pointers. (The Standard doesn't necessitate that this is how it's
done, but this *is* how it's done).

Anyway, if you had an object and if you knew it had virtual functions (or
that its parents had virtual functions) then you could just compare the V-
Table address to the addresses of all other V-Table structures.

Of course, standard C++ provides no means of extracting the V-Table
pointer, but I'm not sure it might have something like "typeid" or
"typeof" for doing this. It's been a few years since I did C++, I'm rusty
on stuff like this.

--
Tomás Ó hÉilidhe
 
Reply With Quote
 
Phil Endecott
Guest
Posts: n/a
 
      01-17-2008
bob the builder wrote:
> On 17 jan, 14:33, Ondra Holub <(E-Mail Removed)> wrote:
>> Note 2: Storing pointers in integer variables is bad idea. Many
>> programs are not portable to other platforms, because their author
>> supposed, that pointer has the same size as integer.


> Thank you, i didnt realize Note 2.


There's a type called intptr_t in stdint.h (and presumably cstdint) that
is an integer type guaranteed to be the same size as a pointer.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      01-17-2008
On Jan 17, 8:48 am, bob the builder <(E-Mail Removed)> wrote:
> On 17 jan, 14:33, Ondra Holub <(E-Mail Removed)> wrote:
>
>
>
> > On 17 Led, 12:44, bob the builder <(E-Mail Removed)> wrote:

>
> > > Can i get the class of an object of which i only have the address?

>
> > > Lets say i have 3 classes: AClass,BClass,CClass.
> > > i also have an object of each class: aObject, bObject, cObject
> > > And finally i have an integer: integerX.

>
> > > Now i let let intergerX be the address of one of the objects (aObject,
> > > bObject or cObject) but i dont know which.
> > > Can get the class of the object at the address contained in integerX?
> > > and how?

>
> > With some modification you can achieved what you need.

>
> > If all candidate classes have 1 common parent class, you can try to
> > dynamic_cast it to derived class type.

>
> > class Base { };
> > class AClass: public Base { };
> > class BClass: public Base { };

>
> > // ...

>
> > Base* ptr = ...; // Get the pointer from somewhere
> > AClass* aptr = dynamic_cast<AClass*>(ptr);
> > if (aptr != 0)
> > {
> > // ptr points to AClass or class derived from AClass

>
> > }

>
> > Note: If you have hierarchy of classes, you have to check child
> > classes and then their parents.

>
> > Note 2: Storing pointers in integer variables is bad idea. Many
> > programs are not portable to other platforms, because their author
> > supposed, that pointer has the same size as integer.

>
> > Note 3: In well designed project you will not need any stuff mentioned
> > above.

>
> Thank you, i didnt realize Note 2.
>
> About Note 3, it isnt really a project. i only need a dirty hack.


Why hack when you can keep it simple?
An integer can't store the adress of an object. Thats true even if on
your particular platform: a pointer happens to occupy the same size as
a pointer. On mine its not (64 bit) - not that it matters. An integer
is not an address. A pointer is not just some value/number - it also
implies a type. Would be funny if a program was to delete/deallocate
an integer instead of the complex object (can you say memory leak?).

You could employ a RTTI where typeid( *p ).name() returns the type
stored at a given pointer p. That, however assumes you've got
polymorphism (at least one virtual function). Here is an example using
boost::shared_ptr:

#include <iostream>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include "boost/shared_ptr.hpp"

class A { virtual void foo() { } };
class B : public A { };
class C : public A { };

template< typename T = A >
struct TypeName
{
void operator()(boost::shared_ptr< T >& bsp)
{
std::cout << typeid(*(bsp)).name() << std::endl;
}
};

int main()
{
typedef boost::shared_ptr< A > SharedPtrA;
std::vector< SharedPtrA > va;
va.push_back(SharedPtrA(new A));
va.push_back(SharedPtrA(new B));
va.push_back(SharedPtrA(new C));

std::for_each(va.begin(), va.end(), TypeName< >());
}


/* your output will vary
1A
1B
1C
*/

As already noted elsewhere, such a design is usually unneccessary and
often indicative of a design issue.
Hacking sucks.
The objects already know what their dynamic type is.
You might as well have each type do_the_foo() in whatever way they
do_the_foo (overide foo() where and if needed).

class A { virtual void foo() { } };
class B : public A { void foo() { } };
class C : public A { void foo() { } };


int main()
{
...
// i don't care which one of the above you might be, just
do_the_foo()
p_a->foo();
...
}

It doesn't get any simpler.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-17-2008
Tomás Ó hÉilidhe wrote:

> Virtual functions are achieved via a V-Table. That is, each object has a
> hidden pointer in it that points to a static structure that contains
> function pointers. (The Standard doesn't necessitate that this is how it's
> done, but this *is* how it's done).


Wrong. Virtual functions *may be* achieved by a vtable. Nothing in the
Standard mandates it. Anything code that assumes a vtable is by
definition non-portable.
 
Reply With Quote
 
jkherciueh@gmx.net
Guest
Posts: n/a
 
      01-17-2008
red floyd wrote:

> Tomás Ó hÉilidhe wrote:
>
>> Virtual functions are achieved via a V-Table. That is, each object has a
>> hidden pointer in it that points to a static structure that contains
>> function pointers. (The Standard doesn't necessitate that this is how
>> it's done, but this *is* how it's done).

>
> Wrong. Virtual functions *may be* achieved by a vtable. Nothing in the
> Standard mandates it. Anything code that assumes a vtable is by
> definition non-portable.


Hm, you make it sound as though the paragraph is a justification of the
initial "Wrong". However, upon closer examination, it turns out that it
isn't: Tomás Ó hÉilidhe makes it very clear that the standard does not
necessitate a v-table implementation; he just claims that this
implementation happens to be chosen by all C++ implementations. In order to
argue that he is wrong you need to name an implementation that does not use
a v-table implementation.

Also, the standard does not define "portable". In particular, should Tomás Ó
hÉilidhe be correct in the assesment that _all_ implementations use a
v-table, then code assuming that might be portable (in the obvious sense
that it works as documented/expected without change on all platforms). An
example of such code could be this:

template < typename T >
class is_polymorphic {

struct D : public T {
virtual ~D (void ) {}
};

public:

static bool const value = ( sizeof(T) == sizeof(D) );

};

It is based upon the idea that making the destructor virtual would increase
the size of a non-polymorphic class. Your claim that such code is not
portable could be empirically false (at least with regard to this instance
and according to my understanding of "portable" which might be different
from yours). All we can say a priory is that such code relies upon behavior
not determined by the standard.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments Krishna Python 4 03-07-2008 09:44 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
can't see have which have been set in the client side okaminer ASP .Net 3 08-01-2005 08:18 AM
Hi I have one web application and i want to get the number of users who are currently accessing the application. Also I want to get the user details of each user, which is stored in a database. How can I do this? Pls help. Getting No: and anu Java 11 05-12-2005 03:25 PM



Advertisments