Syntax problem
-
This syntax with all those brackets is scaring me.... Can someone please tell me what is going on?
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; }
Thank You! :)Is casting whatever there is inside ms_instances array to a Ball object pointer, and then is refering to that object that is beign pointed, you can resume it like this: Ball b; b.m_stateTimeInterval = timeInterbal; Obviously dont replace the code, is just for you to see the same in a more simpler way Hope it helps
-
This syntax with all those brackets is scaring me.... Can someone please tell me what is going on?
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; }
Thank You! :)It could also be written as:
for (...)
{
Ball *pBall = ms_instances[j];
( *pBall ).m_stateTimeInterval = timeInterval;
// or
pBall->m_stateTimeInterval = timeInterval;
}
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
This syntax with all those brackets is scaring me.... Can someone please tell me what is going on?
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; }
Thank You! :)I would prefer this:
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { Ball * const ball = ms_instances[j]; ball->m_stateTimeInterval = timeInterval; }
Now it seems much clearer, I think. The
ms_instances
array contains pointers toBall
objects. This loop initializes them_stateTimeInterval
field of all ofBall
objects. -
This syntax with all those brackets is scaring me.... Can someone please tell me what is going on?
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; }
Thank You! :)ms_instances is apparently an array of pointers to instances of some kind of object. this code gets element 'j' (a pointer) out of the array, then casts that pointer to a pointer-to-a-Ball: "compiler, treat this pointer as if it was a pointer to a Ball, instead of a pointer to whatever it is actually pointing at. then it dereferences the pointer and sets the m_state* member variable of what we all hope is an actual Ball object.
-
This syntax with all those brackets is scaring me.... Can someone please tell me what is going on?
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { ( *( (Ball*)(ms_instances[j]) ) ).m_stateTimeInterval = timeInterval; }
Thank You! :)It is nothing more than the equivalent of doing the following:
for (unsigned int j = 0; j < ms_numInstances; j++) { Ball* pBall = (Ball*)ms_instances[j]; pBall->m_stateTimeInterval = timeInterval; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
I would prefer this:
for ( unsigned int j = 0; j < ms_numInstances; j++ ) { Ball * const ball = ms_instances[j]; ball->m_stateTimeInterval = timeInterval; }
Now it seems much clearer, I think. The
ms_instances
array contains pointers toBall
objects. This loop initializes them_stateTimeInterval
field of all ofBall
objects.Viorel. wrote:
The ms_instances array contains pointers to Ball objects
What happens when j = 0;
Ball * const ball = ms_instance[0];
Does this mean it has no pointers because ms_instance[0] ? Do you need atleast j = 1 to create a pointer ........ like ms_instance[1] means 1 pointer .... ms_instance[2] means array of two pointers......... so what does 0 mean?ball->m_stateTimeInterval = timeInterval;
-------------------------------------------------------------------------------------------- So for each j value, there will be an array of pointers created? -
Viorel. wrote:
The ms_instances array contains pointers to Ball objects
What happens when j = 0;
Ball * const ball = ms_instance[0];
Does this mean it has no pointers because ms_instance[0] ? Do you need atleast j = 1 to create a pointer ........ like ms_instance[1] means 1 pointer .... ms_instance[2] means array of two pointers......... so what does 0 mean?ball->m_stateTimeInterval = timeInterval;
-------------------------------------------------------------------------------------------- So for each j value, there will be an array of pointers created?Jay03 wrote:
Ball * const ball = ms_instance[0];
Arrays in C/C++ are 0-based. This line will access the first element in the ms_instance array and cast it to a pointer-to-a-Ball.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac