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

'const' saves space??

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

P: n/a

"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

P: n/a
>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 (4039.22'N, 11150.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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.