5

For example, x is static in class's definition. I should use x in this way:

class_name::x; rather than instance_of_class.x;

My question is, is the latter one also legal? Or just not correct?

Thanks.

2 Answers 2

13

It is legal and correct, but the general consensus is to use the scope resolution operator.

class_name::x 

rather than

instance.x 
Sign up to request clarification or add additional context in comments.

1 Comment

The only time I've used . was in a template function where it was only sometimes a static member.
4

Both are valid. The reason to prefer use the first one is to make a reader of your code to understand that the member is static and belongs to the whole class of objects rather than each particular instance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.