const , static, MACRO

N

nilavya

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.
 
I

Ivan Vecerina

: 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
 
R

Rolf Magnus

nilavya said:
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.
 
N

nilavya

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.
 
S

suresh

nilavya said:
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.
 
G

Greg Comeau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top