Learning C++ for Games Developers

Ronin Storm

Administrator
Staff member
Just completed the chapter on pointers. It's clearly only the start of details on pointers but they seem pretty straight-forward so far. Updated for the next chapter.

Looks like Sunday afternoons are turning into my C++ study time. :)
 

Wol

In Cryo Sleep
I thought i understood pointers and stuffs, and then got confused with refs. Although someone explained them to me, so tis all better.

Pointers are nice in ways yea. Pointers to pointers are even more fun :p

"no, its over there...", "no. its over there"...
 

Ronin Storm

Administrator
Staff member
From my reading, a pointer is a variable whose value is a memory address. That's it, really. References are the next chapter... ;)
 
E

elDiablo

Guest
The C++ FAQ Lite (linky) is very good at answering little questions like that, when you have the basic knowledge of C++, rather than none at all.

In response to the difference between references and pointers:

Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

By "reseating", it means assigning a new object to the reference/pointer. With a pointer to an object, you can change the value of the pointer to a different memory location so that you point to a different object. For example:

Code:
char* pStr = "we love cheeze!";
char* pStrNew = "we hates cheeze!";
pStr = pStrNew;
cout << pStr << endl;

will display "we hates cheeze!" (and you will have a memory leak, but meh).

You can not do this with a reference, as the reference is a name for an object. You can't separate it from the object. You can, however, write an assignment operator overload for the object, so you can set the object's variables. Eg:

Code:
MyObject x(4,"cheeze!");
MyObject y(10,"bananas!");
x = y;

will make x's variables equal to the variables held in y (persumably, in this example, 10 and "bananas!"). Though this may mean that you are using more memory when working with references (read up on copy constructors and assignment operators, as well as Resource Acquisition Is Initialization, for some fun times), but means you can avoid memory leaks like the one present in the first example!

Still, there's an argument there about which is better, and depending on who you speak to, you get a different reply :)

NB: Obviously this is a VERY rough discussion, so don't take it as a know all guide. I don' know everything :D
 

Ronin Storm

Administrator
Staff member
Yeah, for the record, elD's answer is more advanced than you're going to need just now (if you were up to the same point as me in the book). :)
 

Wol

In Cryo Sleep

Don't be

thats cleared something up for me at least! heh.

Im currently trying to get a wiimote c++ class working nicely, and have been farting around with pointers, function pointers and libraries and wierd things. ugh. although i think its working now so tisnt too bad.

just gotta think of something i can do with all the power of opengl, and a wiimote. *-) and i dont have any IR leds atm, so havent been able to get head tracking working! Although stereoscopic opengl is scaary. eyes screw up very fast.

but ideas welcome (pm)! lol.
 

Ronin Storm

Administrator
Staff member
Slightly behind my own schedule, largely because chapter 1 of the C# specification was tougher than I expected (yes, C# as I'm looking at that in parallel), I've completed week 8. On to week 9, though I think I need more time with references than I've given myself to date.

However, concurrent with weeks 9 to 12 there is also the first project! :) Plenty of scope to practice in there, I guess...
 

Ronin Storm

Administrator
Staff member
On to week 10! I think to make headway on the implementation of Project 1 we'll need to read chapter 13 (week 11), which covers strings and arrays. Okay, not "need" per se but certainly it'd make it easier. That's up next week. The design for the project, however, should be accessible without those so I'll mock my design up today and tomorrow.
 

Ronin Storm

Administrator
Staff member
Rar! See my powA! Week 10 complete in the same day I finish off week 9. :) That makes me caught up with myself again, which is awesome because tomorrow I intend to leap into arrays and strings and then start implementing the first project.

Had a couple of people express an interest in having a look at C++ along the way. Anyone else made any progress yet?
 

thatbloke

Junior Administrator
I would, But i havent ahd the time to spend looking at this or the c# one. Might have a look tomorrow though...
 

Ronin Storm

Administrator
Staff member
Well, chapter 13 was the toughest one I've had yet. A soiree into pointer arithmetic didn't help. I think I'm going to pause and reflect on what I've learned through this week rather than plough on into chapter 14 (though I've updated the chapter list to include chapter 14).

I understand arrays on the stack. I understand character arrays on the stack. I'm still not entirely sure I have got arrays on the heap pinned down, nor all the stuff about character arrays and their interaction with strings. A little peripheral reading tells me that there's a bunch of stuff I can do with strings and arrays that really assist (std::string and std::vector, I believe?) but this chapter didn't cover those. Guess they're coming later.

I think knowing C# is starting to be a small problem, now. It really helped on the early stuff but where .NET Framework constructs exist to make managing collections and strings really easy, the whole business with pointers and pointer arithmetic and character arrays seems counter-intuitive to me. I'll get it, just going to take a bit more work. Anyone got any good (alternate) reference material I could look at?
 

VibroAxe

Junior Administrator
In my experience, if your doing pointer arichmetic and things in C# you've gone wrong somewhere, as it's supposed to be avoiding the need to use them
 

Wol

In Cryo Sleep
I think one major good use of pointers in c# is when using pictures. GetPixel and SetPixel are godawfully slow!
 

Ronin Storm

Administrator
Staff member
Worth a little update I feel.

Some might be wondering where all this got to. I've parked my learning of C++ for the time being. My original intention was to look at C++ to see whether I'd find it interesting and useful to me in my games development, perhaps with a view to picking up a skill that I could use to get me into the games development industry proper.

As an experiment, it was very successful. I now know a bunch more about how C/C++ came to be, how memory works and bits on the complexities of writing OO code in C++. However, C++ wasn't particularly fun or inspiring to write, for me. It was also a real pain in the butt in the first project; I knew what I wanted to write but writing it in C++ seemed to be a minefield of memory-management design decisions (where oh where should this pointer be managed?) and tedious, verbose wiring up.

I found that working with C++ has made me appreciate C# all the more, and understand more about the basic C syntax that I'd not have found if I'd not actually been there.

But C++ is not for me. As a project, very successful. As a language, not at all. I'll come back to it if I absolutely require it and not otherwise.

So, from here, back over to the C# Workshop, picking up from where that didn't quite take off (sorry, which Ronin thought he could do two workshops and work full time at the same time, hmm?).
 

thatbloke

Junior Administrator
Understanding what that pointer actually IS and what the garbage collection and automatic memory management stuff gets up to though is useful. And I'm sure that helped :)
 
Top