So many things keep me from making games

Started by
13 comments, last by apollodude217 17 years, 2 months ago
Quote:Original post by midasmax
Correct me if I'm wrong, but I believe that:

*** Source Snippet Removed ***

and

*** Source Snippet Removed ***

are actually doing the same thing. Because the variable "var" is actually pointing to the address of the object ( of SomeClass ) in the heap. It's just that in C++, you need to specifically state that your variable is a pointer-to-address. In C#, you appear to be assigning a variable to an object, but what is actually happening is that you're assigning the variable to the *address* of that object.

Java programmers would also be familiar with this.



not exactly... the first one must be deleted "manually", the second one is automatically deleted when it gets out of scope (it gets placed on the stack, and is removed when teh stack unwinds/etc). In java, and in a way, C#... the "manually" is handled automatically by a garbage collector... and it is impossible to create the other one, since they are in essence, stackless by design.

Advertisement
I found that learning about the STL collections helped my understanding. Like using a Linked List, or stack.
Try reading this maybe it helps you understand? :)

http://www.codersource.net/c++_pointers.html

Quote:Original post by midasmax
Correct me if I'm wrong, but I believe that:

*** Source Snippet Removed ***

and

*** Source Snippet Removed ***

are actually doing the same thing. Because the variable "var" is actually pointing to the address of the object ( of SomeClass ) in the heap. It's just that in C++, you need to specifically state that your variable is a pointer-to-address. In C#, you appear to be assigning a variable to an object, but what is actually happening is that you're assigning the variable to the *address* of that object.

Java programmers would also be familiar with this.



I've been programming C++ since I was ten years old, but it took me a while to grasp the concept of pointers.

Here's a short code sample.
CMyClass obj;obj.DoSomething();


The first line both declares a CMyClass object and initializes it. The second line calls a function on the object.

Now, using pointers things are a bit more complicated.
CMyClass* obj=NULL;obj=new CMyClass;if(obj!=NULL){  cout << "Memory allocation failed";  return;}obj->DoSomething();CMyClass obj2=*obj;delete obj;


Line 1 declares a "blank" pointer to a CMyClass object. Note that if you try to call a function or access a variable on this empty pointer, all hell will break loose and your program will most likely crash.

Line 2 creates a CMyClass object. The address of this object is stored in "obj". There are two things you need to be careful of:
1) The object creation may fail. If your CMyClass object is bigger than the available memory, "obj" will remain NULL.
2) You will need to destroy the object when you're done using it. If you don't, then you have a "memory leak" which will gradually eat up all of your memory until everything crashes.

The "if" block verifies that the memory allocation worked. If it didn't, then we quit with an error.

The next line calls a member function on the object.

The next line creates a standard C++ object, initializing it with a COPY of the object pointed to by "obj". The * is not multiplication in this case - it's used to mean "the object stored at this address". Beware that unless you have a custom "operator =" function to copy your data, any handles or other data stored in the first object will be copied exactly - once you close the handle on one object, the other handle will be useless.

The last line frees the memory associated with "obj" (automatically calling its destructor).

If you need any more details, drop me a line at "zonenbergsoftware@verizon.net" and I'll see what I can do.
hackerkey://v4sw7+8CHS$hw6+8ln6pr8O$ck4ma4+9u5Lw7VX$m0l5Ri8ONotepad++/e3+8t3b8AORTen7+9a17s0r4g8OP
All code examples will be in C++, not in C# as I have yet to learn it.

Java variables are all either primitive values or references to objects (including arrays).

C/C++, however, allows you to have the values themselves, pointers to the values themselves, pointers to pointers to the values themselves, etc. for primitives, enums, structs, and objects (instances of classes). And for good measure, you even have pointers to functions. You can ALSO have aliases (a.k.a. references?), but I won't go into detail about that one.

The * and & operators are what confuse me personally. They each have 2 meanings related to pointers, etc.

The first meaning comes when the operator follows a type:

int x;
int *y;
int &z = x;

In the above example we have an integer x, a pointer to an integer y, and an alias z to integer x (again, I will not discuss the alias).

If we want y to point to x, we must set it equal to the address of x using the address operator, &.

y = &x

Now, if we want to grab the value of x through the pointer y, we must use the indirection operator *.

*y = 3;

Now x == 3, *y == 3, and y == &x. Does that make sense?

Now for objects. C/C++ actually allows you to access data in structs and objects directly, as well as through pointers, as well as through pointers to pointers. Consider the following:

MyObj obj1; // declare a new object locally, or statically, or ....
MyObj *obj2Ptr = new MyObj(); // declare new object dynamically (on the heap)
// AND declare a pointer to it.

Now, where Java (and I think C#) uses the . operator to access members, C/C++ will use EITHER . -> or ::, depending on what it's accessing the object through.
. member of object
-> member of pointer's object (member of what the pointer is pointing to); this is a shortcut for *(ptr).member.
:: member of a class or namespace

e.g.
obj1.member;
obj2Ptr -> member;
*(obj2Ptr).member; // this is the same as the above line

SO, recognizing the difference between an object and a pointer to an object is CRUCIAL!!! I recommend using pointers to all your objects, and please BE CAREFUL not to modify the pointer unless you know what you're doing!!!

This topic is closed to new replies.

Advertisement