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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

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

Post 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;
}
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

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

Post 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.
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

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

Post 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;
}
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

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

Post 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)
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

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

Post by jakobnator »

Ok thnx it worked, so a class is treated like the type of variable when referencing things? (int,char,bool)
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

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

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply