Ignored const qualifier

This is about the cases, where const qualifier is ignored.
I made somewhat vague conclusions, but maybe someone has
something to add?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void int_(int I) {}
//void int_(const int I) {} // [error]: redefinition

void int_p(const int* I) {}
//void int_p(const int* const I) {} // [error]: redefinition

struct X
{
 X(): i(123) {}
 int i;
};

void tryX(const X x) {}
//void tryX(X x) {} // [error]: redefinition

// -----------------------------------------------------------------

int* ig = new int(123);
const int* cig = new int(223);

int const rConstInt() {return 1;} // ignored const!
int const* const rConstIntConstPtr() {return cig;} // ignored const (last)!
X const rConstX() {return X();}

// -----------------------------------------------------------------

int main()
{
 /*
   Conclusions:

   1. function overloading doesn't work if the only
      difference is a const qualifier for an argument.

   2. built-in types (which?) can't be returned as
      constants, but objects can.
 */

 return 0;
}
For the first three cases, the compiler complains because the only difference between the parameters is whether they're const or not. Since the parameters are passed by copy, a valid call to one version is also a valid call to the other, so it would be ambiguous.

It doesn't make much sense to return or take const things, anyway.
1
2
3
int a=rConstInt();
const int *p=rConstIntConstPtr();
X x=rConstX();
Last edited on
Well, true. Let's consider it solved then.
1
2
3
int const rConstInt() {return 1;} // ignored const!
int const* const rConstIntConstPtr() {return cig;} // ignored const (last)!
X const rConstX() {return X();}


I am not able to understant the difference between char const and const char

1
2
3
4
5
6
7
8
char const *c="new";
        const char *c1="newc";

        c="nnn";
	c1="nac";

	*(c1+1) ='c';
	*(c+1) ='c';




what does char const mean?

Also i am not able to understand the diff btw these two prototypes

1
2
void someFunc(char* c const);
void someFunc(const char* c );




const char* and char const* are the same thing.
Last edited on
However there is a difference between char const * and char * const:

1
2
3
4
5
6
7
8
9
	char const* cstr_ptr = new char[1]; // can not modity the char

	char* const str_cptr = new char[1]; // can not modify the pointer

	*cstr_ptr = 'N'; // illegal can not change the char it points to
	*str_cptr = 'Y'; // okay

	cstr_ptr = 0; // okay
	str_cptr = 0; // illegal can not change the pointer 
This prototype is illegal:

void someFunc(char* c const);

Did you mean void someFunc(char* const c); ?

If so, then the above post gives the answer.
Last edited on
Seriously, stop doing that. If you have more to say, edit your posts instead of posting over and over again.
Seriously, what difference does it make?
It's annoying?
No offense helios, but I thought you were one of the users advocating the restriction of editing posts...
http://www.cplusplus.com/forum/lounge/21770/

:P
Yes, when it's not done in good faith. For example, when two users are having a heated argument and one of them later goes and, because he can't delete posts after they've been replied to, blanks them.

EDIT: Such as this case: http://www.cplusplus.com/member/foobarbaz/
Last edited on
closed account (z05DSL3A)
I must admit when I scanned this thread I did wonder what helios was talking about, but that was because I thought the previous three post where from different people not the same person. I don't alway look at who has posted just what, so it was a bit odd.
Guys

1
2
  char const *c="new";
    const char *c1="newc";


from my understanding through one of the previous thread ( http://www.cplusplus.com/forum/beginner/24734/ ) , it looks like both the above statements are taken equivalent to

const char* var = "new";


Thats why when i execute the following statements, i don get an error


1
2
3
4
5
   
Guys

  [code]  char const *c="new";
    const char *c1="newc";


from my understanding through one of the previous thread ( http://www.cplusplus.com/forum/beginner/24734/ ) , it looks like both the above statements are taken equivalent to

const char* var = "new";


Thats why when i use the following statements, i don get an error


1
2
3
4
5
6
7
8
  char const *c="new";
           const char *c1="newc";

           *c="new str";
                *c1="newc str";

             // *(c+1) = 'h' as well as *(c1+1)='d' are throwing errors
   



But when i use char* c =new char[10]; , then the scenario is totally different and all the meanings of const apply..
http://www.cplusplus.com/forum/general/24845/#
Am I right?
Last edited on
When you use char* c =new char[10]; then nothing is const.

1
2
3
4
5
6
7
8
9
10
char const *c="new";
const char *c1="newc";

*c="new str"; // *c is a single char and you are trying to assign a char* to it (which is the address of a char)
*c1="newc str";

// Do you mean this?

c = "new str";
c1 = "newc str"; 


As noted above const char* and char const* are identical.
Last edited on
When you use char* c =new char[10]; then nothing is const.


Rite.. but now
1
2
3
4
5
6
7
8
9
10
11
 const char* c11= new char[20];
	char* const c12 = new char[20];

	c11 = "Karthick";
	c11 = "mad";
	// *c11 ='k'; error coz c11 is a pointer to a character* which is constant

	*c12 = 'k';
	// c12 = "mad";  error coz c12 is a constant pointer

 



This is what i meant... :)


You got it exactly backwards.
1
2
3
4
5
6
7
8
const char *c11= new char[20];
char * const c12=new char[20];

c11[0]=0; //Error. c11 is a pointer to a constant character.
c11=0; //OK. The pointer can be changed.

c12[0]=0; //OK. The thing being pointed to can be changed.
c12=0; //Error. c12 is a constant pointer to a character 
Topic archived. No new replies allowed.