473,654 Members | 3,035 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 4280

"Martin Vorbrodt" <mv*******@gmai l.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******@mkwah ler.net> wrote in message
news:6H******** *********@newsr ead2.news.pas.e arthlink.net...

"Martin Vorbrodt" <mv*******@gmai l.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*******@gmai l.com> wrote in message
news:ew******** ********@news-wrt-01.rdc-nyc.rr.com...
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:6H******** *********@newsr ead2.news.pas.e arthlink.net...

"Martin Vorbrodt" <mv*******@gmai l.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*********@so nicNOSPAM.com> wrote in message
news:pa******** *************** *****@sonicNOSP AM.com...
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
2027
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 work. The error states: Object.h(16): error C2662: 'DataLib::Object::_type' : cannot convert 'this' pointer from 'const DataLib::Object' to 'DataLib::Object &' namespace DataLib
7
1875
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 constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
11
2822
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 variables. For example, instead of
5
3599
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 fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
14
2567
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:
5
2858
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 template with specializations (copied from Nathan C. Myer's 1995 article): template <class numT> struct float_traits { }; struct float_traits<float{
2
3767
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 common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
15
7854
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
8367
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
8376
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
8290
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
8815
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, 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...
0
8708
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
8594
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...
1
6161
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1596
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.