473,386 Members | 1,827 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,386 software developers and data experts.

'const' saves space??

As I understand, there will be 100 copies of "myTestClass", when
CLASSNAME is declared as below & it is used 100 times in the code:
#define CLASSNAME "myTestClass"

But, there will be only 1 copy of "myTestClass", when
CLASSNAME is declared as below irrespective of the number of times
CLASSNAME is used:
const char* CLASSNAME = "myTestClass" ;

Is my understanding correct ? Isn't that an advantage of 'const'
over '#define' declaration for constants?
Jul 19 '05 #1
4 2819
qazmlp wrote:
As I understand, there will be 100 copies of "myTestClass", when
CLASSNAME is declared as below & it is used 100 times in the code:
#define CLASSNAME "myTestClass"
Basically yes, though some compilers optimize those copies away, at
least if there are several of them in one translation unit.
But, there will be only 1 copy of "myTestClass", when
CLASSNAME is declared as below irrespective of the number of times
CLASSNAME is used:
const char* CLASSNAME = "myTestClass" ;

Is my understanding correct?
Yes.
Isn't that an advantage of 'const' over '#define' declaration for
constants?


You should always prefer consts over #defines in C++, since there are
other advantages (e.g. they can be used in namespaces and classes) and
no or very few disadvantages.

Jul 19 '05 #2

"qazmlp" <qa********@rediffmail.com> wrote in message
#define CLASSNAME "mytestclass"
vs const char *CLASSNAME = "mytestclass".
Is my understanding correct ? Isn't that an advantage of 'const'
over '#define' declaration for constants?

Yes. #define is essentially a word-processing command, and most compilers
will just insert string literals into your source.
However unless the strings are very long this is unlikely to be much of a
problem - programs don't generally run out of storage space for things like
embedded string literals.
Jul 19 '05 #3
>qazmlp wrote:
Isn't that an advantage of 'const' over '#define' declaration for
constants?

In article <bh*************@news.t-online.com>
Rolf Magnus <ra******@t-online.de> writes:You should always prefer consts over #defines in C++, since there are
other advantages (e.g. they can be used in namespaces and classes) and
no or very few disadvantages.


Yes -- but note that the original question was posted to both
comp.lang.c *and* comp.lang.c++. The "const" keyword has very
different meanings in the two languages (basically, in C++, it
"works right" and does what people expect, while in C, it does
something nobody expects until they mentally substitute "read-only
variable" for the word "const").

The original poster should decide which language he wishes to write
in, and stick with that. It *is* possible to write programs that
are not only syntactically correct, but also even have the same
semantics, in both languages (note that this is harder than it
looks at first). But the result is usually horrible C code and
even worse C++ code. In other words, you can write bad code that
just barely works in both languages, or good code that works only
in one language -- so pick one, and write good code in that language.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 19 '05 #4
Isn't that an advantage of 'const' over '#define' declaration for
constants?


You should always prefer consts over #defines in C++, since there are
other advantages (e.g. they can be used in namespaces and classes) and
no or very few disadvantages.


Just to add over the advantages of using consts -
1.Benefit of type checking.
2.Preventing preprocessor from finding bugs. (Debugging becomes more
intuitive).
3. consts can be scoped.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
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...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
3
by: qazmlp | last post by:
As I understand, there will be 100 copies of "myTestClass", when CLASSNAME is declared as below & it is used 100 times in the code: #define CLASSNAME "myTestClass" But, there will be only 1 copy...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
3
by: rwf_20 | last post by:
Hi, I'm looking at the differences between: const NonTrivialObject& obj = functionThatReturnsANonTrivialObjectByValue(); and: const NonTrivialObject obj =
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.