My First Project - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20) +---- Thread: My First Project (/showthread.php?tid=2796) Pages:
1
2
|
My First Project - nevets04 - 11-10-2009 Code: #include <iostream> RE: My First Project - Codine - 11-11-2009 Not bad for your first project. Now try something diffrent, or find ways of making your code better. RE: My First Project - Extasey - 11-11-2009 Niice, good work! RE: My First Project - se7en - 11-11-2009 yeah it is good! RE: My First Project - MrD. - 11-11-2009 (11-10-2009, 11:16 PM)nevets04 Wrote: Just a few points I will make about this which are good habits to get into. 1) Pass parameters that aren't going to be changed in the function as "const", pass complex data types (such as classes, structs, etc) as a const reference (or const pointer if you really must). The "const" part of this will allow the compiler to optimise better since you are telling it that this parameter won't be changed, the reference/pointer part will save copying the complex type (which pass by value would have done). Example: Code: int addition(const int a, const int b) And for a complex data type: Code: int printLine(const std::string &str) 2) You creating a temporary variable (which is using it's default constructor), you are then assigning to that variable (using the assignment operator), before returning by value (which will take yet another copy). All in all that int is having it's value set twice, and copied once; this can be done cheaper. Firstly, you could always use the copy-constructor of the int. (Be careful of this when using complex data types as the copy-constructor might not have been defined properly). This will save the initial defaulting of the value: Code: int addition(const int a, const int b) However you are still creating a temporary instance of the int to just return it by value, so the most optimal way would be to just return the sum of the two numbers: Code: int addition(const int a, const int b) These are trivial points for such a small application, but for larger applications which are performance critical they all start to add up. You might want to read GotW. RE: My First Project - uber1337 - 03-30-2010 Why didn't you just do: Code: int add (int a, int b) RE: My First Project - nevets04 - 03-31-2010 (03-30-2010, 04:54 PM)uber1337 Wrote: Why didn't you just do: This was so long ago, I have no idea what I was thinking. RE: My First Project - uber1337 - 04-02-2010 (03-31-2010, 11:45 PM)nevets04 Wrote: This was so long ago, I have no idea what I was thinking.Heh yeah now I see where you got that code from. You should really cite your sources RE: My First Project - nevets04 - 04-02-2010 (04-02-2010, 03:23 PM)uber1337 Wrote: Heh yeah now I see where you got that code from. You should really cite your sources Unless I take an entire program, I'm not going to cite sources in most cases. Do you site python doc pages? RE: My First Project - Nyx- - 04-05-2010 (11-11-2009, 07:28 AM)MrD. Wrote: Just a few points I will make about this which are good habits to get into. wow, im learning C++ right now and i found this post really helpful, its good to see people giving really decent pointers/tips ^^ |