Results 1 to 13 of 13

Thread: TypeDef and Passing Objects

  1. #1

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Talking

    Hey sup all
    I was wondering, what is the point of typedef. I read about it, but I dont understand the point of it.
    What so useful about it?

    And the other thing was, passing objects as parameters.
    ...how should u pass objects as parameters, call by value, reference, or constant... or are there situations where each is useful?

    I dont think as passing them as constant makes sense, but I read about it in a book which im suppose to be reading from in my class.

    Any help or ideas would be appreciated,
    thanks!
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It allows you to simplify things, for example if you use function pointers, you can use:
    Code:
    typedef void (__stdcall *PFNPROGRESS)(long)
    In this case, it creates the type PFNPROGRESS to point to a function with prototype:
    Code:
    void __stdcall funcname(long)
    Normally when passing objects it's a good idea to use reference or pointer, as this avoids a redundant copy.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Not only does it avoid a redundant copy, but if you're not careful with constructors you can get into an infinite recursion... and that's not good

    Passing objects as constant references is just good practice if you don't want to change the object. It helps prevent side-effect errors where you might change an object by accident.
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Talking

    Ok, the thing i dont understand is how would u change a passing object by accident?

    Thanks for any help
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh I see, everything you code works exactly as you planned does it?

    I didn't say it was essential, I said it was good programming practice, there is a difference. If you don't want to do it then fine, don't but it costs nothing in terms of performance and could save you some hard-to-trace bugs if it doesn't work as you expected.
    Harry.

    "From one thing, know ten thousand things."

  6. #6

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Unhappy example

    can you please specify an example because i can't see how my code would be modified by accident.

    how can i change a class that i'm passing to a function by accident?
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    No, I'm not going to waste my time arguing with you about this, if you want to go and write sloppy code then that's your lookout. Most of the time it will work fine, but why take the chance? If you aren't willing to take up an opportunity to prevent errors with no performance hit and with an extra effort of one word then that's fine, but you'll feel like an idiot when you do make a mistake.

    Evidently, though, you never make mistakes, and all your code works just how you planned.
    Harry.

    "From one thing, know ten thousand things."

  8. #8

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547
    Yo man, i know what ur saying dont have to spaz out
    Im just wondering thats all. Because it doesn't make sense to use something that dosent need to be used.

    Im not traying to start an argument, i just really want a reall example because i cant think of one, AND dont understand this.
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

  9. #9
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    i can see where those types of things are useful. I know that these are two completly different things, but an example of something that seems like a useless nusance at first comes to be useful. Like OPTION EXPLICIT in ASP. It forces you to declare all of your variables. Basically useless for a 10 or 20 line program, but when it starts coming to the 1000's of lines, stuff like that comes in very useful. Unless, as Harry says, you write perfect code the first time, or have your code comletely memorized or something like that.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  10. #10
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    and, if you want help with your code, or want to show it to people and they need to edit it, those types of things make it wasier for them.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  11. #11
    New Member
    Join Date
    Mar 2001
    Posts
    3
    i'm new to c++ and i was wondering if someone could perhaps post a small example of an instance where you would edit a class by accident. what would the code look like?

    would you be accidentally modifying it with your own code?

    thank you

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Okay, here's another reason. It helps the compiler optimise your code. It knows that that object cannot be changed through code since it's passed as const, so it can cache it and speed things up.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Fanatic Member invitro's Avatar
    Join Date
    Jan 2000
    Location
    Outside your window
    Posts
    547

    Thumbs up !!!

    Ok now im getting the idea, thanks for the help!
    ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet?

Posting Permissions

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



Click Here to Expand Forum to Full Width