473,698 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it really necessery to replace #define from const?

I have got some project src, and find it out that there is no ppl using
"const" to define a constance, but using #define still more. Discussing
with my friend, he said 1. #define is much easier in modify values in
program for several propose, and 2. const takes memory but #define not.
However, most modern textbook (for example, C++ Primal Plus 4/e) still
suggest to use const to replace #define. I know that use #define will
have some problem when using as macro, but that affairs to function, not
const defination. anyone have idea why using const instead of using
#define ? any advantage?
Jul 23 '05 #1
4 2246
On Sun, 20 Mar 2005 19:34:22 +0800, Rayer wrote:
const defination. anyone have idea why using const instead of using
#define ? any advantage?


This is covered by the FAQ.

http://www.parashift.com/c++-faq-lit....html#faq-29.7

Jul 23 '05 #2
Compiler may optimize it so it is as effective as #define
Jul 23 '05 #3
"Compiler may optimize", I am afraid may is the keyword...
quite often does not, especially for globals (like PI).

That being said, majority still should use const.

igor
"qfel" <[q_tmp]@[aster.pl]> wrote in message
news:42******** **************@ mamut2.aster.pl ...
Compiler may optimize it so it is as effective as #define

Jul 23 '05 #4
Rayer wrote:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in modify values in
program for several propose,
Eh? That doesn't make sense in English. I can't guess what it means
so it's hard to reply.
and 2. const takes memory but #define not.
Wrong. Both take memory if needed, the compiler can optimize the
memory in both cases if not needed.
Of course, it is possible to take the address of a const, which
means the const will take memory. That's just an extra feature.
However, most modern textbook (for example, C++ Primal Plus 4/e) still suggest to use const to replace #define. I know that using #define will have some problem when using as macro, but that applies to function, not const defination. anyone have idea why using const instead of using
#define ? any advantage?


Sure. E.g. a const has a type. i.e. const std::string foo = "X";
#define FOO "X" doesn't. The result is that passing foo to
void bar( std::string const& ) is cheaper. Every time you call
bar(FOO) the compiler has to convert FOO to a string, foo is
converted only once.

Overloading is more obvious, as it too works on types. Function
templates that deduce types are also affected, even those as
simple as std::find()

Furthermore, you can have consts in functions, and they will obey
function scope. A #define continues for the rest of the file.

Basically, C++ was designed with const in mind. #define was
accepted in C++ only to keep old C code working, and it
doesn't play nice with the new features.

HTH,
Michiel Salters

Jul 23 '05 #5

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

Similar topics

2
6986
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the delete operator with a macro. I can't replace the delete operator by using void* as the first parameter, because then my code will not be able to modify the calling function's pointer, nor would it get the source file name and line number.
13
11017
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code below would work... string gsub(const string & sData, const string & sFrom,
5
2718
by: Michael Hiegemann | last post by:
Hello, I am unaware whether this is the right group to ask. Please point me to another forum if required. I would like to replace a Fortran function by one which is written in C. The function shall provide the same interface for the fortran code calling the library. Fortunately, this works - currently under WinNT - well for all functions which do not return a string, e.g.
7
20970
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above can't filter it. I tried to do this text= Replace(text, "““","'")
18
4618
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
4
1796
by: Marcelo | last post by:
Dear all, I am a beginner of C/C++ programmation and I don't understand how the #define command works. I would like to make this statement #define MAX2(V1, V2) (V1>V2 ? V1 : V2) as a C routine in order to use it in other C programs. However, I don't know what type to put for the retourning value.
2
3639
by: satya0674 | last post by:
Hi, I want to know whether is it necessery to put the sorce address in the arp request packet . as we r getting the MAC address in the datalink layer ,this is due to the broadcast of physical address.and corresponding machine will reply to that request .communication wiill be done between DLL to DLL(data link layer). And there is no link of Data link layer to IP layer. so ther is no need of source address as we require only the...
5
3228
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace #define with const. example: 2 header file StringGrid1.h StringGrid2.h
17
5812
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8862
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.