Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / advancing c++ skills?

Author
Message
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 6th Jul 2010 08:22
I've gotten decent at c++ over the last year, and this is my most complicated project yet:


seen in action here: http://www.youtube.com/watch?v=28G7k0MTfxg

So, I'm decent at c++. But... that program isn't too complicated. The hardest c++ syntax thing in there might be operator overloading or vectors. It's good, but I don't really think it's enough to say that I know c++. What about stuff like multithreading, or any of the many c++ keywords I don't understand? How can I learn about stuff like this? Not only am I interested in becoming a better programmer, but all of these syntax thingies I don't know of surely have a use, for better memory management, less errors, easier debugging, more shortcuts, etc. Basically, what are some good projects, or do you have some good tips on how I should proceed from here? I don't really want to start a project and then figure out I'm doing something completely wrong - I could have memory leaks, bad coding, and possible crash points all over the place without knowing it (I think I have some unsolved synchronization problems in some of my java programs)

Even more of a specific and direct question: What are some of the biggest problems, advancements, or learning leaps you've had in c++?


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 6th Jul 2010 17:52
Learning to use template classes and functions is always useful

As for memory management:
- Minimize the use of small amounts of dynamically allocated memory, but never use static arrays for variable sized data.
- Make it as difficult as possible to be able to allocate something without later deleting it.

Obviously there are many standard ways of accomplishing those things, such as reference counting and smart pointers (google should help with these) but any stable method you design is fine as long as it does those things. (Although for public APIs, people will be more familiar with the standard methods)


Multithreading is useful but should never be used unless it will provide a definite benefit. If two threads keep trying to access the same data they will run far slower than a single thread.


One of the most important things to get right IMO is when code should go in header files, and when it should go in code files.

What I do is this:
- Have a single header file included by all others which declares the names of all public classes and structs (but not their contents)
- Have a header file for each important class which declares the class members but does not define the code (Unless it is an inline function or template class/function).
- Have a code file for each important class which contains the code for all the members in that class.
- When including other header files, include them from the code file wherever possible, and the header file as a last resort (in cases such as when you need to inherit from another class).

Doing it this way will ensure that you get no issues with names being used before they are declared, etc.

[b]
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 6th Jul 2010 20:44
Quote: "- Have a single header file included by all others which declares the names of all public classes and structs (but not their contents)"

So, using the code I posted as an example, would I have a header that looks something like this:

to include in all my source code files? To be honest I'm not too clear on headers and libraries and namespaces and all that. Then there's including stuff like <foo.h> <foo> and "foo.h", I don't know the difference.

Also... how do header files point to the code they declare? For example, in DarkSDK3DMaths.h, seen here:

there are functions declared... but where is the compiler finding the information about what the commands do? When a header file is included, does the compiler (linker?) search through all included directories' files/libraries to find the code/stuff?


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Jul 2010 21:28 Edited at: 6th Jul 2010 21:37
The difference between including with <brackets> or "quotation marks" is where the compiler searches for the file to be included. Here is a good explanation:

http://msdn.microsoft.com/en-us/library/36k2cdd4%28VS.71%29.aspx

Usually, you use quotation marks for the code files that you wrote yourself (since all your files are probably in the same project directory, next to each other) and use the brackets for system include files (standard library, windows) because those have to be searched on a different path. The complete search path is set somewhere among the many compiler and linker options.

Difference between <foo.h> and <foo> is that the first is the "old C" version and the second is the "new C++" version. If possible, use the form without extension, it depends on which versions of the header files exist on the hard disk for the given compiler.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Jul 2010 21:36 Edited at: 6th Jul 2010 21:39
Here is more explanation about header files (and the two styles of includes) and putting classes in header files:

http://www.learncpp.com/cpp-tutorial/19-header-files/

http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/

Think of a header file as the "visible interface" that you want to provide to other modules using that file. If a class structure or some function definitions are in the header, you can attach comments to explain what the classes and functions do. So if you are reading the header, you can tell what everything is and how to use it, without having to read the implementation part (the function bodies which are in the cpp file). The header also provides information for the compiler to be able to link the program together.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 6th Jul 2010 21:52 Edited at: 6th Jul 2010 21:55
Basically, each project outputs a single .exe, .lib or .dll. Every code file in a project (.cpp file) is compiled individually to a .obj file, and it is these .obj files which are linked together to produce the .exe, .lib or .dll.

Since each .cpp file is compiled separately, the compiler knows nothing at the start of each one. That means that ANY functions/classes/namespaces you want to use must be declared somewhere in a header which is included by the .cpp file in some way.

When you write '#include' the compiler effectively dumps the code from the included file into the place where you put the '#include' line and continues.

Since the compiler only does a single pass, everything must be declared earlier than when you use it. You will get compiler errors saying that a symbol is not declared if you have not done this.

There are two things you can do with a function or variable. You can declare it, or you can define it.

Every function or variable should only ever be defined in ONE place in the whole project or set of projects. They must be declared once for every code file in the project.

Declaring means that you are telling the compiler that it exists. No actual code is generated for it. Defining it means that the compiler actually outputs some code.

If you declare something more than once or not at all you will get a compiler error. If you define something more that once or not at all you will get a linker error. (When the linker tries to link the .obj files together, it will either find that the code for a particular function/variable is not present in any .obj file, or it will find it in more than one)

It is called linking because the linker attempts to link up all your declarations with the actual code (the definition) for it. If it cannot determine where the definition is then it will fail.

Declaring a function means just writing the prototype, like this:


Defining a function means writing the code as well:


Declaring a variable:


Defining a variable:


You should only put the initialisation for a variable ( = 0) where it is actually defined.

For the above reasons, definitions should almost always go in a .cpp file (That way it can only every be defined once, as you wouldn't use '#include' on a .cpp file) and public declarations should almost always go in a .h file (So that each .cpp file can include it instead of having to copy the declaration into every one!)

[b]
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Jul 2010 21:54
One more thing. The above include explanation should answer your question about where the system finds the implementation of functions listed in the header files, but it is worth to add that you can add additional directories to the search path. In the linker options, you can set additional directories for headers and libraries as well, so if you have plugins which are installed in different directories, then the compiler will know where to look for them.
jamesL
14
Years of Service
User Offline
Joined: 31st May 2010
Location:
Posted: 8th Jul 2010 23:25
===================================================
here are videos of various computer courses at various universities;
most are just different versions of "intro to programming"
===================================================
here are all the courses laid out in one nice page
http://academicearth.org/subjects/computer-science

notice how you get a 3 course sequence from Stanford
Computer Science I, Computer Science II and Computer Science III

there are also courses from Harvard, Stanford and Berkeley
===================================================
if you do the 3 Stanford ones you'll probably be doing well
===================================================
if you have 135 dollars there's this
http://www.gameinstitute.com/content/courses/cpp2/cpp2_lessonplan_10.pdf

http://www.gameinstitute.com/C++_Programming_for_Game_Developers_Module_2.html

its probably right at your level or maybe just slightly below it

for a little more there's this, the nuts and bolts of DirectX
http://www.gameinstitute.com/content/courses/gp2/gp2_lessonplan_14.pdf

http://www.gameinstitute.com/Graphics_Programming_with_DirectX_9_Module_2.html

Login to post a reply

Server time is: 2024-07-04 10:43:33
Your offset time is: 2024-07-04 10:43:33