Re: Need for const in function argument list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/26/06, Shriramana Sharma <samjnaa@xxxxxxxxx> wrote:
> 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.

> 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
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux