Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const , static, MACRO

Reply
Thread Tools

const , static, MACRO

 
 
nilavya
Guest
Posts: n/a
 
      11-11-2005
Hi Gurus,

Since I learned C++, way back in 2002, I always had a doubt about the
different types of storage in C++. We have "const", "static" ,
"#define" for one or other purpose. Now I have a eVC++ application
running on WINCE. This application has many classes, some are
initialized dynamically and others statically. I have one class which
has many constant values which does not change. So I have #define 'ed
those variables. But I have read the Code Insection Guidelines, and it
is mentioned that #define shouldnt be used in C++, instead one should
use "const unsigned or signed". What difference does it make in using a
value as "const unsigned" and "#define". How does this affect memory
usage and CPU usage.

Thanks,
With Regards,
Bhagat Nirav K.

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-11-2005
"nilavya" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: Since I learned C++, way back in 2002, I always had a doubt about the
: different types of storage in C++. We have "const", "static" ,
: "#define" for one or other purpose. Now I have a eVC++ application
: running on WINCE. This application has many classes, some are
: initialized dynamically and others statically. I have one class which
: has many constant values which does not change. So I have #define 'ed
: those variables. But I have read the Code Insection Guidelines, and it
: is mentioned that #define shouldnt be used in C++, instead one should
: use "const unsigned or signed". What difference does it make in using a
: value as "const unsigned" and "#define". How does this affect memory
: usage and CPU usage.

On today's compilers, memory or CPU usage will be the same
(for int constants in particular).
The key problem with #define-s is that they do not obey scope rules
(e.g. namespaces, etc). So a const, or even an enum-definition of
constant values, shall be preferred.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-11-2005
* nilavya:
> What difference does it make in using a
> value as "const unsigned" and "#define".


Macros do not respect scopes or types and have limited functionality.

See

<url: http://www.research.att.com/~bs/bs_faq2.html#macro>

and

<url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7>.


> How does this affect memory
> usage and CPU usage.


Usually not at all: that isn't the point.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-11-2005
nilavya wrote:

> Hi Gurus,
>
> Since I learned C++, way back in 2002, I always had a doubt about the
> different types of storage in C++. We have "const", "static" ,
> "#define" for one or other purpose.


#define has nothing at all to do with storage. It's like a simple source
code editor built into your compiler.

> Now I have a eVC++ application running on WINCE. This application has many
> classes, some are initialized dynamically and others statically. I have
> one class which has many constant values which does not change. So I have
> #define 'ed those variables. But I have read the Code Insection
> Guidelines, and it is mentioned that #define shouldnt be used in C++,
> instead one should use "const unsigned or signed".


Right. Don't use #define. Use constants. Whenever possible, avoid using the
preprocessor.

> What difference does it make in using a value as "const unsigned" and
> "#define".


#define just replaces every occurance of the specified name with what you
write behind it on source code level, i.e. before compiling it actually
starts.
#define doesn't care about namespaces or scope, so your macros will clutter
up all of them. You can't take the address of it.

> How does this affect memory usage and CPU usage.


With a decent compiler, it won't make a significant difference.
Well, in some cases, a constant might be faster.

 
Reply With Quote
 
nilavya
Guest
Posts: n/a
 
      11-11-2005
thanks for the Quick replies.

I got the difference between #define and const.

One final question, I have one class, and there are some variables
which has specific values which does not change. So do I need to
declare that variables inside the class as constant or outside the
class. AFAIK if I declare it outside it will be global, which is not a
good design. For example...

class CTemp
{
const unsigned nSomeValue = 100;
void SomeFunction();
};

const unsigned nSomeValue = 100;
class CTemp
{
void SomeFunction();
};

And my class CTemp, is dynamically allocated when application starts,
and is deleted when application is exited. Can any one throw some
lights on it.. Please...

Thanks,
With Regards.
Bhagat Nirav K.

 
Reply With Quote
 
suresh
Guest
Posts: n/a
 
      11-11-2005
nilavya wrote:
> thanks for the Quick replies.
>
> I got the difference between #define and const.
>
> One final question, I have one class, and there are some variables
> which has specific values which does not change. So do I need to
> declare that variables inside the class as constant or outside the
> class. AFAIK if I declare it outside it will be global, which is not a
> good design. For example...
>
> class CTemp
> {
> const unsigned nSomeValue = 100;
> void SomeFunction();
> };
>


It depends on how the nSomeValue related to the class. If it is bound
to the class and not to the object (ie. It is going to be constant for
all the objects of that class) then use "static const". And the
syntax for initializing this is little different.
If the nSomeValue is bound to each object (ie. It is constant for an
object but can be different for different objects) then use "const"
as you have declared and should be initialized during construction of
the object.


> const unsigned nSomeValue = 100;
> class CTemp
> {
> void SomeFunction();
> };
>


This is not a good idea unless it is not at all related to the class.
(also prefer to use static if you are looking for symbalic constant
equalant - otherwise it will occupy some memory)

You can also use enums when you have list of realted symbalic
constants.

> And my class CTemp, is dynamically allocated when application starts,
> and is deleted when application is exited. Can any one throw some
> lights on it.. Please...
>
> Thanks,
> With Regards.
> Bhagat Nirav K.


 
Reply With Quote
 
nilavya
Guest
Posts: n/a
 
      11-11-2005
Thanks for the Extremely Fast Reply.

Thanks,
With Regards,
Bhagat Nirav K.

 
Reply With Quote
 
Bronek Kozicki
Guest
Posts: n/a
 
      11-11-2005
nilavya <(E-Mail Removed)> wrote:
> class CTemp
> {
> const unsigned nSomeValue = 100;

^
you need to put "static" keyword here, otherwise it will not compile.


B.




 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      11-11-2005
In article <(E-Mail Removed) .com>,
nilavya <(E-Mail Removed)> wrote:
>Hi Gurus,
>
>Since I learned C++, way back in 2002, I always had a doubt about the
>different types of storage in C++. We have "const", "static" ,
>"#define" for one or other purpose. Now I have a eVC++ application
>running on WINCE. This application has many classes, some are
>initialized dynamically and others statically. I have one class which
>has many constant values which does not change. So I have #define 'ed
>those variables. But I have read the Code Insection Guidelines, and it
>is mentioned that #define shouldnt be used in C++, instead one should
>use "const unsigned or signed". What difference does it make in using a
>value as "const unsigned" and "#define". How does this affect memory
>usage and CPU usage.


This may not answer every question you have, but have a look at

http://www.comeaucomputing.com/techtalk/#definevsconst
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments