Re: How to define a const and initialize it?
From: ranjeet (ranjeet.gupta_at_gmail.com)
Date: 09/15/04
- Next message: ranjeet: "Re: questions of unsigned int, function prototypes"
- Previous message: ranjeet: "Re: Passing a 2-dimensional array"
- In reply to: James Dennett: "Re: How to define a const and initialize it?"
- Next in thread: Karl Heinz Buchegger: "Re: How to define a const and initialize it?"
- Reply: Karl Heinz Buchegger: "Re: How to define a const and initialize it?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 15 Sep 2004 04:57:39 -0700
James Dennett <jdennett@acm.org> wrote in message news:<k8c%c.13060$aW5.2752@fed1read07>...
> alexwu wrote:
>
> > David White <no@email.provided> wrote in message
> > news:kda%c.112$pl.13050@nasal.pacific.net.au...
> >
> >>"alexwu" <alex139@nospam.com> wrote in message
> >>news:9L7%c.561263$Gx4.14435@bgtnsc04-news.ops.worldnet.att.net...
> >>
> >>>How to define "const int MYCONST = 0x97;" as a global variable in a .h
> >>
> >>file?
> >>
> >>>The .h will be included in several .cpp files so that the const can be
> >>
> >>used?
> >>
> >>Unless you have a particular reason for having the value stored at a
> >>specific location (such as taking the address of it), you can simply place
> >>the definition in the header file as you've written it above, i.e.,
> >>
> >>// Header file
> >>const int MYCONST = 0x97;
> >>
> >>This is the usual method of defining constants that are needed by multiple
> >>source files in C++.
> >>
> >>DW
> >>
> >
> > If including the .h file that contains,
> >
> > // Header file
> > const int MYCONST = 0x97;
> >
> > in multiple source files, will it cause duplicate inclusion that compiler
> > complain?
>
> In C, it'll make the linker complain, as const objects still
> have external linkage unless you specify them as static.
>
> In C++, it's fine.
Can you tell me one thing i can do this by making a macro also
#deifne MYCONST 0x97
now if i use this macro.h in all the source file then it will
do the same work as const int MYCONST will do....
So i am bit puzzled about this issue ...
that why we need the const key word..????
when i can do the same whith the macros......
what u all say please highlight the diffrence between of there usage??
>
> -- James
- Next message: ranjeet: "Re: questions of unsigned int, function prototypes"
- Previous message: ranjeet: "Re: Passing a 2-dimensional array"
- In reply to: James Dennett: "Re: How to define a const and initialize it?"
- Next in thread: Karl Heinz Buchegger: "Re: How to define a const and initialize it?"
- Reply: Karl Heinz Buchegger: "Re: How to define a const and initialize it?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]