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

Home Posts Topics Members FAQ

Static variable initialisation. (easy)

Hi,

If I have this in a header file:

//Part of the class myClass:
static int myVar=99;

but then in the source file have this:

int myClass::myVar= 77;

which one takes priority?

Thank you,
Q
Oct 11 '06 #1
7 1651
Quantum wrote:
If I have this in a header file:

//Part of the class myClass:
static int myVar=99;
Not allowed in C++. Non-const static members cannot be
initialised in the class definition.
>
but then in the source file have this:

int myClass::myVar= 77;

which one takes priority?
None. The program is ill-formed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #2
Victor Bazarov wrote:
Quantum wrote:
>If I have this in a header file:

//Part of the class myClass:
static int myVar=99;

Not allowed in C++. Non-const static members cannot be
initialised in the class definition.
>but then in the source file have this:

int myClass::myVar= 77;

which one takes priority?

None. The program is ill-formed.

V

"Not allowed in C++. Non-const static members cannot be initialised in
the class definition."
Yep, right enough... :) I thought there was something weird going on,
but MSVC2K5 was compiling it. I've just cleaned and rebuilt the solution
and it now complains!!! Hmm.... Anyways, thanks for the quick reply! (:

Q
Oct 11 '06 #3
Quantum wrote:
Victor Bazarov wrote:
>Quantum wrote:
>>If I have this in a header file:

//Part of the class myClass:
static int myVar=99;

Not allowed in C++. Non-const static members cannot be
initialised in the class definition.
>>but then in the source file have this:

int myClass::myVar= 77;

which one takes priority?

None. The program is ill-formed.

V


"Not allowed in C++. Non-const static members cannot be initialised
in the class definition."
Yep, right enough... :) I thought there was something weird going on,
but MSVC2K5 was compiling it. I've just cleaned and rebuilt the
solution and it now complains!!! Hmm.... Anyways, thanks for the
quick reply! (:
Just a piece of additional information: if a static integral constant
is initialised in the class definition (like this

static const int myVar = 99;

then the definition outside of the class (if you need one), shall NOT
have an initialiser:

const int myClass::myVar;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #4
Victor Bazarov wrote:
Quantum wrote:
>Victor Bazarov wrote:
>>Quantum wrote:
If I have this in a header file:

//Part of the class myClass:
static int myVar=99;
Not allowed in C++. Non-const static members cannot be
initialised in the class definition.

but then in the source file have this:

int myClass::myVar= 77;

which one takes priority?
None. The program is ill-formed.

V

"Not allowed in C++. Non-const static members cannot be initialised
in the class definition."
Yep, right enough... :) I thought there was something weird going on,
but MSVC2K5 was compiling it. I've just cleaned and rebuilt the
solution and it now complains!!! Hmm.... Anyways, thanks for the
quick reply! (:

Just a piece of additional information: if a static integral constant
is initialised in the class definition (like this

static const int myVar = 99;

then the definition outside of the class (if you need one), shall NOT
have an initialiser:

const int myClass::myVar;

V
Just curious... when would you need a definition outside of the class?
Oct 11 '06 #5
Mark P wrote:
Victor Bazarov wrote:
>[static const integral data members need a definition]

Just curious... when would you need a definition outside of the class?
When you take address of that variable, for example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 12 '06 #6
Victor Bazarov wrote:
Mark P wrote:
>Victor Bazarov wrote:
>>[static const integral data members need a definition]
Just curious... when would you need a definition outside of the class?

When you take address of that variable, for example.
I see, so may I then infer from your answer that a static const defined
only within a class does not actually get allocated in memory? Is it
simply replaced by the compiler with its integral value?
Oct 12 '06 #7
Mark P wrote:
Victor Bazarov wrote:
>Mark P wrote:
>>Victor Bazarov wrote:
[static const integral data members need a definition]
Just curious... when would you need a definition outside of the
class?

When you take address of that variable, for example.

I see, so may I then infer from your answer that a static const
defined only within a class does not actually get allocated in
memory? Is it simply replaced by the compiler with its integral
value?
That's generally true for any integral constant, i.e. not necessarily
a member of a class, however, I am not sure there is a requirement to
that extend in the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 12 '06 #8

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

Similar topics

12
5469
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can initialize the static object without invoking the constructor? Below is the sample coding. Header file ASURegistrationManager.h #include "ASURegistration.h"
8
2532
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class static variables) is initializated (constructed) before main is invoked . . ." .. . .
6
2360
by: clilley | last post by:
The following code causes a segmentation fault on DEC Tru64: foo.cc (built into libFoo.so) //--------------------------- include <iostream> bool createFoo() { std::cout << "createFoo" << std::endl; }
7
12453
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have blindly searched for the answer but I still not thoroughly
3
6381
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
14
2569
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
10
4199
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: int get_x();
10
2647
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a static class member is that you cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file...
1
2699
by: Anonymous | last post by:
I want to carry out initialisation ONCE for use by a class. I am using a dummy static variable dummy to do this. However, during my initialisation code, I am allocating memory etc for various variables used by the class. Although I am not explicitly deallocating memory, alloc'd memory will be returned to the heap when the app terminates - but I am not happy with this. I am looking for a way to explicitly deallocate memory when there...
0
8678
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
8609
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
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
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
7737
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...
1
6525
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
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
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
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...

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.