hi
what is the meaning of const in the declaration below:

const int*const Method3(const int*const&)const;

Thanks

The values there will be constant when put in, like read only, you can't modify them after.

The function Method3 returns a pointer to an int. Neither the pointer returned nor the int pointed to by the returned pointer is modifiable:

const int*const Method3(const int*const&)const;

The function Method3 takes as a parameter a reference to a pointer to an int. Neither the pointer passed nor the int pointed to by the passed pointer is modifiable:

const int*const Method3(const int*const&)const;

And the function Method3 is a member function that will not be modifying any of the object's data:

const int*const Method3(const int*const&)const;

Or something like that.

Maybe this will help :

//regular pointer
int *ptr = 0;

//pointer-to-const-data type
//pointer can change what its pointing to 
const int * ptr = 0;
//const-pointer
//pointer can't change what its pointing to
//pointer can only point to non-const object
int * const ptr = 0;

//Now combine them two to create :

//const pointer that cannot change what its pointing to
//could point to const variable or non-const variable
//once assigned( at deceleration)  it "stays put"
const int* const ptr = 0;

Maybe this will help :

//regular pointer
int *ptr = 0;

//pointer-to-const-data type
//pointer can change what its pointing to 
const int * ptr = 0;
//const-pointer
//pointer can't change what its pointing to
//pointer can only point to non-const object
int * const ptr = 0;

//Now combine them two to create :

//const pointer that cannot change what its pointing to
//could point to const variable or non-const variable
//once assigned( at deceleration)  it "stays put"
const int* const ptr = 0;

Are you sure about your second one?

Perhaps you could add code of can/can't rather than the comments, which I found a little confusing (mostly the 2nd and 4th). But in part, that depends on inline code or function calls.

>>Are you sure about your second one?

Maybe the wording was wrong. When I say "pointer can change what its pointing to " I am talking about the pointer itself. The pointer can point to different const data. Of course the const value of what its
pointing to can't change.

Here is the example for that one :

#include <iostream>
#include <string>
using namespace std;

template<typename Type>
void print(const Type& val, std::string endWith = "\n"){
    cout << val << endWith;
}
int main(int argc, char**argv)
{
    const size_t num = 12;
    const size_t *ptr = &num;

    print("value of ptr : "," ");
    print(*ptr);
    print("Pointing-Address of ptr : " , " ");
    print(ptr);

    const size_t num2 = 21;
    ptr = &num2;

    print("\nvalue of ptr : "," ");
    print(*ptr);

    print("Pointing-Address of ptr : ", " ");
    print(ptr);


    return 0;
}

This code :

const int* const ptr = 0;

Means that ptr can't change whats it pointing to nor can it change the data its pointing to.
It must be initialize when declared, just like a regular const int.

Here is an example for that one :

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char**argv)
{
    const size_t num = 12;

    const size_t* const ptr = &num; //GOOD

    size_t temp = 0;
    //ptr = &temp; //BAD : can't change what its pointing to
    //ptr = 0;    //BAD : can't change what its pointing to

    const size_t forty = 40;
    //const size_t* const ptr2; //BAD : need to initialize in decleration


    return 0;
}

The function Method3 takes as a parameter a reference to a pointer to an int. Neither the pointer passed nor the int pointed to by the passed pointer is modifiable:

const int*const Method3(const int*const&)const;

Don't you think that the

const&

is kind of redundant because we can't reassign a different value to a reference like

int p;
int q;
int& a= &p;
a = &b; // not allowed

so what is the point of declaring it as

const* const&

when

const* &

would suffice?

Thanks

another related thing:

class X {
public:
X&operator=(const X& rhs);
const X& operator+(const X& rhs) const;
const X& operator+(int m);
private:
int n;
};

int main()
{
X a,b,c;
a = b+5;
b = a+5+c;
(c=a+a) = b+c;
a = b+c+5;
a=a=b+c;
return 0;
}

Why did I get compile for the line 16?

test2.cpp:16: error: passing ‘const X’ as ‘this’ argument of ‘const X& X::operator+(int)’ discards qualifiers

Also how this is even working as the '+' operator function is not even defined?

Thanks.

Don't you think that the

const&

is kind of redundant because we can't reassign a different value to a reference like

int p;
int q;
int& a= &p;
a = &b; // not allowed

so what is the point of declaring it as

const* const&

when

const* &

would suffice?

Thanks

When the code is more concrete to me (when I'm writing something I'm really into), I tend to understand better. So I can't say that I'm really being very authoritative on this. In the meantime, I can play with some code and the compiler (I recommend doing so) and see what kinds of results I might get to better help me understand.

This tells me something.

class T
{
   int x;
public:
   const int *const Method2(const int *      &z) const
   {
      return ++z; // no error
   }
   const int *const Method3(const int *const &z) const
   {
      return ++z;  // error: increment of read-only reference `z'
   }
};

int main()
{
   const int value = 42, *ptr = &value;
   T t;
   t.Method2(ptr);
   t.Method3(ptr);
   return 0;
}

What it tells me is something for me to either dig further into, or maybe just share it, or I don't know. I've been a bit sub-par lately.

For examples, firstPerson, I was thinking something maybe even a little simpler(?):

int main()
{
   int a[] = {42,-5};

   // CAN change pointed-to data. CAN change pointer value.
   int *ptr = a;
   *ptr = 7; // okay
   ++ptr;    // okay

   // CAN'T change pointed-to data. CAN change pointer value.
   const int *ptr_to_readonly_int = a;
   *ptr_to_readonly_int = 7; // error: assignment of read-only location
   ++ptr_to_readonly_int;    // okay

   // CAN change pointed-to data. CAN'T change pointer value.
   int *const readonly_ptr_to_int = a;
   *readonly_ptr_to_int = 7; // okay
   ++readonly_ptr_to_int;    // error: assignment of read-only location

   // CAN'T change pointed-to data. CAN'T change pointer value.
   const int *const readonly_ptr_to_readonly_int = a;
   *readonly_ptr_to_readonly_int = 7; // error: assignment of read-only location
   ++readonly_ptr_to_readonly_int;    // error: assignment of read-only location
}

I'm sure your example is fine; I've just been off my game lately.

This code :

const int *const Method2(const int * &z) const
   {
      return ++z; // no error
   }

You are changing what z is pointing to. This is really not good now since
it points to something else than what it originally pointed to.

Thus this code :

const int *const Method3(const int *const &z) const
   {
      return ++z;  // error: increment of read-only reference `z'
   }

Means that you cannot change what z is pointing to.

[nevermind]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.