Skip to main content
gamedev.net
🔒 Locked

Strict C++ coding

Started by caseyd 4 replies 1.1k views
Original Post
caseyd
caseyd
I want to make sure that when I am writing code, I am writing in fully compliant C++. I've seen too many tutorials and books that have older code in it that isn't C++ standard or uses mixed c/c++. I am finding that I am falling into that category of coder who doesn't really code in C++ but in weird hybrid of C and C++, missing out on a lot of the benefits of C++. Any suggestions or comments on things to avoid or common mistakes that many people make?
TheC00L1
TheC00L1
Read the newer released books, there's always different tricks people don't realize.
A wiseman once said nothing, but no one was there to hear it.
kuphryn
kuphryn
design encapsulate

Kuphryn
doodo
doodo
By definition C++ is a hybrid programming language, so I wouldn't worry about it. Instead you should focus your attention on learning mathematics and OOP. These two will help you more than reading some book about what 'is' the correct way to use C++. View C++ as a tool that you apply mathematic theory and good OOP design.

In the mean time grab a book on computer architecture and start reading how the low levels of the computer systems work. C/C++ by nature are an engineering programming language that is, they reflect direct access to the computer systems memory.

You will be writing two different type of code in C++. They’re Systems code and also higher language code. Systems code deals with raw data manipulation on the hardware level, where higher language code encapsulates it to make it easier to manage.
JohnBolton
JohnBolton
Depending on the compiler you are using, there are options that will turn off non-standard features supported by the compiler. However, you goal of not writing a "weird hybrid of C and C++" is a little misguided -- C++ is a hybrid. There is no such thing as pure C++ that doesn't include C. If you are writing C, you are writing code that is fully compliant C++ (except, as somebody will most certainly point out, recent extensions to C that are not in C++).

My advice is to learn about the more advanced features in C++ and focus on learning OOP principles, techniques, and methods that utilize them. Don't worry about getting away from C, because you can't.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Hollower
Hollower
The most important thing would probably be to become very well acquainted with the STL. But anyway, I guess my main bullet points for beginners would be:

    read C++ FAQ Lite
    use the extensionless headers ie. #include <string> instead of #include <string.h>
    use STL containers instead of C-style arrays, and use iterators and algorithms when working with them
    use std::cout for console output instead of printf
    use std::string for strings instead of char arrays, and use its member functions and algorithms for string manipulation instead of the old C str... functions
    use fstream for file IO instead of fopen or the other C file IO stuff
    if you must cast, use one of the C++ ..._cast keywords not a C-style cast
    In C++ you can declare local variables near where they are used instead of all at the top of the scope as is done in C
    in C++ structs are simply classes which default to public access and you declare them the same way. There is no need for the old typedef struct idiom to give them a short name.


And some secondary "good idea" points:

    avoid pointers when possible, and use 'smart pointers' and 'handles' in larger projects
    avoid #DEFINE macros. Instead consider a const variable, a typedef, an enum, or an inline function.
    make use of templates for generic type-independant code
    load/new stuff in constructor, release/delete stuff in destructor
    avoid global and static variables when possible (and avoid thinking of singletons as a replacement)
    don't write functions with the variable argument (...) form
    use namespaces, and if you write a 'using' declaration restrict it to the scope in which it is desired (definately don't put it in a header)


Well, that's what I come up with off the top of my head.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.