472,954 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

some questions about "const"

1.int const x=5;
const int x=5;
2.const char *p=new char[20];
char const *p=new char[20];

the above pairs are equivalent?

thanks!
Nov 22 '05 #1
5 1401
nick wrote:
1.int const x=5;
const int x=5;
2.const char *p=new char[20];
char const *p=new char[20];

the above pairs are equivalent?
yes

thanks!

Nov 22 '05 #2

nick wrote:
1.int const x=5;
const int x=5;
2.const char *p=new char[20];
char const *p=new char[20];

the above pairs are equivalent?

thanks!


Refer Stroupstrup's C++ Style and Technique FAQ
http://www.research.att.com/~bs/bs_f...constplacement

Nov 22 '05 #3

Neelesh wrote:

Refer Stroupstrup's C++ Style and Technique FAQ

Sorry for the splling mistake, typing error.
Bjarne Stroustrup's C++ Style and Techniques FAQ

http://www.research.att.com/~bs/bs_f...constplacement

Nov 22 '05 #4
"nick" <i1********@yahoo.com> wrote in message
news:dl***********@justice.itsc.cuhk.edu.hk...
: 1.int const x=5;
: const int x=5;
:
: 2.const char *p=new char[20];
: char const *p=new char[20];
:
: the above pairs are equivalent?

Yes, so which one you use is a matter of stylistic convention.

I only use a leading const for true compile-time constants:
const int x = 5;
const double pi = 3.1415926535;
(note that these are never composite types).
In all other cases, I put const *after* the type it affects:
char const *const p = new char[20];
int const h = getHeight( myWindow );
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 22 '05 #5

"nick" <i1********@yahoo.com> wrote in message
news:dl***********@justice.itsc.cuhk.edu.hk...
1.int const x=5;
const int x=5;
2.const char *p=new char[20];
char const *p=new char[20];

the above pairs are equivalent?

thanks!


Yes. Simple rule: const modifies what is to its immediate left. BUT...if
there's nothing to its left, then it modifies what is to its immediate
right.

-Howard
Nov 22 '05 #6

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

Similar topics

1
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
11
by: Der Andere | last post by:
What exactly is the sense of using a "const" modifier before a function declaration? Actually, you can use it in three places. For instance, take a look at the following function declaration (from...
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
16
by: Chris | last post by:
Looking at some code I see a declaration inside a function like static const string s("some string"); Does the static serve any purpose here?
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
3
by: =?Utf-8?B?U3VybWVldCBKYW5kdQ==?= | last post by:
Hi, Can any body tell me that instead of "const" what can we use in MC++. Thanks in advance!!
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.