473,657 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = SomePointerToIn t, * const pA = SomePointerToIn t;

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 2584
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 = SomePointerToIn t, * const pA = SomePointerToIn t;

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 = SomePointerToIn t, * const pA = SomePointerToIn t;
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************ *******@nwrddc0 3.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 = SomePointerToIn t, * const pA = SomePointerToIn t;

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
3627
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
4
1976
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 some typedef structure and offcourse x is a pointer to it. Needless to say my intention for writing such functions in C was to protect the accident write to x's content. So far so good. While porting to C++, I've mada DATA_TYPE as class and I want...
13
2487
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 example, if you only have this code ... void foo( const int n ) {}
11
2115
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
2616
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
2776
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1868
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, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
5
1984
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 function, and then figure that the function will not be modifying the list at all, so a const qualifier seems appropriate in the parameter. So essentially I have:
4
2222
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 say I have a function written in assembler/(some non-C language) which takes one pointer-to-const-char arg, the declaration being: extern unsigned int str_len(const char *s);
9
1380
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
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6167
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.