'const' saves space??

Q

qazmlp

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

Rolf Magnus

qazmlp said:
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.
 
M

Malcolm

#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.
 
C

Chris Torek

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

Josephine Schafer

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.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top