This thread is to discuss the quiz question in the 4/6/2004 CodeGuru newsletter.
Brad!
Printable View
This thread is to discuss the quiz question in the 4/6/2004 CodeGuru newsletter.
Brad!
these 3 are legal
const char * myPtr = "A";
char * const myPtr = "A";
const char * const myPtr = "A";
these 3 are NOT legal
const char * myPtr = 'A';
char * const myPtr = 'A';
const char * const myPtr = 'A';
reason: 'A' is a constant int.
I will give you that all three are legal and correct.
They are, however, each forcing something different.
The single quotes should not be an issue as these are character pointer definitions (not strings).
The differences between each of these three are..... part of the quiz question :)
Brad!
well,
1. const char * myPtr = "A";
myPtr is not a const, but contents pointed to by myPtr is const
2. char * const myPtr = "A";
myPtr is const, but contents pointed by myPtr aren't
3. const char * const myPtr = "A";
Both myPtr and contents pointed to by myPtr are consts.
One way to know is, as I read somewhere, draw an imaginary line thru *.
If there is a const to it's left, the contents is const
If there is a const to it's right, the var is a const
- const char * myPtr = "A";
declares a pointer to a constant string. This means that you can later change myPtr to point to something different, but you cannot change the string's contents.
I.e. ++myPtr is legal, but myPtr[0] = 'B' is illegal.- char * const myPtr = "A";
Declares a constant pointer to a string. It's sort of the opposite from above. This means that you cannot change the value of myPtr, but you can change the contents of the string.
I.e. ++myPtr is illegal, but myPtr[0] = 'B' is legal;- const char * const myPtr = "A";
You can neither change the pointer nor the value pointed to.
P.S. Is there a way to access the newsletter from the site?
I'm going to try to start posting the newsletters to the site in the news and announcements section; however, this will generally be a few days after the newsletter goes out. (I will be posting last week's tomorrow....-- I'm behind).
You can also get the newsletter by going to:
http://e-newsletters.internet.com/codeguruupdate.html
Brad!
I get the newletters by email already but I cannot access the email folder where it gets put from home, that's why I asked ;) No big deal, sorry to be off-topic.
Once at these forums, there was a thread, where one member put a question, then the one who gives the correct answer, get his turn to ask any other question and in this way the thread moved on.
Isn't it cool, if we have a thread like there. Like Brad asked first question and kirants answered correctly, now kirants asked and the one who'll answer correctly can ask the next question. (Just a suggestion :) )
Without even looking at the newsletter yet...I would think that the following might be part of the answer... :DQuote:
Originally posted by Brad Jones
The differences between each of these three are..... part of the quiz question :)
1. Reference - Can change the referenced object
2. Const-Reference - Can not change the referenced object
3. Const-Pointer - Can change the adress it is pointing to but not the object
4. Pointer-Const - Can change the object but no the address it is pointing to
5. Const-Pointer-Const - Can neither change the address nor the object
Code:CFoo Instance;
CFoo &ReferenceToInstance = Instance; // 1.
const CFoo &ConstReferenceToInstance = Instance; // 2.
const CFoo *pConstPointer = &Instance; // 3.
CFoo *const pPointerConst = &Instance; // 4.
const CFoo *const pConstPointerConst = &Instance // 5.