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 4 2198
On Wed, 07 Apr 2004 18:37:55 +0800, Hunter Hou <hy***@lucent.c om>
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
On Wed, 07 Apr 2004 18:37:55 +0800, Hunter Hou <hy***@lucent.c om>
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
Hunter Hou <hy***@lucent.c om> 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
Hunter Hou <hy***@lucent.c om> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 here</b><a href="http://www.cnn.com">click me</a>
... then the output should be this:
|
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;
$this->key = 'id';
echo "$this->key is $this->field"; // prints "id is Array"
// Thus I am forced to do this
$keyval = $this->field;
|
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" name="filesToDelete">
<input type="text" name="filesToDelete">
<input type="text" name="filesToDelete">
</form>
|
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 group. Rather than make them group writable where in anybody could make a script and write to my files i would like to make them setuid. I tried making a c prog with the setuid function. I used chmod and made it setuid. I called it with the system...
|
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:
<?
| |
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
|
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 line 28
|
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 converting a site of mine to PHP-Nuke, but if
the individual modules aren't picked up in search engines I'm not
going to do it.
Thanks
phpKid
|
by: lawrence |
last post by:
What is the PHP equivalent of messaging, as in Java?
|
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 state that a
function cannot be undefined and adding "exit" to the included page doesn't
free it up either.
Any suggestions?
B44CCD21
|
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...
| |
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,...
|
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...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |