473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

const qualifier and VC6.0

Hello,

I am working with Visual C++ 6.0 compiler.

In the following declaration:

int const A = 10, B = 10;

both A and B are const. However, in declaration

int * const pA = SomePointer, pB;

The pB is just int and not int * const. Is it VC++ compiler bug? What pB has
to be according to C++ standard?
Also, VC++ allows the following declaration of two int * const:

int * const pB = SomePointerToInt, * const pA = SomePointerToInt;

However it says in help file, that const type qualifier can appear only once
in declaration. Where is the truth?

Thanks,
Sergey Tolstov
Jul 19 '05 #1
8 2566
Sergey Tolstov wrote:
Hello,

I am working with Visual C++ 6.0 compiler.

In the following declaration:

int const A = 10, B = 10;

both A and B are const. However, in declaration

int * const pA = SomePointer, pB;

The pB is just int and not int * const. Is it VC++ compiler bug? What
pB has to be according to C++ standard?
Not a bug. This is how the language works.
Also, VC++ allows the following declaration of two int * const:

int * const pB = SomePointerToInt, * const pA = SomePointerToInt;

However it says in help file, that const type qualifier can appear
only once in declaration. Where is the truth?


Pointer declaratiosn contain two type. The type of what is pointed to, and
the pointer itself.
int *p;
ptr to int

const int *p;
ptr to const int (does not mean the int is const, but cannot be modified via
the ptr)

int * const p;
const ptr to int (the pointer itself cannot be modified, but the int it
points to can)

const int * const p;
const ptr to const int (neither the pointer nor the valure pointed by it can
be modified)
--
Attila aka WW
Jul 19 '05 #2
Sergey Tolstov wrote:

The trick is to see what the compiler is doing:
Hello,

I am working with Visual C++ 6.0 compiler.

In the following declaration:

int const A = 10, B = 10;
effectively, (int const) A = 10,B = 10;
A and B const int.
both A and B are const. However, in declaration

int * const pA = SomePointer, pB;
* is a unary operator returning the value at the address of the
following argument,
effectively: int (* const pA = somePointer),pB;

pA const pointer to int (*pA must be initialized and contents can be
altered!), pB a plain int!

Consider the very misleading:
int* a,b;
a is pointer to int, b is int!

effectively int (*a),b;

Since the * operator is unary on the following address, it is better
style (uh-oh ;-)) to write * with the variable than with the type:
int *a,*b;
The pB is just int and not int * const. Is it VC++ compiler bug? What pB has
to be according to C++ standard?
Also, VC++ allows the following declaration of two int * const:

int * const pB = SomePointerToInt, * const pA = SomePointerToInt;
The effect is pB and pA are const pointer to non-const data, and the
compiler will allow:
*pA = 10;
*pB = 5;
but address assignment will be illegal:
pA = &someInt;
pB = &otherInt;

const is misplaced. If const left of *, then data is constant, if right,
then the pointer is constant, if both, then a constant pointer to
constant data.

You need to decide what is constant. From "Effective C++" by Scott Meyers:

"For pointer, you can specify whether the pointer itself is const, the
data it points to is const, both, or either:

char *p = "Hello"; // non-const pointer, non-const data

const char *p = "Hello"; // non-const pointer, const data

char * const p = "Hello"; // const pointer, non-const data

const char * const p = "Hello"; // const pointer, const data"

You seem to want non-const pointer to const data:

const int *pA,*pB; // Note no initialization required!

or const pointer to const data:
const int * const pA = ...,* const pB = ...;
effectively (const int)(* const pa = ...),(* const pB = ...); // Since *
must be used for each pointer, so must the corresponding const.
However it says in help file, that const type qualifier can appear only once
in declaration. Where is the truth?
Thanks,
Sergey Tolstov


Regards
Andrew

Jul 19 '05 #3
On Tue, 07 Oct 2003 13:12:00 +0200, Andrew Morgan
<An***********@telkomsa.net> wrote:
Since the * operator is unary on the following address, it is better
style (uh-oh ;-)) to write * with the variable than with the type:
int *a,*b;


The inevitable bite:

In C++ it is better to put the * with the type, since in C++ the
emphasis is on types, while in C the emphasis is on expressions. It is
also important that you only use one pointer or reference declaration
per line.

int* p, j; //bad!

int* i; //good
int j; //good

(FWIW, Stroustrup uses this style)

Tom
Jul 19 '05 #4
tom_usenet wrote:
On Tue, 07 Oct 2003 13:12:00 +0200, Andrew Morgan
<An***********@telkomsa.net> wrote:
Since the * operator is unary on the following address, it is better
style (uh-oh ;-)) to write * with the variable than with the type:
int *a,*b;


The inevitable bite:

In C++ it is better to put the * with the type, since in C++ the
emphasis is on types, while in C the emphasis is on expressions. It is
also important that you only use one pointer or reference declaration
per line.

int* p, j; //bad!

int* i; //good
int j; //good

(FWIW, Stroustrup uses this style)


Dan Saks (worked with Stroustup on the language) suggests to use the style
which reflects the semantics of the language:

int *p, j; //bad, but visibly!

int *i; //good
int j;

--
Attila aka WW
Jul 19 '05 #5
In article <nf*******************@nwrddc03.gnilink.net>, sa***@yahoo.com
says...
Hello,

