Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why are class static functions not in the scope of the class?

Reply
Thread Tools

Why are class static functions not in the scope of the class?

 
 
Shriramana Sharma
Guest
Posts: n/a
 
      05-03-2013
Hello. Please see the following code:


struct A { static int i ; } ;
int A::* ip = & A::i ;


I get the following errors with GCC 4.7.3 and Clang 3.2:

GCC: error: cannot convert ‘int*’ to ‘int A::*’ in initialization

Clang: error: cannot initialize a variable of type 'int A::*' with an rvalue of type 'int *'

I thought members of a class are in its scope. Reading §9.4 (class.static) of C++11 doesn't help me any since it doesn't seem to speak about the scope of static members. Even §3.3 (basic.scope) and §3.3.7 (basic.scope.class) doesn't seem to help me.

What am I missing? Please help. Thanks.
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      05-03-2013
On 03.05.13 07.01, Shriramana Sharma wrote:
> Hello. Please see the following code:
>
> struct A { static int i ; } ;
> int A::* ip =& A::i ;


First of all you are not talking about functions here. i is a class data
member of A. However, this makes no important difference for the following.

> I get the following errors with GCC 4.7.3 and Clang 3.2:
>
> GCC: error: cannot convert ‘int*’ to ‘int A::*’ in initialization
>
> Clang: error: cannot initialize a variable of type 'int A::*' with an rvalue of type 'int *'


Static class members have different types than class instance members.
This has technical reasons, but also a semantic difference.

The type int A::* may point to any integer member within class A or it's
base classes (if there are any). It will never point to any integer
directly. It is a open instance reference to an integer inside A. I.e.
with int A::* you can access a non-static integer in A using a *class
instance* of type A (or a subclass of A).
It is more like an array index, i.e. take the /4th/ integer in A, than
like a pointer.

If you want to dereference a int A::* you will /always/ need an object
of type A (or subclass) because int A::* only tells /which/ integer of A
to access but not of /which instance of A/.
A a;
a.*ip = 7;

In theory one could define a semantic that also allows int A::* to point
to a static member of A. But you would still need an instance of A to
dereference the pointer, because you can't be sure that it refers only
to a static int. C++ avoids things like this because it would only add
more confusion. Furthermore there would be a significant runtime overhead.

The type int* in contrast may point to any single integer outside a
class or to any static integer in a class or also to any integer of a
class *instance.*

> I thought members of a class are in its scope.


They are, but this only belongs to name resolution and access modifiers.
It doesn't change the type of the objects.


Marcel
 
Reply With Quote
 
 
 
 
Shriramana Sharma
Guest
Posts: n/a
 
      05-03-2013
On Friday, May 3, 2013 12:19:15 PM UTC+5:30, Paavo Helde wrote:
> This is not about scope. The scope part (&A::i) was resolved just fine.
> It is the assignment part which causes problems, as you want to assign
> the result to a variable of a wrong type.
>
> Try
> auto ip = & A::i ;
> or
> int* ip = & A::i ;


Um thanks for your reply people. So IIUC int A::* is valid only within an Aobject, and the layout of A actually affects the contents of the pointer because you've called it an offset from the address location of the first byte of the object. So I should read int A::* as "a pointer within A to an integer" rather than "a point to an integer within A".

Further to your feedback I also tried the following code which gave the following errors:

struct A { int i ; static int si ; } ;
int * ip = & A:: i ; // error: can't convert int A::* to int*
int * sip = & A::si ;
int A::* aip = & A:: i ;
int A::* asip = & A::si ; // error: can't convert int* to int A::*

and I understand it now, sort of...
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      05-03-2013
On 03.05.13 11.49, Shriramana Sharma wrote:
> struct A { int i ; static int si ; } ;
> int * ip =& A:: i ; // error: can't convert int A::* to int*
> int * sip =& A::si ;
> int A::* aip =& A:: i ;
> int A::* asip =& A::si ; // error: can't convert int* to int A::*


Only for completion, there is a third valid variant:

A a;
int * ip2 =& a.i;

In this case ip2 refer to i in exactly this single instance of A. As
anyy other Pionter it gets invalid as soon as the object it refers to
gets invalid.

In Contrast the int A::* references never get invalid because they refer
to a compile time Objekt, the Declaration of i within A. This is similar
to function pointers. They always refer to compile time objects as well
- as long as you don't write self modifying code, of course.


Marcel
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      05-05-2013
On Fri, 03 May 2013 02:49:55 -0700, Shriramana Sharma wrote:

> Um thanks for your reply people. So IIUC int A::* is valid only within
> an A object, and the layout of A actually affects the contents of the
> pointer because you've called it an offset from the address location of
> the first byte of the object. So I should read int A::* as "a pointer
> within A to an integer" rather than "a point to an integer within A".


That is right. The type 'A::*' is called a pointer-to-member (of A), but
it would be more correct to call it a pointer-to-non-static-member (of A).

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows 8 - so bad it's hastening the death of the PC? ~misfit~ NZ Computing 18 04-15-2013 04:15 AM
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments