PDA

View Full Version : From Java to C++


mt_maui
2006.11.25, 02:40 AM
Hey, I've been programming in Java for awhile now, and I'm trying to make the switch over to C++. I've finally got a little bit of momentum going on a project using SDL, but I'm getting the feeling as I go that I'm approaching this with too much of a "Java mindset." My main concern is that there are basically no pointers in my program, everything is passed by value.

I feel like eventually this is going to cause performance issues, or that even worse I won't really learn to use the programming language to it's full extent. I've read enough about pointers to understand how to use them, it's just that as I go through this I don't really understand why I would use them.

I've posted some code, I'd hope someone take a look and give me some feedback on how I've structured these classes. The source file is included, though really all you need to see are the header files. Vector2D is a class to help out with vector math, Dot would be a like a gamepiece or particle on the screen.

Here's the link, thanks in advance for any feedback (http://www.filecloud.com/files/category.php?user_category_id=835455)

akb825
2006.11.25, 03:02 AM
For an interim solution, you can use references. Essentially, for parameters, you could use type &name. The only problem with that is you can't pass something like NULL, but in some cases it's a bit more convenient to use references anyway . However, pointers are a very powerful and very useful tool to have, so be sure to learn about those ASAP.

OneSadCookie
2006.11.25, 03:19 AM
Not really a Java mindset; in Java everything that's not a primitive is a pointer.

Of course, Java gives you garbage collection to help with cleaning up ;)

Any particular reason you're trying to switch from a decent language to a horrible one?

akb825
2006.11.25, 05:52 AM
^^Opinions may vary. :p (my opinions of which one is decent and which is horrible is the exact opposite)

mt_maui
2006.11.25, 03:26 PM
^^Opinions may vary. :p (my opinions of which one is decent and which is horrible is the exact opposite)

Well, I don't really know enough to have a strong opinion yet, another good reason to learn.

Thanks for the feedback, and for anyone else who might take a look at the code.

akb825
2006.11.25, 03:32 PM
BTW, I suggested this set of tutorials (http://www.cplusplus.com/doc/tutorial/) to another member last night, and it does appear to have a good introduction to pointers, among other parts of C++. Also, as a side note, you may want to learn parts of C while you're learning C++. (for example, I find that I use C's IO functions almost exclusively)

mt_maui
2006.11.25, 07:56 PM
Those tutorials are helpful, thanks alot.

A quick question: I want to make sure this doesn't create a memory leak:

void Path::addToPath(float x, float y, float x1, float y1) {
Segment *pAdd = new Segment(x, y, x1, y1);
pathSegments.push_back(pAdd);
}
void Path::clearPath() {
for(unsigned int i = 0; i < pathSegments.size(); i++) {
delete pathSegments.at(i);
}
pathSegments.clear();
}

Clear path is called in the destuctor, but is this actually calling delete on the pointers held in the vector? And is there a way to test this?

Thanks again.

OneSadCookie
2006.11.25, 08:13 PM
That looks fine to me.

akb825
2006.11.25, 08:16 PM
yes, that would call the destructor on those pointers. To make that a little cleaner, thanks to the magic of operator overloading, you could use the vector like an array, and do
delete pathSegments[i]

Also note, that thanks to the magic of references, you can also assign to a vector like an array. Say you were replacing a segment, you could do this:
delete pathSegments[i];
pathSegments[i] = otherSegment;

For some of these data structures, such as vectors, you may find it easier to use regular objects rather than pointers, since you don't have to worry so much about the memory. (assuming you overload the operator = and copy constructor properly) For more heavy duty objects, though, it's still better to use pointers because otherwise it's going to copy the data many times as the data structure resizes, or if you need to swap elements, etc. If you're using something like a tree, though, or a pre-sized vector, (basically where you know that the objects won't be moving around that much) storing the objects themselves rather than the pointer would definitely be the way to go.

mt_maui
2006.11.25, 09:58 PM
Okay, I didn't realize I could use that method with vectors, too, that's convenient. This is actually a problem that forced me to use pointers and the heap in order to find the solution, so it really helped me to understand the concept.

So in your example, otherSegment a reference? So you're just redirecting the pointer at pathSegments[i], instead of creating a new one, right?

akb825
2006.11.25, 11:56 PM
Yeah, that's pretty much what's happening. Essentially, references are merely pointers being handled behind the scenes. Operator [] in vector returns a reference to the type it's holding, in this case a Segment *. So what the reference does behind the scenes is take the address of the Segment * the vector is holding, returns that, and whenever you do anything with what was returned, it automatically dereferences it for you. (basically it's just pointers without having to do the syntax for pointers)