const?

  • Thread starter Alexander Dong Back Kim
  • Start date
A

Alexander Dong Back Kim

Hi all,

I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?

Please don't laugh at my stupidness =)

best regards,
Alexander Dong Back Kim
 
P

pauldepstein

Hi all,

I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?

Please don't laugh at my stupidness =)

best regards,
Alexander Dong Back Kim

I very much doubt that your question is stupid, but I don't understand
what you're asking. I know that English is not your first language,
and I understand the difficulty. Clearly you feel that some uses of
the keyword "const" are unnecessary. Could you please provide an
example of such usage that you're asking about? In other words,
please give an example of "const" that illustrates your point, and
let's discuss that example.

Paul
 
J

Juha Nieminen

Alexander said:
I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?

In some cases it's mandatory. For example:

const int size = 10;
int table[size]; // 'size' must be const

In other cases it's a good safeguard against some programming errors
(if you accidentally try to modify a const variable, the error will be
detected at compile time).
 
P

pauldepstein

Alexander said:
I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?

  In some cases it's mandatory. For example:

const int size = 10;
int table[size]; // 'size' must be const

  In other cases it's a good safeguard against some programming errors
(if you accidentally try to modify a const variable, the error will be
detected at compile time).

I interpreted the OP's question differently. I thought he was talking
about const member functions and asking "If const member functions
don't change anything [a false assumption], what's the point of them?"
But I wasn't (and am not) sure hence my request for clarification.

Paul
 
M

mohi

Alexander said:
I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?

In some cases it's mandatory. For example:

const int size = 10;
int table[size]; // 'size' must be const

In other cases it's a good safeguard against some programming errors
(if you accidentally try to modify a const variable, the error will be
detected at compile time).

i would like to add to the above post that if iam not wrong
its not neccessary to declare "size" in the present example as const,
this syntax was required in i guess old compilers but for new
compilers declaring
int size=10;
int array[size];
is absolutely fine
though its a good practice to declare size here as "const int"
 
M

Michael DOUBEZ

mohi a écrit :
Alexander said:
I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?
In some cases it's mandatory. For example:

const int size = 10;
int table[size]; // 'size' must be const

In other cases it's a good safeguard against some programming errors
(if you accidentally try to modify a const variable, the error will be
detected at compile time).

i would like to add to the above post that if iam not wrong
its not neccessary to declare "size" in the present example as const,
this syntax was required in i guess old compilers but for new
compilers declaring
int size=10;
int array[size];
is absolutely fine
though its a good practice to declare size here as "const int"

You are wrong from the standard point of view. This syntax is supported
because c++ compilers also implement C standard which authorize this.

From 8.4.4 of the standard, arrays are defined as:
T D where D has the form D1 [constant-expression]

So the size must be constant value.

If you compile with -pedantic, g++ 3.4.5 gives
error: ISO C++ forbids variable-size array `table'

Note also that the following is always an error:
int size = 10;
int table[size]={}
While the following works:
const int size = 10;
int table[size]={}



Michael
 
M

mohi

mohi a écrit :


Alexander Dong Back Kim wrote:
I know 'const' means there is no affects on member variables or
original variables. However, we can control it on codes right? then
why should we have to use the keyword?
In some cases it's mandatory. For example:
const int size = 10;
int table[size]; // 'size' must be const
In other cases it's a good safeguard against some programming errors
(if you accidentally try to modify a const variable, the error will be
detected at compile time).
i would like to add to the above post that if iam not wrong
its not neccessary to declare "size" in the present example as const,
this syntax was required in i guess old compilers but for new
compilers declaring
int size=10;
int array[size];
is absolutely fine
though its a good practice to declare size here as "const int"

You are wrong from the standard point of view. This syntax is supported
because c++ compilers also implement C standard which authorize this.

From 8.4.4 of the standard, arrays are defined as:
T D where D has the form D1 [constant-expression]

So the size must be constant value.

If you compile with -pedantic, g++ 3.4.5 gives
error: ISO C++ forbids variable-size array `table'

Note also that the following is always an error:
int size = 10;
int table[size]={}
While the following works:
const int size = 10;
int table[size]={}

Michael

i am really very sorry for this its my fault .
g++ and other c++ compilers don't allow the above diclaration.
mohan
 
R

red floyd

Krice said:
You don't have to use it, it's optional.

Try passing a temporary or an rvalue to a function taking a reference
without using const, and tell me how optional it is.
 
R

Ron Natalie

red said:
Try passing a temporary or an rvalue to a function taking a reference
without using const, and tell me how optional it is.

Follow the thread. He's talking about the TOP LEVEL const. A
reference to const is not the TOP LEVEL const.
 
