Need for const in function argument list
In the Qt constructor for the DateTime class, I see:
QDateTime::QDateTime(const QDate &date)
what is the need for the keyword const here? If it is to indicate that the date variable does not change, then in very many such places should the const keyword be used. But it is not.
As a related question, in the syntax for printf, we have printf(const char *format) -- why should we have const here?
Signature · 3 lines
Tux #395953 resides at http://samvit.org playing with KDE 3.51 on SUSE Linux 10.0 $ date [] CCE +2006-03-26 W12-7 UTC+0530
Quoted 7 lines · Shriramana Sharma
In the Qt constructor for the DateTime class, I see:
QDateTime::QDateTime(const QDate &date)what is the need for the keyword const here? If it is to indicate that the date variable does not change, then in very many such places should the const keyword be used. But it is not.
You've answered your own question. If there is no const in places where it should be there can be three reasons: (a) either the developer have forgotten to mark the parameters const or (b) the code has been written before ANSI C where the const keyword was not known. Most sophisticated libraries provide properly designed functions, so I am sure, (c) they have had their reasons not to "const" in some places.
Quoted 2 lines
As a related question, in the syntax for printf, we have
printf(const char *format)-- why should we have const here?
Because the format (the string to display) is provided as a constant expression and it is not being modified:
printf("%s\n", "abc");
printf("%s\n", mystring);In both cases the format argument is a constant string.
\Steve
Sunday, 26 March 2006 16:27 samaye, Steve Graegert alekhiit:
Quoted 5 lines
Because the format (the string to display) is provided as a constant expression and it is not being modified:
printf("%s\n", "abc"); printf("%s\n", mystring);In both cases the format argument is a constant string.
Why, the following works as well:
#include "stdio.h"
void main(void)
{
char s[10] = "\n%s\n\n";
printf(s, "hello");
}Here s is not a const char *. It is a variable char *. That actually compiled and executed. So probably it's only because the printf *function* does not change the value (and *should* not change the value during parsing, I presume).
Signature · 3 lines
Tux #395953 resides at http://samvit.org playing with KDE 3.51 on SUSE Linux 10.0 $ date [] CCE +2006-03-26 W12-7 UTC+0530
Quoted 12 lines · Shriramana Sharma
Sunday, 26 March 2006 16:27 samaye, Steve Graegert alekhiit:
Quoted 5 lines
Because the format (the string to display) is provided as a constant expression and it is not being modified:
printf("%s\n", "abc"); printf("%s\n", mystring);In both cases the format argument is a constant string.
Why, the following works as well:
#include "
stdio.h"
void main(void)
Quoted 7 lines
{ char s[10] = "\n%s\n\n"; printf(s, "hello"); }Here s is not a const char *. It is a variable char *. That actually compiled and executed.
Yes, it compiles and why should it not do so? It's a __string constant__, since it cannot be changed in any way. It's effectively the same as the statement
char *s = "\n%s\n\n"
The situation is different with malloc(2). It allows for dynamic allocation of memory, thus turning string constants into dynamic data structures:
char * s; s = (char *) malloc(100);
Every string inside double quotes (" ") is a character constant.
\Steve
Quoted 15 lines · Shriramana Sharma
Quoted 5 lines
Because the format (the string to display) is provided as a constant expression and it is not being modified:
printf("%s\n", "abc"); printf("%s\n", mystring);In both cases the format argument is a constant string.
Why, the following works as well:
#include "
stdio.h"
void main(void){ char s[10] = "\n%s\n\n"; printf(s, "hello"); }
Quoted 4 lines
Here s is not a const char *. It is a variable char *. That actually compiled and executed. So probably it's only because the printf *function* does not change the value (and *should* not change the value during parsing, I presume).
If a pointer parameter is declared as const, you can pass either const or non-const pointers. If it isn't declared as const, then you can only pass non-const pointers, not const pointers.
When applied to function parameters, const indicates that the function will not modify the data to which it points. The absence of const indicates that the function may modify the data, and thus you cannot pass a pointer to immutable data.
When applied to variables (lvalues), const indicates that the data should not be modified by anything. Attempts to modify it (either directly or by passing a pointer as a non-const parameter) will generate a warning, and the data itself may be stored in a read-only memory segment.
Pointer types should always be declared as const unless you actually need to modify the data.
Similarly, if you aren't going to modify an array, it should be declared as const (or static const for a local variable), e.g.:
static const char s[] = "\n%s\n\n";
Doing so will cause the compiler to place the array in the rodata section. The rodata section is mapped read-only, and one copy is shared by all processes using the executable (or shared library), rather than needing one copy for each process.
For local variables, using "static const" eliminates the need to copy the string into the array at run-time. For a function which is called frequently, this will improve performance.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 5 lines · Glynn Clements
If a pointer parameter is declared as const, you can pass either const or non-const pointers. If it isn't declared as const, then you can only pass non-const pointers, not const pointers.
For that alone please use const when it is needed. Some moron I had to work with once wrote a wrapper for some printing functions and in the most significant of his functions he had void PrintText(char*){/...}
and there was no way for me to write:PrintText("Hello World");
in my code, I had to use the ugly:PrintText(const_cast<char*>("Hello World"));
because of someone else's ignorance.
Signature · 1 line
Make it work. Make it fast. Make it right. Pick any two...