473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

const , static, MACRO

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
8 2366
"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
* 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
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
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
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
Thanks for the Extremely Fast Reply.

Thanks,
With Regards,
Bhagat Nirav K.

Nov 11 '05 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
19
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
4
by: Pelle Beckman | last post by:
Hi, I saw this code in an earlier post (not that there's anything wrong with it) #include <iostream> using std::cout; const int hour = 3600; const int min = 60;
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
3
by: arut | last post by:
I would like to know when a const should be used and when a #define is necessary in a program using a constant. What are the pros and cons?
9
by: Chris Pearl | last post by:
Are there Python tools to help webmasters manage static websites? I'm talking about regenerating an entire static website - all the HTML files in their appropriate directories and...
5
by: 2b|!2b==? | last post by:
I'm getting this rather cryptic com,pile time error when I compile my code. I have the following lines in my header file I know I could use anonymous namespaces to get around this but I was being...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.