M

mohi

Follow the thread. He's talking about the TOP LEVEL const. A
reference to const is not the TOP LEVEL const.

as i know const keyword is also used to make sure that the value you
passed to a pointer as a const oject is not manupulated by that
pointer and if its passed to a function then if the function attemps
to modify it (if u dont want that function to modify that object)
compiler will produce errors and logic errors can be corrected.
 
P

peter koch

as i know const keyword is also used to make sure that the value you
passed to a pointer as a const oject is not manupulated by that
pointer and if its passed to a function then if the function attemps
to modify it (if u dont want that function to modify that object)
compiler will produce errors and logic errors can be corrected.

It is rather to inform the caller that the value will not be changed,
but yes - you are right. But this is not a top-level const.

/Peter
 
K

Krice

Follow the thread. He's talking about the TOP LEVEL const.

I don't know what top level const is. I was talking about
the situation when you as developer can choose to use
const as a safety method to prevent object getting mutated,
but that kind of const correctness is not required. You
can write a functional program without using const
(or references which I also hate).
 
L

lbonafide

Follow the thread.  He's talking about the TOP LEVEL const.   A
reference to const is not the TOP LEVEL const.

Follow the thread? How did you get from 'However, we can control it
on codes right?' that he was talking about top level const?
 
R

Ron Natalie

Krice said:
I don't know what top level const is. I was talking about
the situation when you as developer can choose to use
const as a safety method to prevent object getting mutated,
but that kind of const correctness is not required. You
can write a functional program without using const
(or references which I also hate).

TOP LEVEL const is a colloquialism for the const qualification
on the parameter itself. That was what was asked in the original
question:

int foo(const int x);
and
int foo(int x);

have the same type. They differ only in the const qualification
of the parameter x. The only difference in these two declarations
occurs inside the execution of the function foo. In the first
case, you can't change x inside foo. Other than that there
is no difference in the declarations. To the outside world
they behave identically. It is ill-formed to declare them both in the
same scope.

Then YOU brought up the following

int foo(const int& x)
and
int foo(int& x)

Here we're not talking about the const qualfication of the parameter
x. In both cases x has no qualifier. You have different typed
functions. The first has a parameter which is a reference to
const int. In the second has reference to (non-const) int.

The same is true if you use pointers (just change reference to pointer
in the previous paragraph.
 
A

Alexander Dong Back Kim

TOP LEVEL const is a colloquialism for the const qualification
on the parameter itself.  That was what was asked in the original
question:

        int foo(const int x);
and
        int foo(int x);

have the same type.  They differ only in the const qualification
of the parameter x.   The only difference in these two declarations
occurs inside the execution of the function foo.   In the first
case, you can't change x inside foo.   Other than that there
is no difference in the declarations.  To the outside world
they behave identically. It is ill-formed to declare them both in the
same scope.

Then YOU brought up the following

        int foo(const int& x)
and
        int foo(int& x)

Here we're not talking about the const qualfication of the parameter
x.   In both cases x has no qualifier.   You have different typed
functions.   The first has a parameter which is a reference to
const int.  In the second has reference to (non-const) int.

The same is true if you use pointers (just change reference to pointer
in the previous paragraph.

Dear Ron,

I think your answer is the one that I was looking for. I appreciate
about your short tutorial =)
Thank you very much.

best regards,
 
A

Alexander Dong Back Kim

as i know const keyword is also used to make sure that the value you
passed to a pointer as a const oject is not manupulated by that
pointer and if its passed to a function then if the function attemps
to modify it (if u dont want that function to modify that object)
compiler will produce errors and logic errors can be corrected.

That is exactly what I think about the keyword "const".
 
P

pauldepstein

That is exactly what I think about the keyword "const".- Hide quoted text -

- Show quoted text -

Presumably, you had in mind some uses of "const" that you had seen in
a book or some notes etc. IMHO you should have simply copied from the
book/notes/other person to say exactly what use of "const" you were
talking about.

Paul Epstein
 
P

pauldepstein

Presumably, you had in mind some uses of "const" that you had seen in
a book or some notes etc.  IMHO you should have simply copied from the
book/notes/other person to say exactly what use of "const" you were
talking about.

Paul Epstein- Hide quoted text -

- Show quoted text -

Why did I get pointedly tagged with "one star" ratings? Could the
person who did this please explain the problem with my postings.

Thanks,

Paul
 
E

Erik Wikström

Why did I get pointedly tagged with "one star" ratings? Could the
person who did this please explain the problem with my postings.

Do not worry about ratings, it is a Google Groups only thing, not
available for rest of the usenet users (which is the majority of those
who post in here).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top