Pointers

I'm having the hardest time with pointers. I understand what they DO and how to use them, what I'm having a hard time grasping is WHY you would use them. Can someone give me any specific examples of why you would/should use pointers on a general day to day basis? I would greatly appreciate it.
One good example is when you have two classes which both contain instances of each other.
You couldn't do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A{
	A();
	~A();
private:

	B b;
};

class B{
	B();
	~B();
private:

	A a;
};


as class A does not know the full definition of class B, so it can't contain a B object. You can however, give a class a pointer to an object without having the full class definition, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class B; // this forward declares B, so that A knows it exists, before it has been fully defined

class A{
	A();
	~A();
private:

	B* b;
};

class B{
	B();
	~B();
private:

	A* a;
};


and then initialise the pointer variables with operator new in the class' constructors.
Last edited on
Ahh, thanks. That's a good one, but couldn't I just code both classes separately and then include them in each class?
Another situation is when you pass arrays to functions, you can't pass the enitire array at once so you pass a reference to the first character and troll through the array from within the function. You could recursivley call the function passing it each and every instance in the array but that isn't as cool.

How about when you dynamically allocate memory? I'd like to see you do THAT with a non-pointer variable!

Strings, Vectors, Lists etc. <- All use pointers.

I sometimes use long and descriptive variable names for the real variables then handle them exclusivley through pointers. I don't quite know where I picked this habit up from but it's handy as the code pretty much documents it self this way.

Also you can access private members of a class\structure directly using pointers. This is important to know since some libraries seem to insist making you do it.
if you create data structures, you cant do them without pointers..... google for data structures...
also, if you do any advanced things, pointers will be very helpful.. ( pointer arithmetics and all those stuff is very powerful when combined with C lang. , you will need them to write parts of an OS , thread handling programs..etc. )
also advanced sorting algorithms often use them..
Last edited on
if you create data structures, you cant do them without pointers

How about java?
as mentioned above there are several cases where c++ simply just relies on the functionality of pointers to perform however if you really need an appealing reason then its speed, moving around a pointer is much cheaper then moving around data, which loops back into why c++ is so pointy in the first place

you really dont have much of a choice about using pointers in c++, their functionality is one of the main building blocks of the language.. yes they can seem a bit exotic when coming from some other language that does the data management for you but in time they become as second nature as the If statement or something like that
@stereoMatching: AFAIK, except base types, all is a pointer in java (they are just hidden from the user).
@quirkyusername: that can't be constructed because the size will be infinite (first example)

Polymorphism http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming#C.2B.2B

Last edited on
good question stereoMatching,
in java you will not be able to see the difference a lot like in C ( between pointers and other var s)
take a look at "Introduction to Algorithms with Java by Drozdek" in the first chapter it shows how pointers work in java..
"There are pointers in java but u see them as same as normal variables" (you dont use a syntax as "*p" to declare a pointer, its just "p")
java example:
i am not very good with java(actually i have forgotten many of it) but i think you know that "new" keyword is something similar to a macro function, it returns an address..
in java
p = new array[10];
p is a pointer..
Topic archived. No new replies allowed.