3

Possible Duplicate:
When do I use a dot, arrow, or double colon to refer to members of a class in C++?

When I try to access my static variable using Class.Variable I get the error that Error left of 'Variable' must have class/struct/union and when I do Class::Variable I get no error. Although in both cases I get Variable through intellisense. What exactly is the different between . and :: in this case?

Community
  • 1
  • 1
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122

6 Answers6

6

Static members of a class can be accessed through two ways

(a) With instances of class - Use . e.g. obj.variable

(b) Without instance of class - Use :: e.g. class::variable

Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
2

. is used for objects :: for class names.

 struct foo {
     int x;
     static int y;
 };

 foo bar;

 bar.x = 10; // ok
 bar.y = 20; // ok - but bad practice

 foo.x = 10; // WRONG foo is class name
 foo.y = 20; // WRONG foo is class name

 foo::x = 10; // WRONG x requires object
 foo::y = 20; // ok

best practice:

 bar.x = 10; 
 foo::y = 20;
Artyom
  • 31,019
  • 21
  • 127
  • 215
  • 1
    `bar.y = 20; // ok - but bad practice` __NO__, it's just plain __wrong__ and not allowed by the standard. Sadly VC++ provides some _extensions_ that make it legal, but it's by all means not. – orlp Jul 18 '11 at 10:33
  • 3
    @nightcracker: it's not "wrong"; `.` or `->` can be used to access any member, static or otherwise. @Artyom: why do you say it's "bad practice"? – Mike Seymour Jul 18 '11 at 10:55
0

. is an instance reference (e.g. on the LHS there is an object). :: is a static reference. On the RHS there is a type.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
0

The dot operator, also called the "class member access operator", needs an instance of a class/struct.

struct X
{
    static int x = 5;
    int y = 3;
}
namespace NS
{
    int z = 1;
}

void foo()
{
    X instance;
    instance.x;  // OK, class member access operator
    X::x;        // OK, qualified id-expression
    instance.y;  // OK
    X::y;        // Not OK, y is not static, needs an instance.
    NS.z;        // Not OK: NS is not an object
    NS::z;       // OK, qualified-id expression for a namespace member.
}

The wording is taken from C++0x FDIS.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • so, who is correct here, David (saying a.b is ok for static b), or nightcracker (saying that a.b is by all means wrong if b is static)??? – stijn Jul 18 '11 at 10:40
  • David: I guess it is. FDIS C++0x `9.4/2` does indeed state it is legal. Will edit. – rubenvb Jul 18 '11 at 10:41
  • `X.x` and `X.y` are both not OK in your example. – interjay Jul 18 '11 at 10:47
  • Interjay: see FDIS 9.4/2. It says it *is* OK.Please note this answer has been edited to be correct, and David's comment was on the previous version... – rubenvb Jul 18 '11 at 10:48
  • 9.4/2 says no such thing. It says you can use `Class::static_member` or `expression.static_member`. – interjay Jul 18 '11 at 11:01
  • interjay: it says explicitely: `A static member may be referred to using the class member access syntax, in which case the object expression is evaluated.` The second part doesn't matter in my case, it does in the case of the FDIS example. `instance` in my example takes the place of your `expression` in your second code bit. – rubenvb Jul 18 '11 at 11:06
  • @rubenvb: but `X` isn't an object, so `X.x` and `X.y` are invalid. `instance.x` and `instance.y` would be OK. – Mike Seymour Jul 18 '11 at 11:09
  • I guess you meant to use `instance` instead of `X` then? – interjay Jul 18 '11 at 11:10
  • I have removed the comment as it no longer made sense, and fixed the code sample. As @Mike and @interjay correctly state, to use the member class access syntax you need an *instance* not a type. (And a simple run through a compiler would have told you so, I consider it *good practice* to run the code bits by a compiler to verify the results) – David Rodríguez - dribeas Jul 18 '11 at 11:15
  • David: Ah yes, typos typos typos. Sorry for the confusion. My fault. Thanks for the fixup – rubenvb Jul 18 '11 at 11:52
0

the :: is for class/namespace scope, but the left hand side of . must be a variable in this case. Note that this would work, which might be why Intellisense works as well for you:

Class x;
doSomething( x.Variable );
stijn
  • 34,664
  • 13
  • 111
  • 163
0
Class::StaticProperty
InstanceOfClass.Property
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210