passing objects

This is a discussion on passing objects within the C++ Programming forums, part of the General Programming Boards category; Hi im fairly new to oo programming, and ive started working on a little isometric game in c++ using sdl. ...

  1. #1
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9

    passing objects

    Hi im fairly new to oo programming, and ive started working on a little isometric game in c++ using sdl. i have a question about the alternatives to using global variables. heres my problem.

    I have a class screen which sets up the video for my game.
    i have a class sprite, for each image that is drawn to the screen.
    i have a class map which contains tile objects. each of these contains a sprite, of the tile type.

    i then also have a player class which will contain a sprite.

    to draw a sprite i have to get a surface from the screen object.

    so in order to do this i could either make my screen object global, and available to both the player class and the tile class, or pass a pointer to the screen object to the map then to each tile, and also pass it to the player class.

    would passing lots of pointer around have any effect on the speed of the code?

    is there an easier way to do this? would making my screen object global be more suited than passing it to one class then to another?

    does anyone have any good links describing the kind of thing im doing? ive come across this before in java, and have always worked around it, and not been very OO in the process. but id like to try and be as OO as possible with this.

    cheers for absolutely any help. thanks rob.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    As far as I know passing pointers to objects is fine. It might be better to pass a reference... I don't know.

    When I wrote a program that passed pointers-to-objects, it seemed too complicated... like "I must be doing something wrong..." But, when I think of an object as a "smart structure", it doesn't seem as strange. I wouldn't find anything strange about passing around pointers-to-structures.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    would making my screen object global be more suited than passing it to one class then to another?
    More experienced people than me can(and will) comment further, but I don't think having global variables is a good approach for most situations. The only example my C++ book gives for using global variables is when you want to ensure a constant is the same across all the files in your program.

    Passing pointers or references to your objects sounds like a much better approach. You might also consider having a <vector> as a member variable in one or more of your classes. A <vector> is just like an array but it is easier to deal with, and it has more features. For instance, it can automatically expand it's size as you add elements. Maybe a Sprite <vector> would be appropriate for your Screen class. Then, you could access the Sprite objects directly using a Screen class object.
    Last edited by 7stud; 03-04-2005 at 07:36 PM.

  4. #4
    h4x0r1ng t3h m41nfr4m3!!1 r0bbb's Avatar
    Join Date
    Mar 2005
    Location
    West London
    Posts
    9
    cheers for your help guys, thats some very useful information.

    thanks.

    rob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing local objects by reference
    By manav in forum C++ Programming
    Replies: 15
    Last Post: 03-31-2008, 07:32 AM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  3. Passing objects
    By xshapirox in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2004, 11:34 AM
  4. Passing Objects using menus
    By TankCDR in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2003, 10:11 AM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21