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

Rationale for Static Member Definition in ISO C++

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

class Fred {
public:
Fred();
...
private:
static int N = 0;
};

it must be defined as

int Fred::N = 0;

* However, a static member function can be defined in-class (or,
inline).

What is(are) the problem(s) with in-class static member variable
initialization? Would appreciate any input and help.

Gary

May 12 '06 #1
11 2779
ziman137 wrote:
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

class Fred {
public:
Fred();
...
private:
static int N = 0;
};

When would the initialisation take place? A static member isn't bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.

--
Ian Collins.
May 12 '06 #2
Ian Collins wrote:
ziman137 wrote:

class Fred {
public:
Fred();
...
private:
static int N = 0;
};
When would the initialisation take place? A static member isn't bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.


Yes, this rationale makes sense. Yet, a static member function isn't
bound to any instance of the class, either. We can still legally define
static member functions as follows,

class Fred {
public:
Fred();
static void do() { //... };
...
};

Reversely we may say that, as defined
class cls
{
...
private:
static int N = 0;
}
in the class, we could also initialize "cls::N" as a global-like
member, recognized by the tag "static". Not quite sure anyway.

Thanks,
Gary

--
Ian Collins.


May 12 '06 #3
ziman137 wrote:
Ian Collins wrote:
ziman137 wrote:
class Fred {
public:
Fred();
...
private:
static int N = 0;
};

When would the initialisation take place? A static member isn't bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.

Yes, this rationale makes sense. Yet, a static member function isn't
bound to any instance of the class, either. We can still legally define
static member functions as follows,

class Fred {
public:
Fred();
static void do() { //... };
...
};

Code, which is no different form any other code and doesn't require
initialisation.
Reversely we may say that, as defined
class cls
{
...
private:
static int N = 0;
}
in the class, we could also initialize "cls::N" as a global-like
member, recognized by the tag "static". Not quite sure anyway.

There still has to one an instance of it somewhere. That somewhere
would be in each compilation unit that sees the class, so you would end
up with multiple instances of cls::N.

If you have in a header (outside of a class),

int N = 0;

each compilation unit that includes the header will have its own locally
scoped N.

If you have

extern int N;

N is a global with only a single instance and a single definition.

A static class member has to have global scope and exist only once, so
the same rules apply as the extern int above. This is a somewhat
counter intuitive use of 'static'.

--
Ian Collins.
May 12 '06 #4

ziman137 wrote:
Ian Collins wrote:
ziman137 wrote:

class Fred {
public:
Fred();
...
private:
static int N = 0;
};

When would the initialisation take place? A static member isn't bound
to any instance of the class.

Where as

static const int N = 0;

Can be initialised at compile time.


Yes, this rationale makes sense. Yet, a static member function isn't
bound to any instance of the class, either. We can still legally define
static member functions as follows,


Think of it this way. A static member variable is essentially an extern
variable that should be defined somewhere, a static member function is
an inline function if defined in the class definition.

May 13 '06 #5
> Yes, this rationale makes sense. Yet, a static member function isn't
bound to any instance of the class, either. We can still legally define
static member functions as follows,

class Fred {
public:
Fred();
static void do() { //... };
...
};


Wow, slow down and think again! Even a non-static member function isn't
bound to any instance of the class. The only difference between a static
member function and a non-static member function is that the latter
takes an extra (hidden) parameter (the this pointer.)

So there is *no* difference in *storage* between a static member
function and a non-static member function.

But there is a storage distinction between a static and a non-static
member variable, right?

Regards,
Ben
May 14 '06 #6
Thanks guys!
Wow, slow down and think again! Even a non-static member function isn't
bound to any instance of the class. The only difference between a static
member function and a non-static member function is that the latter
takes an extra (hidden) parameter (the this pointer.)


Yes, this is a good point. Do you mean that, "this" points to somewhere
partly containing only the stack for local variables, return addresses?
So, for static member function, "this" key word is invalid (true).

May 17 '06 #7

ziman137 wrote:
Thanks guys!
Wow, slow down and think again! Even a non-static member function isn't
bound to any instance of the class. The only difference between a static
member function and a non-static member function is that the latter
takes an extra (hidden) parameter (the this pointer.)

Yes, this is a good point. Do you mean that, "this" points to somewhere
partly containing only the stack for local variables, return addresses?


Yes, for some compilers (gcc) this is passed through stack along with
other arguments.

Consider the output of the following illegal code:

[max@localhost exp]$ cat exp.cpp
#include <stdio.h>

struct S
{
void foo() { printf("%s: this=0x%p\n", __PRETTY_FUNCTION__, this);
}
};

int main()
{
union
{
void(S::*m)();
void(*f)(S*);
} u = { &S::foo };

S s;
s.foo();
u.f(&s);
}

[max@localhost exp]$ g++ --version
g++ (GCC) 4.1.0 20060304 (Red Hat 4.1.0-3)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

[max@localhost exp]$ g++ -Wall -Wextra exp.cpp -o exp

[max@localhost exp]$ ./exp
void S::foo(): this=0x0xbfb8a007
void S::foo(): this=0x0xbfb8a007

May 17 '06 #8
>Maxim Yegorushkin wrote:
Yes, this is a good point. Do you mean that, "this" points to somewhere
partly containing only the stack for local variables, return addresses?


Yes, for some compilers (gcc) this is passed through stack along with
other arguments.

Consider the output of the following illegal code:

[max@localhost exp]$ cat exp.cpp
#include <stdio.h>

struct S
{
void foo() { printf("%s: this=0x%p\n", __PRETTY_FUNCTION__, this);
}
};

int main()
{
union
{
void(S::*m)();
void(*f)(S*);
} u = { &S::foo };

S s;
s.foo();
u.f(&s);
}

[max@localhost exp]$ g++ -Wall -Wextra exp.cpp -o exp
[max@localhost exp]$ ./exp
void S::foo(): this=0x0xbfb8a007
void S::foo(): this=0x0xbfb8a007


This is because the only syntax for pointing to a member function is
"&Class::FunctionName()" - I got it. In above code, if we
change the "foo" to the static, all bets are off with not only "this",
but also any other non-static members. This tells me that, a static
member function is really a global function!(?) May we conclude
as below?

/* a static member is simply a global variable/function, with its class
** name as the scope qualifier - equivalent to a namespace way.
*/

By the way, what's the illegal part of above codes?

Thanks much,
Gary

May 18 '06 #9

ziman137 wrote:
Maxim Yegorushkin wrote:
Yes, this is a good point. Do you mean that, "this" points to somewhere
partly containing only the stack for local variables, return addresses?
Yes, for some compilers (gcc) this is passed through stack along with
other arguments.

Consider the output of the following illegal code:

[max@localhost exp]$ cat exp.cpp
#include <stdio.h>

struct S
{
void foo() { printf("%s: this=0x%p\n", __PRETTY_FUNCTION__, this);
}
};

int main()
{
union
{
void(S::*m)();
void(*f)(S*);
} u = { &S::foo };

S s;
s.foo();
u.f(&s);
}

[max@localhost exp]$ g++ -Wall -Wextra exp.cpp -o exp
[max@localhost exp]$ ./exp
void S::foo(): this=0x0xbfb8a007
void S::foo(): this=0x0xbfb8a007


This is because the only syntax for pointing to a member function is
"&Class::FunctionName()" - I got it. In above code, if we
change the "foo" to the static, all bets are off with not only "this",
but also any other non-static members.


I could not understand the last sentence. Sorry.
This tells me that, a static
member function is really a global function!(?) May we conclude
as below?

/* a static member is simply a global variable/function, with its class
** name as the scope qualifier - equivalent to a namespace way.
*/
Global is not the same as class scope. A static member function is a
function with external linkage within the scope of ist class.
By the way, what's the illegal part of above codes?


Casting a pointer to (nonstatic) member function to a (regular)
function pointer is UB.

May 18 '06 #10
Maxim Yegorushkin wrote:
ziman137 wrote:
This is because the only syntax for pointing to a member function is
"&Class::FunctionName()" - I got it. In above code, if we
change the "foo" to the static, all bets are off with not only "this",
but also any other non-static members.


I could not understand the last sentence. Sorry.


In my last sentence, I mean, if we define "foo()" as a static member

struct S
{
static void foo1() { // ... };
};

in foo() definition body, it is not allowed to access pointer "this"
and any other non-static members. It is not bound to any "S" object -
otherwise there would be unresolvable ambiguity about "this" pointer in
foo() when we have two different "S" objects. In that sense, "foo()" is
encapsulated in class "S" scope ONLY for the sake of its namespace.
When you say, global and class scope is not the same, I agree upon the
sense of namespace. Beyond the namespace, the static "foo()" is
otherwise just like a global function. Would you agree?

Thanks,
Gary

May 18 '06 #11

ziman137 wrote:
Maxim Yegorushkin wrote:
ziman137 wrote:
This is because the only syntax for pointing to a member function is
"&Class::FunctionName()" - I got it. In above code, if we
change the "foo" to the static, all bets are off with not only "this",
but also any other non-static members.


I could not understand the last sentence. Sorry.


In my last sentence, I mean, if we define "foo()" as a static member

struct S
{
static void foo1() { // ... };
};

in foo() definition body, it is not allowed to access pointer "this"
and any other non-static members. It is not bound to any "S" object -
otherwise there would be unresolvable ambiguity about "this" pointer in
foo() when we have two different "S" objects. In that sense, "foo()" is
encapsulated in class "S" scope ONLY for the sake of its namespace.
When you say, global and class scope is not the same, I agree upon the
sense of namespace. Beyond the namespace, the static "foo()" is
otherwise just like a global function. Would you agree?


I would.

May 19 '06 #12

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

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
12
by: elvis_the_king | last post by:
Hi all, I'm still new to C# so maybe someone can explain me the rationale for CS0536: 'someClass' does not implement interface member 'some.Interface.Member()'. 'someClass.Member()' is either...
10
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...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
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?
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.