-
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! :D
-
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.
-
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.
-
Ok, the thing i dont understand is how would u change a passing object by accident?
Thanks for any help
-
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.
-
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?
-
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.
-
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.
-
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.
-
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.
-
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 :)
-
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.
-
!!!
Ok now im getting the idea, thanks for the help!