By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

const , static, MACRO

P: n/a
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.

Nov 11 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"nilavya" <ni*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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
Nov 11 '05 #2

P: n/a
* 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?
Nov 11 '05 #3

P: n/a
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.

Nov 11 '05 #4

P: n/a
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.

Nov 11 '05 #5

P: n/a
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.


Nov 11 '05 #6

P: n/a
Thanks for the Extremely Fast Reply.

Thanks,
With Regards,
Bhagat Nirav K.

Nov 11 '05 #7

P: n/a
nilavya <ni*****@gmail.com> wrote:
class CTemp
{
const unsigned nSomeValue = 100;

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


Nov 11 '05 #8

P: n/a
In article <11**********************@g43g2000cwa.googlegroups .com>,
nilavya <ni*****@gmail.com> 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?
Nov 11 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.