Skip to main content
GameDev.net gamedev.net
🔒 Locked

void pointer to class problem

Started by uncutno May 25, 2002 at 10:12 AM 3 replies 150+ views
Original Post
uncutno
uncutno

    

//class

class CA{
public:
    //variables and func, not so inportant

    float x,y,z;
    void set(float xx,float yy,float zz){
        x=xx;y=yy;z=zz;
    };
};

//code

CA list[1000];
void *pA;
pA = &A


//now the question is!

//how do you accses A[x] trough pA?



(CA*)pA[5].set(1,2,3);//?

pA(CA*)[5].set(1,2,3);//?


//if know i can store the size of CA somewhere, and then create 

//pA as a char buffer, but then i can only accses the data,not 

//the funcs... i also know that i can do:


CA *temp_pA = (CA*)pA
temp_pA[5].set(1,2,3);

  
my question is, is it posible to accses a function in a class trough a void pointer? [edited by - uncutno on May 25, 2002 11:14:04 AM]
-Anders-Oredsson-Norway-
fallenang3l
fallenang3l
AFAIK, no. You have to cast the pointer to a known type before you try to dereference it with * or [].
uncutno
uncutno
thats to bad... :-)
you see, i will do it about 8000 times each second, so i think creating a new variable ewery time i call the func is bad...
maybe i redesign this part, its not many lines of code..

thanks...
-Anders-Oredsson-Norway-
Conner McCloud
Conner McCloud
I may be misunderstanding something, but all you have to do is make the ''void *pA'' into ''CA *pA'', and you''ll be fine. It won''t take any additional time, because defining a pointer to a class doesn''t create an actual instance of the class. In other words, no constructor is called, just memory for one pointer set aside. And that''ll happen with a void pointer anyhow, so there''s absolutely zero performance difference.

To answer your first question, though, you''d do it like this if you''re determained not to use a CA* variable:


  
CA A[1000]; //I assume this is what you meant to do, since you never mention list again in your code

void *pA = &A
((CA*)pA)[5].set(1,2,3);
(CA)(pA[5]).set(1,2,3);


Either of those methods should work, and in both cases there''s an extra set of parenthesis that may not be neccessary. I just never remember the precedence rules, and when in doubt I add extra parenthesis. There''s also reinterpret_cast, which I believe is the prefered method for casting pointers, but I don''t recall off hand how to use it. I think it''s ''reinterpret_cast(pA)[5].set(1,2,3)'', but don''t quote me. And now that I think about it, I may be misspelling reinterpret anyhow.

However, chances are you don''t need that void pointer at all. The only time it would be neccessary is if you don''t know the type in advance, in which case you shouldn''t be casting it to some random type anyhow. You might be able to get it to compile doint that, but it could get rather messy.

CM
declspec
declspec
casting it wont change the run time well compared to screwing round with looking up offsets prolly decrease it.


see ok. the thing is you ever wonder how a function in a class gets a "this". your first idea is that every instance of a class has a memory block that is a function. naw. further there isnt a look up cost unless its virtual. thats the virtual functions use vtables with are just offsets at the front of the instance. but if its not virtual the offset of the call is hard coded into the resultant assembly. the call offset is one operation. no look up.

"this" is the first (maybe last parameter past to the function.

for example if its
class coo
{
void foo (int i);
}

foo is actually called like
void foo (coo* c, int i);

whats the relavence. its to this:
"so i think creating a new variable ewery time i call the func is bad..."
casting like

void* p;
(Foo*)p->foo_function();

doesnt create a variable other then that void pointer. that just tells the compilier to use the offset for foo_function and call it like
push p
call offset foo_function

in effect atleast. thats all. nothing is being created in a cast is the point. its a compile time expression thats just communicating with the brains in the compilier. laugh (laugh at "brains" i mean)








[edited by - declspec on May 25, 2002 3:50:25 PM]

Topic Locked

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

Sign in to reply to this topic.