Page 1 of 1

Pass a class object as a parameter? (C++/SDL)

Posted: Sat Jun 25, 2011 2:49 pm
by jakobnator
Someone tried to explain this to me earlier and it didn't work out to well so if someone can show me how using my code I would like too,
Have a class object created in main, and have a function before main use its member functions.

Cake for the person that can!

Code: Select all

void option(const Card &card)
{
    if (keystates[SDLK_LEFT])
    {
        apply_surface(0,0,Table,screen);
        Card1.applyCard(100,280);
        Card2.applyCard(140,280);
        Card3.applyCard(420,50);
        apply_surface(370,513,Border,screen);
        spot = false;
    }
    else
    if (keystates[SDLK_RIGHT])
    {
        apply_surface(0,0,Table,screen);
        Card1.applyCard(100,280);
        Card2.applyCard(140,280);
        Card3.applyCard(420,50);
        apply_surface(500,513,Border,screen);
        spot = true;
    }
    else
    if (keystates[SDLK_RETURN])
    {
        if (spot == false)
        {
            pos = false;
        }
        else
        if (spot = true)
        {
            pos = true;
        }
    }
}

int main ( int argc, char *args[] )
{
    srand(time(0));
    bool quit = false;
    if( init() == false ) { return 1;}
    if( load_files() == false ) { return 2;}
    apply_surface( 0,0,Table,screen);
    apply_surface(370,513,Border,screen);
    Card Card1;
    Card Card2;
    Card Card3;
    Card1.applyCard(100,280);
    Card2.applyCard(140,280);
    Card3.applyCard(420,50);
    while (quit == false)
    {
        while ( SDL_PollEvent( &event ) )
            {
                if( event.type == SDL_QUIT )
                {
                    quit = true;
                }
            }
        SDL_Flip( screen );
    }
    cleanup();
    return 0;
}

Re: Pass a class object as a parameter? (C++/SDL)

Posted: Sat Jun 25, 2011 3:09 pm
by bnpph
References are like pointers, except they use a different syntax.

void option(Card &card); can do the same things that void option(Card *card) can.
A const reference is read-only. If you want to write, you generally use pointers instead, although references work too.

Re: Pass a class object as a parameter? (C++/SDL)

Posted: Sat Jun 25, 2011 3:31 pm
by jakobnator
so, this would be correct?
I am confused were the name of the object is supposed to go, and is using a member function just reading?

Code: Select all

void option(const Card &Card1)
{
    if (keystates[SDLK_LEFT])
    {
        apply_surface(0,0,Table,screen);
        Card1.applyCard(100,280);
        Card2.applyCard(140,280);
        Card3.applyCard(420,50);
        apply_surface(370,513,Border,screen);
        spot = false;
    }
    else
    if (keystates[SDLK_RIGHT])
    {
        apply_surface(0,0,Table,screen);
        Card1.applyCard(100,280);
        Card2.applyCard(140,280);
        Card3.applyCard(420,50);
        apply_surface(500,513,Border,screen);
        spot = true;
    }
    else
    if (keystates[SDLK_RETURN])
    {
        if (spot == false)
        {
            pos = false;
        }
        else
        if (spot = true)
        {
            pos = true;
        }
    }
}

int main ( int argc, char *args[] )
{
    srand(time(0));
    bool quit = false;
    if( init() == false ) { return 1;}
    if( load_files() == false ) { return 2;}
    apply_surface( 0,0,Table,screen);
    apply_surface(370,513,Border,screen);
    Card Card1;
    Card Card2;
    Card Card3;
    Card1.applyCard(100,280);
    Card2.applyCard(140,280);
    Card3.applyCard(420,50);
    while (quit == false)
    {
        while ( SDL_PollEvent( &event ) )
            {
                if( event.type == SDL_QUIT )
                {
                    quit = true;
                }
            }
        SDL_Flip( screen );
    }
    cleanup();
    return 0;
}

Re: Pass a class object as a parameter? (C++/SDL)

Posted: Sat Jun 25, 2011 3:55 pm
by bnpph
Using a member function is read-only if it has const after it:
void method() const {}
(I forget the term for this)

This is how you could use references:

Code: Select all

void foo(int& x) {
  x += 5;
}
int main() {
  int var = 10;
  foo(var);
  //var is now 15
}
so, this would be correct?
That wouldn't work. This should: (why do you have so many global scoped variables?)

Code: Select all

void option(Card &Card1, Card &Card2, Card &Card3)

Re: Pass a class object as a parameter? (C++/SDL)

Posted: Sat Jun 25, 2011 9:22 pm
by jakobnator
Ok thnx it worked, so a class is treated like the type of variable when referencing things? (int,char,bool)

Re: Pass a class object as a parameter? (C++/SDL)

Posted: Sat Jun 25, 2011 9:42 pm
by Ginto8
jakobnator wrote:Ok thnx it worked, so a class is treated like the type of variable when referencing things? (int,char,bool)
That's actually exactly what it is. Classes and structs are a way of defining your own types. There are some more complicated interactions between them, but in essence it's just adding more data types to your program.

Re: Pass a class object as a parameter? (C++/SDL)

Posted: Sun Jun 26, 2011 5:10 am
by MrDeathNote
Ginto8 wrote:
jakobnator wrote:Ok thnx it worked, so a class is treated like the type of variable when referencing things? (int,char,bool)
That's actually exactly what it is. Classes and structs are a way of defining your own types. There are some more complicated interactions between them, but in essence it's just adding more data types to your program.
There's actually one of my professors who refuses to call them classes and will only call them user-defined types.