I am working with Visual C++ 6.0 compiler.
You might want to consider updating your compiler -- this one came out
well before the standard, so there are substantial parts of the standard
that it doesn't attempt to implement. With that said, however...
In the following declaration:

int const A = 10, B = 10;

both A and B are const. However, in declaration

int * const pA = SomePointer, pB;

The pB is just int and not int * const. Is it VC++ compiler bug? What pB has
to be according to C++ standard?
In this area it's absolutely correct. If you switched the two items
around:

int pB, *const pA = SomePointer;

you'd still be defining the same things.
Also, VC++ allows the following declaration of two int * const:

int * const pB = SomePointerToInt, * const pA = SomePointerToInt;

However it says in help file, that const type qualifier can appear only once
in declaration. Where is the truth?


The help file you're reading is doing a poor job of expressing the idea,
and the compiler is doing the right thing. When you define more than
one object, you can apply const to each perfectly legitimately. You can
even use const more than once in defining a single object:

int const * const x;

for one example, is well-formed. What you can't do is directly apply
const twice, as in something like:

int const const x = 1 ; // ill-formed.

If, however, the const-ness gets introduced a second time because of
something like a template, it's usually allowed:

template<class T>
struct X {

void f(T const &t) { }
};

X<char const x> y;

Taken literally, this _would_ mean that the argument to f is a reference
to a const const char, but in fact the extra const is simply ignored.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #6
tom_usenet wrote:
On Tue, 07 Oct 2003 13:12:00 +0200, Andrew Morgan
<An***********@telkomsa.net> wrote:

Since the * operator is unary on the following address, it is better
style (uh-oh ;-)) to write * with the variable than with the type:
int *a,*b;

The inevitable bite:

In C++ it is better to put the * with the type, since in C++ the
emphasis is on types, while in C the emphasis is on expressions. It is
also important that you only use one pointer or reference declaration
per line.


This is a self-imposed restriction, since the compiler could not care
less about:
int a,*b,**c;

int* p, j; //bad!

int* i; //good
int j; //good

I have no intentional of being drawn into a controversy, so my only
response will be to quote verbatim from "C++ The Complete Reference, 3rd
Edition" by Herbert Schildt:

"A Matter of Style
------------------
When declaring pointer and reference variables, some C++ programmers use
a unique coding style that associates the * or the & with the type name
and not the variable. For example, here are two functionally equivalent
declarations:

int& p; // & associated with type
int &p; // & associated with variable

Associating the * or & with the type name reflects the desire of some
programmers for C++ to contain a separate pointer type. However, the
trouble with associating the & or * with the type name rather than the
variable is that, according to the formal C++ syntax, neither the & nor
the * is distributive over a list of variables. Thus, misleading
declarations are easily created. For example, the following declaration
creates one, not two, integer pointers.

int* a,b;

Here, b is declared as an integer (not an integer pointer) because,
as specified by the C++ syntax, when used in a declaration, the * (or &)
is linked to the individual variable that it precedes, not to the type
that it follows. The trouble with this declaration is that the visual
message suggests that both a and b are pointer types, even though, in
fact, only a is a pointer. This visual confusion not only misleads
novice C++ programmers, but occasionally old pros, too.

It is important to understand that, as far as the C++ compiler is
concerned, it doesn't matter whether you write int *p or int* p. Thus
if you prefer to associate the * or & with the type rather than the
variable, feel free to do so. However, to avoid confusion, this book
will continue to associate the * and the & with the variables that they
modify rather than their types."
(FWIW, Stroustrup uses this style)

Tom


It is simply a matter of style I and do not think that people should be
berated for a style preference. FWIW many of the great C++ authors like
Scott Meyers, Herbert Schildt and James Coplien use the int *a style,
which others like Herb Sutter, Nicolai Josuttis and Andrei Alexandrescu
use int* a.

Andrew

Jul 19 '05 #7
Andrew Morgan wrote:
I have no intentional of being drawn into a controversy, so my only
response will be to quote verbatim from "C++ The Complete Reference,
3rd Edition" by Herbert Schildt:


Don't! Please oh don't quote Schildt here. Should we extend Godwin's law?

--
Attila aka WW
Jul 19 '05 #8

"Andrew Morgan" <An***********@telkomsa.net> wrote in message
news:bl**********@ctb-nnrp2.saix.net...
Sergey Tolstov wrote:

..., in declaration

int * const pA = SomePointer, pB;


* is a unary operator returning the value at the address of the
following argument,
effectively: int (* const pA = somePointer),pB;

pA const pointer to int (*pA must be initialized and contents can be
altered!), pB a plain int!


The above statement doesn't make sense to me. You say the * in the above
statement is a unary operator (the dereference operator, I assume)? It's
not an operator at all in this case, but part of the declaration. It
declares pA to be of type int* (a pointer-to-int). It does not return
anything. This is a declaration (actually two declarations, the second
being "int pB"), with an initialization (the value of SomePointer). The end
result is as you describe, but the reason is different.

-Howard
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
13
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
9
by: sarathy | last post by:
Hi, Can anyone just help me with what exactly is constant expression. I read the section on Constant expression in K&R2. i am not fully clear with it.
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
9
by: istillshine | last post by:
It seems I never need to use it. It only made a long line longer, and made you type five more characters. I feel so strange why people are talking about it.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.