Passing a class for use in an other function

Started by
7 comments, last by SiS-Shadowman 13 years, 9 months ago
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!
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.
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.
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]
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.
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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

I had edited the post, so :P

Use 2D vectors, that should help.
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.
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.

This topic is closed to new replies.

Advertisement