C++ questions

Posts 1–12 of 12 · Page 1 of 1
C++ questions
I am converting a code from c++ to c#.

I am stuck here:

m_pChildren[nX * 2 + nZ];

seems simple? Well maybe for you. m_pChildren is a pointer to a class. What confuses me is that it is NOT an array of objects, it is an object it self. How come that works?
And later on the code it says this:

m_pChildren = pNodes[nOffset];

pNodes actually being a list of the same class that m_pChildren is. This part actually makes sense but the part above is complete gibberish for me.
If m_pChildren is actually defined as:

Code:
Object** m_pChildren
Then the [] operator operates as a pointer array such that:

Code:
for( i = 0; i < numObjects; i++ )
   ptr = m_pChildren + ( sizeof( PVOID ) * i );
However, if m_pChildren is defined as an array:

Code:
Object m_pChildren[ 100 ];
The [] operator takes a different approach such that:

Code:
for( i = 0; i < 100; i ++ )
   ptr = ADDRESS_OF( m_pChildren ) + ( sizeof( Object ) * i );
Well at least, that's what I assume you're looking for since you didn't exactly give the clearest of information...
Quote Originally Posted by Hitokiri~ View Post
If m_pChildren is actually defined as:

Code:
Object** m_pChildren
Then the [] operator operates as a pointer array such that:

Code:
for( i = 0; i < numObjects; i++ )
   ptr = m_pChildren + ( sizeof( PVOID ) * i );
However, if m_pChildren is defined as an array:

Code:
Object m_pChildren[ 100 ];
The [] operator takes a different approach such that:

Code:
for( i = 0; i < 100; i ++ )
   ptr = ADDRESS_OF( m_pChildren ) + ( sizeof( Object ) * i );
Well at least, that's what I assume you're looking for since you didn't exactly give the clearest of information...
Yep that is it. So why would they do that? I mean I don't get it, by accessing the memory ahead like that wouldn't it return something absurd and random? Since it is not a list it would end after the first bytes. Now that i think of it, maybe they allocate memory and then just fill the space? Cause if that is the case then i fully understand the code now.
You should research memory models a bit more. It's as you said but "they" don't do that but rather how objects are laid out in memory.
Quote Originally Posted by Hitokiri~ View Post
You should research memory models a bit more. It's as you said but "they" don't do that but rather how objects are laid out in memory.
One more thing, how does this loop work?

for (; nSkyPortalCount; --nSkyPortalCount)
Quote Originally Posted by BlenMiner View Post
One more thing, how does this loop work?

for (; nSkyPortalCount; --nSkyPortalCount)
It's the same thing as

Code:
while (nSkyPortalCount > 0) {
        ; do stuff
        --nSkyPortalCount ;
}
Quote Originally Posted by InunoTaishou View Post
It's the same thing as

Code:
while (nSkyPortalCount > 0) {
        ; do stuff
        --nSkyPortalCount ;
}
The closer equivalent would be:

Code:
while (nSkyPortalCount)
{
      --nSkyPortal;
}
@BlenMiner

It iterates through something you haven't told us about by decrementing the nSkyPortalCount variable. It probably uses it like :

Code:
for(; nSkyPortal; --nSkyPortal)
{
	unk[nSkyPortal] = ?;
}
Edit: Yeah, just a loop backwards. Didn't read your post properly.
Quote Originally Posted by ~BlackMaster~ View Post
The closer equivalent would be:

Code:
while (nSkyPortalCount)
{
      --nSkyPortal;
}
That would imply that nSkyPortal can be a negative value, in which case it would be an infinite loop unless the user, somewhere in the code, decides to change nSkyPortal to a positive value or break; the loop

Code:
int nSkyPortal = -42;
while (nSkyPortal ) {
    // do stuff
   --nSkyPortal;
    // -42 is just going to increase
}
Quote Originally Posted by InunoTaishou View Post
That would imply that nSkyPortal can be a negative value, in which case it would be an infinite loop unless the user, somewhere in the code, decides to change nSkyPortal to a positive value or break; the loop

Code:
int nSkyPortal = -42;
while (nSkyPortal ) {
    // do stuff
   --nSkyPortal;
    // -42 is just going to increase
}
Judging from his code you would expect nSkyPortal to always be greater than 0 (until of course the decrement operator takes it down to 0 which will cause the loop to exit). I just gave you the direct translation as the second 'parameter' of the for loop is a "while true" statement and it tests whether it's valid (not zero) and not whether it's greater than 0.
Quote Originally Posted by BlenMiner View Post
I am converting a code from c++ to c#.

I am stuck here:

m_pChildren[nX * 2 + nZ];

seems simple? Well maybe for you. m_pChildren is a pointer to a class. What confuses me is that it is NOT an array of objects, it is an object it self. How come that works?
And later on the code it says this:

m_pChildren = pNodes[nOffset];

pNodes actually being a list of the same class that m_pChildren is. This part actually makes sense but the part above is complete gibberish for me.
have you updated your C++ programing program?
Quote Originally Posted by XXX_g3b4n_XXX View Post
have you updated your C++ programing program?
Does it matter? The project i am working with is from around 2003. So yeah, considering i am using visual studio 2013
So it just does the loop backwards?
Posts 1–12 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us