473,503 Members | 3,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static inline member function

so if i have this:

// header.h
class X {
public:
static int s = 1;
};

i still need this:

// source.cpp
int X::s;

right?

what about this:

class X {
public:
static int S() { return 1; }
};

how come static member needs definition outside the class, but static member
can be inlined? i don't quite understand how that is allowed. and why.

please explain
Oct 20 '05 #1
5 4263

"Martin Vorbrodt" <mv*******@gmail.com> wrote in message
news:Jo*****************@news-wrt-01.rdc-nyc.rr.com...
so if i have this:

// header.h
class X {
public:
static int s = 1;
};

i still need this:

// source.cpp
int X::s;

right?
Yes.

what about this:

class X {
public:
static int S() { return 1; }
};

how come static member needs definition outside the class, but static
member can be inlined?
Static data members occur only once, not per
object instance. When you create an object,
memory for all the non-static data members is
alloted, for each object. IOW the memory is
'inside' the objects. The static member occurs
only once. So you need to provide it somewhere.

Also, note that the concept of 'inline' really
only has meaning for functions, not data.

No matter how many objects you create, there is
still only one copy of each member function, whether
'inlined' or not. 'inline' (specified by either
putting a function definition inside a class body,
or outside it with the 'inline' keyword, is a 'hint'
to the compiler to try to insert the function body
at the point of call, rather than have it 'really'
call and return. But note that a compiler is free
to honor or ignore such a 'hint'.

i don't quite understand how that is allowed. and why.


I hope my explanation helped.

-Mike
Oct 20 '05 #2
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:6H*****************@newsread2.news.pas.earthl ink.net...

"Martin Vorbrodt" <mv*******@gmail.com> wrote in message
news:Jo*****************@news-wrt-01.rdc-nyc.rr.com...
so if i have this:

// header.h
class X {
public:
static int s = 1;
};

i still need this:

// source.cpp
int X::s;

right?
Yes.


so putting int X::s in header file right after class definition, and later
including that file more than once, could yeld link errors?

what about this:

class X {
public:
static int S() { return 1; }
};

how come static member needs definition outside the class, but static
member can be inlined?
Static data members occur only once, not per
object instance. When you create an object,
memory for all the non-static data members is
alloted, for each object. IOW the memory is
'inside' the objects. The static member occurs
only once. So you need to provide it somewhere.


so why isn't "static int s = 1;" declared (and defined?) inside the class
enough? why must i put it in CPP file?

Also, note that the concept of 'inline' really
only has meaning for functions, not data.

No matter how many objects you create, there is
still only one copy of each member function, whether
'inlined' or not. 'inline' (specified by either
putting a function definition inside a class body,
or outside it with the 'inline' keyword, is a 'hint'
to the compiler to try to insert the function body
at the point of call, rather than have it 'really'
call and return. But note that a compiler is free
to honor or ignore such a 'hint'.

i don't quite understand how that is allowed. and why.


I hope my explanation helped.

-Mike

Oct 21 '05 #3

"Martin Vorbrodt" <mv*******@gmail.com> wrote in message
news:ew****************@news-wrt-01.rdc-nyc.rr.com...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:6H*****************@newsread2.news.pas.earthl ink.net...

"Martin Vorbrodt" <mv*******@gmail.com> wrote in message
news:Jo*****************@news-wrt-01.rdc-nyc.rr.com...
so if i have this:

// header.h
class X {
public:
static int s = 1;
};

i still need this:

// source.cpp
int X::s;

right?
Yes.


so putting int X::s in header file right after class definition, and later
including that file more than once, could yeld link errors?


Yes, it would violate the 'one definition rule' (ODR)

what about this:

class X {
public:
static int S() { return 1; }
};

how come static member needs definition outside the class, but static
member can be inlined?
Static data members occur only once, not per
object instance. When you create an object,
memory for all the non-static data members is
alloted, for each object. IOW the memory is
'inside' the objects. The static member occurs
only once. So you need to provide it somewhere.


so why isn't "static int s = 1;" declared (and defined?)


It's not defined, only declared.

inside the class enough?
Because it's only a declaration, not a definition.
The initializer is allowed as a 'special case'
for static constant integer types (which means
-- I didn't notice it before -- that what you
had is not valid, since it's not const).
why must i put it in CPP file?


Because it still needs to be defined (storage
allotted for it).

-Mike
Oct 21 '05 #4
On Fri, 21 Oct 2005 00:21:30 +0000, Martin Vorbrodt wrote:

so why isn't "static int s = 1;" declared (and defined?) inside the class
enough? why must i put it in CPP file?


You can't do what that line above says. You can only initialize static
*const* integral types.

If it was that case that you did have "static const", then you may *not*
have to put it in the cpp file, assuming you don't take its address or do
anything else funky with it, and assuming the compiler uses the const
value directly.

- Jay
Oct 21 '05 #5
"Jay Nabonne" <ja*********@sonicNOSPAM.com> wrote in message
news:pa****************************@sonicNOSPAM.co m...
On Fri, 21 Oct 2005 00:21:30 +0000, Martin Vorbrodt wrote:

so why isn't "static int s = 1;" declared (and defined?) inside the class
enough? why must i put it in CPP file?
You can't do what that line above says. You can only initialize static
*const* integral types.

If it was that case that you did have "static const", then you may *not*
have to put it in the cpp file, assuming you don't take its address or do
anything else funky with it, and assuming the compiler uses the const
value directly.


According to the standard:

If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify a constant initializer which
shall be an integral constant expression (5.19). In that case, the member
can appear in integral constant expressions within its scope. The member
shall still be defined in a namespace scope if it is used in the program and
the namespace scope definition shall not contain an initializer.

sooo... you can give it a value in the calss definition and use it in
constant int* expressions (expressions evaluated at compile time, right?),
but apparently you still need to put it in some CPP file to allocate space
for it? am i reading this right (english is my 2nd language, and standard
ain't plain vanilla english:) )

so what do you mean by not doing funky things to it? not using it in any
other way than in const integral expressions?

- Jay

Oct 21 '05 #6

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

Similar topics

1
2016
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
7
1865
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
11
2795
by: ziman137 | last post by:
Hi all, I have a question here. What is the rationale behind ISO C++ for Static Member Definition? * ISO C++ forbids in-class definition/initialization of non-constant static member...
5
3583
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
14
2551
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...
5
2846
by: Hong Ye | last post by:
Traits is a useful template technique to simplfy the implementation of some classes. however, I met some questions when I tried to understand and implement it. Following is an example of traits...
2
3762
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
15
7837
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?
17
8352
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
7192
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
7064
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
7261
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
7315
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...
1
6974
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...
0
7445
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...
0
5559
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,...
0
4665
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...
0
3158
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...

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.