Sign in to follow this  

How deep can you nest functions in c++

This topic is 1201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I nested a lot of functions, while I'm not lost with what I did, I am confused about why the last nested function is giving me problems with the function arguments. Here is some code from the second nested function to the last in spefic order.

 

bool touches_wall( SDL_Rect box, SDL_Rect ys, Tile *tiles[], SideTile *walls[] )

 

2#

In this function I call  check_collision_region

 bool touches_wall( SDL_Rect box, SDL_Rect ys, Tile *tiles[], SideTile *walls[] ) 
 { 
	int count = 0;
	//Go through the tiles 
	for( int t = 0; t < TOTAL_TILES; t++ ) 
	{ 
		//If the tile is a wall type tile 
		if( ( tiles[ t ]->get_type() >= TILE_CENTER ) && ( tiles[ t ]->get_type() <= TILE_TOPLEFT ) ) 
		{ 
			//If the collision box touches the wall tile 
			if( check_collision( box, tiles[ t ]->get_box() ) == true ) 
			{ 
				 return true; 
			}  
		} 
	} 

	for( int s = 0; s < TOTAL_TILES_WALL; s++ ) 
	{ 
		//If the tile is a wall type tile 
		if(  walls[ s ]->get_type() == TILE_RED_WALLs ) 
		{ 
			//If the collision box touches the wall tile 
			if( check_collision_region( box, walls[s]->get_base_box(), ys, walls[s]->get_box() ) == true ) 
			{ 	
					return true;
			}  
		} 
	} 

 //If no wall tiles were touched 
 return false; 
 }

3# in this function I call check_collision_topside

bool check_collision_region(SDL_Rect A, SDL_Rect B, SDL_Rect C, SDL_Rect D )
 {
	 //The sides of the rectangles 
	int leftA, leftB, leftC, leftD; 
	int rightA, rightB, rightC, rightD; 
	int topA, topB, topC, topD; 
	int bottomA, bottomB, bottomC, bottomD; 

	bool regionTop;
	bool regionBottom;
	bool regionNone;

	//Calculate the sides of rect A 
	leftA = A.x; 
	rightA = A.x + A.w; 
	topA = A.y; 
	bottomA = A.y + A.h; 

	//Calculate the sides of rect B 
	leftB = B.x; 
	rightB = B.x + B.w; 
	topB = B.y; 
	bottomB = B.y + B.x;

	//Calculate the sides of rect C 
	leftC = C.x; 
	rightC = C.x + C.w; 
	topC = C.y; 
	bottomC = C.y + C.h; 

	//Calculate the sides of rect D 
	leftD = D.x; 
	rightD = D.x + D.w; 
	topD = D.y; 
	bottomD = D.y + D.x;
	

	//Test
	 //If any of the sides from A are inside of B 
	if(( bottomA >= topB ) && ( bottomA <= topB + TILE_WALLs_HEIGHT/2 ) && (rightA >= leftB) &&(leftA <= rightB)) 
	{ 
				
		if (check_collision_topside(  SDL_Rect A, SDL_Rect B, SDL_Rect C, SDL_Rect D) == true) 
		{
			return true;
		}
		
	} 

	//Second function 
/*	if(( bottomA <= bottomB) && (bottomA >= bottomB - TILE_WALLs_HEIGHT/2 ) )
	{ 
		if((rightA >= leftB) &&(leftA <= rightB))
		{		
			//regionBottom = true; 
			if (check_collision_bottomside( SDL_Rect A, SDL_RECT B, ,SDL_Rect C, SDL_Rect D) == true)
			{
				return true
			}
		}
	} */





	return false;
 }

4# I haven't really worked on the forth yet it's kinda generic but I will fine tune it if I can get the 3# to work.

