5 Replies - 385 Views - Last Post: 23 August 2011 - 02:48 PM Rate Topic: -----

#1 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 86
  • Joined: 18-June 11

Trying to comprehend constant pointers in a function

Posted 22 August 2011 - 03:30 PM

I'm getting pretty confused looking at this code right here


// functionTwo, passes a const pointer
    const SimpleCat * const FunctionTwo (const SimpleCat * const theCat)
    {
             cout << "Function Two. Returning...\n";
             cout << "Frisky is now " << theCat->GetAge();
             cout << " years old \n";
             // theCat->SetAge(8);   const!
             return theCat;
}




Okay const is a command that does not let a value get changed right? And pointers point to the memory location of a value and can also change the value as well right? If both of these definitions are true , then this function (FunctionTwo), is a class of a SimpleCat that is assigning the memory address to FunctionTwo in which both of their values stay the same. Also the parameter in the function is an object of the class SimpleCat that has become a pointer and they both cannot change values. So finally in the function, theCat object is pointing to the memory address of GetAge which value is 5. So now theCat object has the value 5 stored within itself and is returned ... This is my summary of what I think is happening can someone please tell me if what I'm saying is correct or wrong

Also here's the rest of the code so you can get an idea of it
4:         #include <iostream.h>
5:
6:         class SimpleCat
7:         {
8:         public:
9:                 SimpleCat();
10:                 SimpleCat(SimpleCat&);
11:                 ~SimpleCat();
12:
13:                 int GetAge() const { return itsAge; }
14:                 void SetAge(int age) { itsAge = age; }
15:
16:         private:
17:                 int itsAge;
18:            };
19:
20:            SimpleCat::SimpleCat()
21:            {
22:                   cout << "Simple Cat Constructor...\n";
23:                   itsAge = 1;
24:            }
25:
26:            SimpleCat::SimpleCat(SimpleCat&)
27:            {
28:                   cout << "Simple Cat Copy Constructor...\n";
29:            }
30:
31:            SimpleCat::~SimpleCat()
32:            {
33:                   cout << "Simple Cat Destructor...\n";
34:            }
35:
36:const SimpleCat * const FunctionTwo (const SimpleCat * const theCat);




Is This A Good Question/Topic? 0
  • +

Replies To: Trying to comprehend constant pointers in a function

#2 thephpdev  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 59
  • Joined: 01-July 11

Re: Trying to comprehend constant pointers in a function

Posted 22 August 2011 - 04:22 PM

I think the more you look at it the more confused you will get, the function is pretty simple. All it does is return a object that cannot be changed unless casted. It seems like the const before the class name is unnecessary.
Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Trying to comprehend constant pointers in a function

Posted 22 August 2011 - 04:39 PM

A const pointer to a const object means you can only call functions that are marked const, and it also means you cannot change what the pointer points to.
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Trying to comprehend constant pointers in a function

Posted 23 August 2011 - 10:31 AM

int i1 = 0, i2 = 0;

/*
 col1      col2    col3        */
      int       *       p1 = &i1;
const int       *       p2 = &i1;
      int const *       p3 = &i1;
      int       * const p4 = &i1;
const int       * const p5 = &i1;
      int const * const p6 = &i1;

//note: a const in col1 or col2 has exactly the same meaning

*p1 = 1  ; //valid - no const in col1 or col2
 p1 = &i2; //valid - no const in col3

*p2 = 1  ; //error -    const in col1
 p2 = &i2; //valid - no const in col3

*p3 = 1  ; //error -    const in col2
 p3 = &i2; //valid - no const in col3

*p4 = 1  ; //valid - no const in col1 or col2
 p4 = &i2; //error -    const in col3

*p5 = 1  ; //error -    const in col1
 p5 = &i2; //error -    const in col3

*p6 = 1  ; //error -    const in col2
 p6 = &i2; //error -    const in col3


This post has been edited by Hezekiah: 23 August 2011 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Trying to comprehend constant pointers in a function

Posted 23 August 2011 - 02:20 PM

Read declarations (especially when they contain "const") from right to left:

const SimpleCat * const FunctionTwo (const SimpleCat * const theCat)

theCat is a constant pointer to a SimpleCat constant.

FunctionTwo returns a constant pointer to a SimpleCat constant.

So what do these mean:

A "constant pointer to" means that the pointer can not be changed. Oddly enough constant pointers are not used all that often the main place they might be used is when passing a pointer by-reference to a function (or if you needed a global pointer). In C++ the pointer "this" is always a const pointer.

"pointer to a constant" means that the thing being pointed to can not be changed. This is much more common for example string litterals are "char const*" or "const char*" which is a "pointer to a constant char" or "pointer to a char constant" which both mean the same thing, the char they are pointing to can not be changed, but the pointer can.


So back to your function: inside of the function body the pointer "theCat" CAN ONLY point to that one cat and that cat can't change.

Outside the function things are a bit different. Returning a constant pointer is kind of an oddity because it does not really "mean" much. I can't really store the value in a constant pointer unless I am initializing the variable (because you can't modify the value of a const pointer so it can't be a used on the left hand side of an assignment). -- what you CAN do is store it in a non-const pointer, because you are just making a copy of the data.

So in general it really serves little purpose to return a constant pointer.

you might return a pointer (or reference) to a constant pointer.


Anyway you can use cdecl.org: C gibberish ↔ English to help read complex declarations.
Was This Post Helpful? 0
  • +
  • -

#6 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 86
  • Joined: 18-June 11

Re: Trying to comprehend constant pointers in a function

Posted 23 August 2011 - 02:48 PM

Thank you all for your replies and especially you NickDMax, you helped get a better understanding of the const pointer function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1