By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Pointer and new

P: 79
I am now reading a lot of C++ learning materials and encountered the following problems. Please help!

1. What is the difference between gcnew and new, and their uses?
2. What is the difference bettween pointer ^ and * ?
Mar 17 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
1)The first is quite easily answered by searching gcnew in google....
2) I've not come accross ^ used for pointers, the only use for ^ that I'm familiar with is to raise a number to the power of another number. As ^ and & are so close on the keyboard, though, I'll wax eloquent about & and * for a second:

You can think of * as a dereferencing operation and & as a referencing operation.....There are many tutorials available on the net, but I'll try a quick explanation....
when you use * you are saying "The value At", when you use & you are saying "The Address of". It satnds to reason, then that *(&a) is a.
The most common cause of confusion, is that people think of poth these operands as pointer indicators, rather than functions....
To declare a pointer, we say something like
Expand|Select|Wrap|Line Numbers
  1. int* a_ptr
If we substitute our earlier sentence for the asterisk, we are declaring "the value at" a_ptr to be of type int (so a_ptr is now a pointer). The hardest part I always found to understand, was when you later encounter
Expand|Select|Wrap|Line Numbers
  1. *a_ptr + x;
;
This can (when you are unfamiliar with pointers) appear to be saying "the pointer at a_ptr", but if we substitute in our sentence before it is really "the value at" a_ptr.

The & is the opposite operator. Obviously it wouldn't make sense to declare
Expand|Select|Wrap|Line Numbers
  1.  int &a;
bwecause you are saying "The Address of a" to be an integer (which it usually is, but we have no control over that). This operator is used when we want to use an address or pointer in a calculation....
If you have an integer a and you do something like (probably not a good idea unless you know what's going on)
Expand|Select|Wrap|Line Numbers
  1.  (&a)++;
you have set the pointer a to the location in memory after a (I'm pretty sure the compiler is clever, and knows how far to jump across the current element, no matter what it is)....

I know what I have written is a little confusing, so if there is anything specific you still don't understand, post again and I'll try to be a little clearer in my explanation.

One of the better books I have read on the topic is called(I think) simply.... "Understanding Pointers in C++" which you might like to look out for in library/bookshop...
Mar 18 '07 #2

arne
Expert 100+
P: 315
I am now reading a lot of C++ learning materials and encountered the following problems. Please help!

1. What is the difference between gcnew and new, and their uses?
2. What is the difference bettween pointer ^ and * ?
Just like * denotes a pointer to an object returned by new, ^ denotes the handle of an object returned by gcnew. It's called a handle and is pointing to an object that is managed by garbage collection mechanisms.
Mar 18 '07 #3

DeMan
100+
P: 1,806
Learns something new every day.....
Mar 18 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.