473,513 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const v static const

53 New Member
Hello, i would kindly like to know what's the differences in between assigning a const to a variable or static const. Also which one is more sufficient.

Especially with in a class, why not just use const, and initialise the members with in the constructor?

i know const variable are compiled into hard coded values in the machine code and that static is stored on the .. stack or something. Anyway i know how it works in programmes.
May 14 '08 #1
2 1400
Banfa
9,065 Recognized Expert Moderator Expert
i know const variable are compiled into hard coded values in the machine code and that static is stored on the .. stack or something. Anyway i know how it works in programmes.
This is only true for global consts (which you shouldn't use) and static const class members.

A const class member could have a different value for every instance of the class (initialised in the constructor) and therefore can not be compiled into hard coded values in the machine code.

And that is the difference, static consts provide the same value for all instances of the class and are more efficient in terms of memory use and speed/size of code produced. consts allow for a different value for every instance of the class but are less efficient as they require memory and processing to access them.
May 14 '08 #2
themadme
53 New Member
ahh yes, that would make sense. Thank you very much for your useful reply
May 20 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3582
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a...
12
3402
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and...
3
2214
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. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at...
4
5406
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside classes is only allowed for integral consts, like static const enum and static const int, not for arrays and structs. It does make sense for an array...
10
21226
by: Dave | last post by:
const static int ARRAY_SIZE = 4; Comeau online gives this warning: "ComeauTest.c", line 10: warning: storage class is not first const static int ARRAY_SIZE = 4; Why is static const preferable to const static?
5
2984
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the occurences. i have been looking at both static and const variables as the options, but couldn't decide on one.
2
2231
by: Adrian | last post by:
Hi, In a header file I tried const char *someval="this is a test"; which is included all over the place and I get linker errors about multiple defines. Why is this not folded when const char someval="this is a test"; is and I get no linker errors?
5
11305
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
7
4001
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const member inside my class. class Packet { static const int session_id;
2
10023
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
0
7270
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...
1
7125
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
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...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
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...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
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
1
813
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.