bool check_collision_topside( SDL_Rect A, SDL_Rect B, SDL_Rect C, SDL_Rect D) 
 { 
	//The sides of the rectangles 
	int leftA, leftB, leftC, leftD; 
	int rightA, rightB, rightC, rightD; 
	int topA, topB, topC, topD; 
	int bottomA, bottomB, bottomC, bottomD; 


	int top;


	//Calculate the sides of rect A 
	leftA = A.x; 
	rightA = A.x + A.w; 
	topA = A.y; 
	bottomA = A.y + A.h; 

	//Calculate the sides of rect B 
	leftB = B.x; 
	rightB = B.x + B.w; 
	topB = B.y; 
	bottomB = B.y + B.h; 

	//Calculate the sides of rect C 
	leftC = C.x; 
	rightC = C.x + C.w; 
	topC = C.y; 
	bottomC = C.y + C.h; 

	//Calculate the sides of rect D 
	leftD = D.x; 
	rightD = D.x + D.w; 
	topD = D.y; 
	bottomD = D.y + D.x;


	//the top collision only
	top =  bottomA - topB; 

    //If any of the sides from A are outside of B 
	if( bottomC <= topB + top  ) 
	{ 
	
		return false; 
	} 

	if( bottomC >= topB + TILE_WALLs_HEIGHT/2 ) 
	{ 
		return false; 
	} 

	if( rightA <= leftB ) 
	{ 
		return false; 
	} 

	if( leftA >= rightB ) 
	{ 
		return false; 
	} 

	//If none of the sides from A are outside B 
	return true; 
 } 
Edited by LAURENT*

Share this post


Link to post
Share on other sites

KulSeran is right, you're having compilation errors. But apart from that, why are you using 4 rects in each function? Collision detection is usually done betwen 2 objects. It looks like you don't use D, and you only use of C looks really weird:

//If any of the sides from A are outside of B 
	if( bottomC <= topB + top  )

What does the condition have to do with the comment?

 

Also, you do:

//the top collision only
	top =  bottomA - topB; 

and immediatelly after it you use "top" this way:

    //If any of the sides from A are outside of B 
	if( bottomC <= topB + top  ) 

That condition is the same as "bottomC <= bottomA" (topB + top = topB + (bottomA - topB) = bottomA).

 

So, even with nested functions not being a problem here, are you sure you need 3 nested functions to do a collision check?

Share this post


Link to post
Share on other sites

I tried to provoke Xcode, just for fun.  Note that this is a limit imposed by the compiler, not necessarily the standard itself.

 

The limit is 256 levels, 255 selected ones plus the one used for the method itself:

 

nestinglevel.png

 

Edit:

 

Seems like there even is a command line option to allow for more brackets, so it is definitively not a restriction in the standard:

 

-fbracket-depth=N
Sets the limit for nested parentheses, brackets, and braces to N. The default is 256.
Edited by aregee

Share this post


Link to post
Share on other sites

Right, there was something I wanted to say in my last post and you made me remember it.

 

Nesting is what aregee did, blocks inside blocks (like chained "if" statements). Calling functions from another functions (what the OP did) is not nesting and it's not limited by the bracket depth, otherwise you'll be limited in your app by the call hierarchy of any external library you're using, or using recursive functions won't work well.

Edited by DiegoSLTS

Share this post


Link to post
Share on other sites

Nesting is what aregee did, blocks inside blocks (like chained "if" statements). Calling functions from another functions (what the OP did) is not nesting and it's not limited by the bracket depth, otherwise you'll be limited in your app by the call hierarchy of any external library you're using, or using recursive functions won't work well.

 
It's also worth noting that nesting within a function is not the same thing as a nested function.  Neither standard C or C++ allow nested functions (though gcc does have an extension to allow nested functions).
 
edit: I should give an example.
 
This is nesting within a function:
 



int foo() {
 
  int x = 5;
  {
    x++;
    {
      x++;
    }
  }
  x++;
 
 return x;
 
}

 
This is a nested function:
 



int foo() {
 
  int bar() {
    int x = 5;
    x++;
 
    return x;
  }
 
  return bar();
 
}
Edited by LennyLen

Share this post


Link to post
Share on other sites

Alright thank you all I see my error with the function argument.

 

 

Also for any questions regarding the collision detection. It's is somewhat unique but not a real complex concept. To summarize it my collision detection require one extra step before collision is detected.

 

 

Edited: Now that I knew there wasn't a problem with nesting I turned my attention to other potential problems which allowed me to figure out what I was doing wrong. Thanks guys

Edited by LAURENT*

Share this post


Link to post
Share on other sites

This topic is 1201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this