473,385 Members | 2,274 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,385 software developers and data experts.

const Vs static

Ant
Hi,

I'm having trouble trying to define the difference between declaring a field
in a class const or static. i've noted that when referencing them from the
class that a const is symbolized with a field symbol & a static has a blue
box. What is the difference? What is the blue box? Has it got to do with
scope or get/setability?

Thanks for your thoughts in advance

Ant
Nov 17 '05 #1
4 14419
Ant <An*@discussions.microsoft.com> wrote:
I'm having trouble trying to define the difference between declaring a field
in a class const or static. i've noted that when referencing them from the
class that a const is symbolized with a field symbol & a static has a blue
box. What is the difference? What is the blue box? Has it got to do with
scope or get/setability?


The value of a const is compiled into any class which uses it, rather
than referencing the value at runtime. The potentially makes it more
efficient, but at the cost that if they're in different assemblies and
you change the value of the const, you have to recompile the other
assembly in order to see the change.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Hi Ant,

a const has a value, that is known at compiletime. That value can't change
while runtime and
can be part of a constant expression.
static is a member, that belongs to a class/struct not to an instance of
that class/struct.
A static field is a variable that exists once per application, whil instance
variables exist once per instance.
The value of a static variable can change during runtime.

Christof

"Ant" <An*@discussions.microsoft.com> schrieb im Newsbeitrag
news:98**********************************@microsof t.com...
Hi,

I'm having trouble trying to define the difference between declaring a
field
in a class const or static. i've noted that when referencing them from the
class that a const is symbolized with a field symbol & a static has a blue
box. What is the difference? What is the blue box? Has it got to do with
scope or get/setability?

Thanks for your thoughts in advance

Ant

Nov 17 '05 #3
Ant <An*@discussions.microsoft.com> wrote:
I'm having trouble trying to define the difference between declaring a field
in a class const or static. i've noted that when referencing them from the
class that a const is symbolized with a field symbol & a static has a blue
box. What is the difference? What is the blue box? Has it got to do with
scope or get/setability?


The value of a const is compiled into any class which uses it, rather
than referencing the value at runtime. The potentially makes it more
efficient, but at the cost that if they're in different assemblies and
you change the value of the const, you have to recompile the other
assembly in order to see the change.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Hi Ant,

a const has a value, that is known at compiletime. That value can't change
while runtime and
can be part of a constant expression.
static is a member, that belongs to a class/struct not to an instance of
that class/struct.
A static field is a variable that exists once per application, whil instance
variables exist once per instance.
The value of a static variable can change during runtime.

Christof

"Ant" <An*@discussions.microsoft.com> schrieb im Newsbeitrag
news:98**********************************@microsof t.com...
Hi,

I'm having trouble trying to define the difference between declaring a
field
in a class const or static. i've noted that when referencing them from the
class that a const is symbolized with a field symbol & a static has a blue
box. What is the difference? What is the blue box? Has it got to do with
scope or get/setability?

Thanks for your thoughts in advance

Ant

Nov 17 '05 #5

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

Similar topics

2
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual...
10
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...
2
by: Susan Baker | last post by:
Hi, I got this error msg whilst building some classes. It is realatively asy to fix. But I just wondered, does anyone know the technical reason why one can't initialiaze a const static...
8
by: nilavya | last post by:
Hi Gurus, Since I learned C++, way back in 2002, I always had a doubt about the different types of storage in C++. We have "const", "static" , "#define" for one or other purpose. Now I have a...
3
by: Russell Mangel | last post by:
Hi, I would like to encapsulate the following CRC32 routine inside a C++ un-mananged class. I am having difficulty initializing the "const static unsigned int table" inside an un-managed C++...
2
by: psujkov | last post by:
Hi everybody, let us have some class A with const static int variable var with compile-time well-known value. let us have some function f(), which has return type of A. and let us have some...
9
by: t | last post by:
Lippman's C++ Primer says that if you initialize a const static data member in a class, you still need to define the member outside the class but without an initializer. I tried it using Visual...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
5
by: Bob Doe | last post by:
I have a const static object. What is the right syntax to get a reference to the std::vector within the std::map variable?: class MyObj { public: ... std::map<std::string,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.