Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: classes in c?

                  
   
  1. #11
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    psp Well...

    I sorta figured something out...

    typedef struct clsPoint{
    int X, Y;
    } Point;

    Point createPoint(int a, int b){
    Point p;
    p.X = a;
    p.Y = b;
    return (p);
    }

    Point pn = createPoint(10,15);

    I need to know 2 other things now though...

    1. Is there a more efficient way to do what I did above?
    2. How to make a function that I can use like this:

    pn.Move(2, 5); // Moves the point over 2 and down 5.

    Could you help me one more time please. Sorry I ask so many questions.

  2. #12
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    1. Instead of:
    Point ThisPoint = createPoint( 1, 1 );

    you do:
    Point ThisPoint = { 1, 1 };
    // That *should* work. If not:
    Point ThisPoint;
    ThisPoint.X = 1;
    ThisPoint.Y = 1;
    // That is far more 'efficent'

    2. You can't, that's a class. The closest you can do is:
    void MovePoint( Point * pPoint, int MoveX, int MoveY )
    {
    if( pPoint != NULL )
    {
    pPoint->X += MoveX;
    pPoint->Y += MoveY;
    }
    }

    // Usage
    Point ThisPoint = { 1, 1 };
    MovePoint( &ThisPoint, 10, 15 );

    3. For god's sake, just use classes.

  3. #13
    DCEmu Newbie CT_Bolt's Avatar
    Join Date
    Mar 2006
    Location
    Somewhere in Space
    Posts
    39
    Rep Power
    0

    Thumbs up OK... thank you.

    1 & 2:
    Well... I thought that might be what I would have to do... I just wanted to know if there were better ways to do this in 'c'

    3. You can't in 'c' but, I would much prefer that.

    OK Well THANK YOU for your help!

  4. #14
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    Why are you using C over C++ anyway?

  5. #15
    Dream Coder
    Join Date
    Apr 2004
    Location
    Miami, FL
    Age
    38
    Posts
    4,675
    Rep Power
    50

    Default

    Just as a point of interest, I think it's always a good idea to relearn how to do things without the programming language's help. This is a relatively poor example, but there are places where you would rather use structs than classes and C over C++.

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •