Pointer shortcuts
I was wondering if using a "short worded" pointer, point to a "huge worded" variable slower than just using the variable for example. Also, would that be considered bad programming practices?
I would hate to write this over and over again and getting syntax errors
factory[0]->model.make.type=5;
this is much easier, but is it faster?
ANYOBJECT *point = &factory[0]->model.make.type;
point = 5;
A good optimizing compiler will probably turn both versions into the same machine code. Do whichever one you think will make your code easier to understand.
What you want to do is sort of possible, but you may run into issues with type declarations. For example:
That would work yes, but only on type and color. Because shortcut is a integer pointer, it wont work on radius, and its up in the air if it''d work with the FlavorT enumeration.
struct Binkey { int type; int color; float radius; enum FlavorT flavor;};struct Binkey * mybinkey = new struct Binkey;int * shortcut = &(myBinkey->type);*shortcut = 1;
That would work yes, but only on type and color. Because shortcut is a integer pointer, it wont work on radius, and its up in the air if it''d work with the FlavorT enumeration.
If this is C++, I sometimes use references when my lines are way too long for similar issues as this.
int Agony() { return *((int*)0); } Mwahaha... >8)
void SomeFunction(CSomeClass Instance){ float& rX = Instance.pMember->Variables.pLocation->x; //Which is basically identical to float* pX = &(Instance.pMember->Variables.pLocation->x); //But the reference looks nicer, since you //don''t have to dereference it all the time.}
Now I''m not an expert on this stuff. I don''t know how much lines like Instance.pMember->Variables.pLocation->x slow down the program, but I am pretty sure that using a single reference or pointer shouldn''t hurt your performance, and it might make it better. Either way, it makes your code look nicer, and thus increases readability and reduces the chance of making stupid bugs or syntax errors. So I''d fully recommend it. int Agony() { return *((int*)0); } Mwahaha... >8)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement