Jump to content
  • Advertisement
Sign in to follow this  
Ruddie

Passing a class for use in an other function

This topic is 2952 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

Hi their, I'm wondering how to get my classes to be use-able in other functions without declaring these classes in the global scope. In this case, it's an array that I would like to use, but I'm not sure if that matters.

Here the small part of my code(I'm using allegro, but you don't have to know anything about it to solve the problem):




void drawTiles( ?? )/* <--- This is the function I want to use my Class in */
{
for ( int lcA= 0; lcA < SCREEN_WIDTH/20; lcA++ )
{
for ( int lcB = 0; lcB < SCREEN_HEIGHT/20; lcB++ )
{
*myTiles[lcA][lcB].draw( &*tiles, &*buffer );
}
}
}

int main()
{
/* --- variables --- */
// Integers

// Booleans

// Strings

// Classes
cPlayer myPlayer;
cTile myTiles[SCREEN_WIDTH/20][SCREEN_HEIGHT/20]; /* <<--- This is the class that I wish to pass through to the function */

if ( init() == 1 ){ return 1; }

while ( !bQuit ) // main loop
{
// Get all the input
checkKeyboardInput();
checkMouseInput();

// Draw everything to the buffer
drawTiles( ?? ); // <<--- What to place here??

// Blit the buffer to the screen
blit( buffer, screen, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
clear_bitmap( buffer );
}
deinit();

return 0;
}
END_OF_MAIN();





Thank you!

Share this post


Link to post
Share on other sites
Advertisement
Why should the function not be a part of the class itself?

In most cases where you would require to send a class to a function, it would make sense for the function to be part of the class.

Also why *myTiles[lcA][lcB].draw( &*tiles, &*buffer ); ?
and not *myTiles[lcA][lcB].draw( tiles, buffer );

EDIT: Oops, missed something.

Share this post


Link to post
Share on other sites
Quote:
Original post by elegantmoron
Why should the function not be a part of the class itself?

In most cases where you would require to send a class to a function, it would make sense for the function to be part of the class.

Also why *myTiles[lcA][lcB].draw( &*tiles, &*buffer ); ?
and not myTiles[lcA][lcB]->draw( tiles, buffer );


The buffer & tiles are in the main function scope. I can not use them otherwise.

Share this post


Link to post
Share on other sites
Well, you need to pass a 2D array to the function from what I can see, the code will turn ugly with calls to malloc and free.

Without the array part, it would have been easier, to be more precise, without the 2D array part.

A simple way to do it would be to create a 2D vector, and the do the same thing.
use:
(vector<vector<cTiles>>& argument) for the function.


[Edited by - elegantmoron on July 15, 2010 2:57:00 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by elegantmoron
Well, you need to pass a 2D array to the function from what I can see, the code will turn ugly with calls to malloc and free.

Without the array part, it would have been easier, to be more precise, without the 2D array part.


So, you recommend to get the array in the function to, or just place it in the global loop. Okay. Well, Thank you.

Share this post


Link to post
Share on other sites
Why not write a class that encompasses the 2D array, and give it the method you already have as a public method Draw()?

The whole thing then becomes Tiles.Draw() and all you have to include is Tiles.h in the central .cpp file.

Share this post


Link to post
Share on other sites
Ew, I've never really worked with vectors, and I heard that many programmers are having problem with it, but I will give it a try.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ruddie
Ew, I've never really worked with vectors, and I heard that many programmers are having problem with it, but I will give it a try.


Probably the ones that are new to the concept. But vectors work just fine :)
Since you're new to the concept of vectors you should definitely give them a try.

But using boost multidimensional array might be a better solution, since it's designed for n-dimensional arrays, whereas a std::vector<std::vector<whatever>> is merely a vector of vectors, where each of the inner vectors might have a different size.

The code from the documentation should be pretty self explanatory, but if you do you have questions, feel free to ask.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!