473,385 Members | 1,732 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.

in-class initialization

Folks,

If a member variable is initialized in class, it must be static and
const. I know this and regard it as a convention. But who knows why
standard define this? What's the advangtage or otherwise shortcomings?

For example,

class C {

const int a = 0;
static int a = 0;

// Must be like this:

static const int a = 0;

// or as an enum
};
Thanks,
Hunter
Jul 22 '05 #1
4 2173
On Wed, 07 Apr 2004 18:37:55 +0800, Hunter Hou <hy***@lucent.com>
wrote:
Folks,

If a member variable is initialized in class, it must be static and
const. I know this and regard it as a convention. But who knows why
standard define this?


Integral const expressions are a special case - they can be used as
template parameters, array bounds, etc. No other const type has this
special aspect - there's nothing you can do with a const float with a
known compile time value (such as a float literal, 5.0) that you can't
do with one where the value is known only at runtime. So it is
potentially useful for static integral const members of classes to be
integral const expressions, and for this they have to be defined
inside the class definition.

As for static, what is the purpose of a non static const that you
initialize in the class declaration? All instances will have the same
value for that variable, so it may as well be static.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
On Wed, 07 Apr 2004 18:37:55 +0800, Hunter Hou <hy***@lucent.com>
wrote:
Folks,

If a member variable is initialized in class, it must be static and
const. I know this and regard it as a convention. But who knows why
standard define this?


Integral const expressions are a special case - they can be used as
template parameters, array bounds, etc. No other const type has this
special aspect - there's nothing you can do with a const float with a
known compile time value (such as a float literal, 5.0) that you can't
do with one where the value is known only at runtime. So it is
potentially useful for static integral const members of classes to be
integral const expressions, and for this they have to be defined
inside the class definition.

As for static, what is the purpose of a non static const that you
initialize in the class declaration? All instances will have the same
value for that variable, so it may as well be static.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
Hunter Hou <hy***@lucent.com> wrote in message news:<c5********@netnews.proxy.lucent.com>...
Folks,

If a member variable is initialized in class, it must be static and
const. I know this and regard it as a convention. But who knows why
standard define this? What's the advangtage or otherwise shortcomings?

For example,

class C {

const int a = 0; This can't be used because you cannont initialise members in the class
definition. The best place for this is in the initialisation list of
the class constructor eg:
C:C () : a(0)
{}
static int a = 0; This won't work because each time the class is insantiated the
definition is reference, however if "a" is static ie global across
instances of that class then it may be changed somewhere else by
another instance rendering such an initialisation upon each reference
invalid.
// Must be like this:

static const int a = 0; This works because it is a static const, "a" will be instantiated
(before even the class is instantiated unless that is static also)
just once and will not change across instances of the class and will
always be 0
// or as an enum
};
Thanks,
Hunter

Jul 22 '05 #4
Hunter Hou <hy***@lucent.com> wrote in message news:<c5********@netnews.proxy.lucent.com>...
Folks,

If a member variable is initialized in class, it must be static and
const. I know this and regard it as a convention. But who knows why
standard define this? What's the advangtage or otherwise shortcomings?

For example,

class C {

const int a = 0; This can't be used because you cannont initialise members in the class
definition. The best place for this is in the initialisation list of
the class constructor eg:
C:C () : a(0)
{}
static int a = 0; This won't work because each time the class is insantiated the
definition is reference, however if "a" is static ie global across
instances of that class then it may be changed somewhere else by
another instance rendering such an initialisation upon each reference
invalid.
// Must be like this:

static const int a = 0; This works because it is a static const, "a" will be instantiated
(before even the class is instantiated unless that is static also)
just once and will not change across instances of the class and will
always be 0
// or as an enum
};
Thanks,
Hunter

Jul 22 '05 #5

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

Similar topics

3
by: Curious Expatriate | last post by:
Hi- I'm completely stumped. I'm trying to write some code that will parse a file and rewrite it with all URLs replaced by something else. For example: if the file looks like this: <b>click...
1
by: JS Bangs | last post by:
I started using PHP's object-oriented stuff a little while ago, which has mostly been a joy. However, I've noticed that they don't seem to echo as I would like. Eg: $this->field = 255;...
5
by: lawrence | last post by:
I've waited 6 weeks for an answer to my other question and still no luck, so let me rephrase the question. I know I can do this: <form method="post" action="$self"> <input type="text"...
0
by: Ben Eisenberg | last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
1
by: James | last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP ? Where ID = $ID ! Any examples or URLS ? Thanks
1
by: Patrick Schlaepfer | last post by:
Why this code is not working on Solaris 2.8 host. Always getting: PHP Fatal error: swfaction() : getURL('http://www.php.net' ^ Line 1: Reason: 'syntax error' in /.../htdocs/ming2.php on...
1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
3
by: Quinten Carlson | last post by:
Is there a way to conditionally define a function in php? I'm trying to run a php page 10 times using the include statement, but I get an error because my function is already defined. The docs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...

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.