By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

How to state the scope of an variable

P: n/a
Usually, when i declare a method and a variable in the
header file i go as follows.

public:
int number;
void doSome ();

Then, in the CPP-file i will define it as follows.

int number;
void SomeClass::doSome () {this.number = 5;}

How do i explicitly state that "number" belongs to the class
"SomeClass"? I know for a fact that i don't go SomeClass::
in front of it.

I understand that the class name followed by colon-colon is
there to specify in what class the definition is supposed to
be put. Does the lack of the explicit specification on the
variables mean that they are global? How to avoid it?

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
The Cool Giraffe wrote:
Usually, when i declare a method and a variable in the
header file i go as follows.

public:
int number;
void doSome ();

Then, in the CPP-file i will define it as follows.

int number;
This is a mistake. It defines a file-scope variable named number, which
you don't want since you already have a member variable with the same name.
void SomeClass::doSome () {this.number = 5;}

How do i explicitly state that "number" belongs to the class
"SomeClass"? I know for a fact that i don't go SomeClass::
in front of it.
You already did, in the class declaration.
I understand that the class name followed by colon-colon is
there to specify in what class the definition is supposed to
be put. Does the lack of the explicit specification on the
variables mean that they are global? How to avoid it?
No. The lack of the explicit specification means the compiler will use
whatever variable is in scope. It is a member variable, not a global.
Generally speaking, the scope is defined when you declare the variable.
You have to explicitly specify scope only if the default is not what you
need.

To simplify your thinking: member functions can refer to member
variables without specifying a scope.

--
Scott McPhillips [VC++ MVP]

Feb 24 '07 #2

P: n/a
The Cool Giraffe wrote:
Usually, when i declare a method and a variable in the
header file i go as follows.

public:
int number;
void doSome ();

Then, in the CPP-file i will define it as follows.

int number;
void SomeClass::doSome () {this.number = 5;}

How do i explicitly state that "number" belongs to the class
"SomeClass"? I know for a fact that i don't go SomeClass::
in front of it.
This is clearly a misunderstanding you have.

You don't need a seperate definition of the number variable, just remove
it, it's completely unrelated to the number variable you've declared in
your class.

The rules are different for static member variables however.

john
Feb 24 '07 #3

P: n/a

"The Cool Giraffe" <gi******@viltersten.comwrote in message
news:54*************@mid.individual.net...
Usually, when i declare a method and a variable in the
header file i go as follows.

public:
int number;
this number is local to the class.
void doSome ();

Then, in the CPP-file i will define it as follows.

int number;
this number is global.
void SomeClass::doSome () {this.number = 5;}
why this.number? it should be this->number if you wanted to use number
anyway since this is a pointer. You can just say:
number = 5;
and it will use the number local to the class. If you want to use the
global number you would use:
::number = 5;
How do i explicitly state that "number" belongs to the class
"SomeClass"? I know for a fact that i don't go SomeClass::
in front of it.
Don't put anything in front of it, it's the one local to the class. Use
this->number, its the one local to the class. Use :: in front, it's the
global number (unnamed namespace)
I understand that the class name followed by colon-colon is
there to specify in what class the definition is supposed to
be put. Does the lack of the explicit specification on the
variables mean that they are global? How to avoid it?
No, other way around. Lack of explicit speicification uses the tightest
specfiication, the local number.

Feb 24 '07 #4

P: n/a
Jim Langston wrote/skrev/kaita/popisal/schreibt :
"The Cool Giraffe" <gi******@viltersten.comwrote in message

<snip>
>void SomeClass::doSome () {this.number = 5;}

why this.number? it should be this->number if you wanted to use number
anyway since this is a pointer. You can just say:
number = 5;
and it will use the number local to the class. If you want to use the
global number you would use:
>>number = 5;

Are you sure? What you wrote looks a bit ambigous
to me. Now, i'm no guru of C++ exactly so i won't
make any bold statements here but as far i've
understood the matter, i was expecting

this->number = 5; //local variable call
::number = 5; //global scope variable

Was it a typo or do i still miss something?

As for the this-dot or this-arrow wondering. I've
been tought to be explicit in order to avoid errors
believing to be in one scope but actually being
elsewhere. Perhaps it's just a bad habit. :)
>How do i explicitly state that "number" belongs to the class
"SomeClass"? I know for a fact that i don't go SomeClass::
in front of it.

Don't put anything in front of it, it's the one local to the class. Use
this->number, its the one local to the class. Use :: in front,
it's the global number (unnamed namespace)
Great, i got it. Thanks to all!

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 25 '07 #5

P: n/a
>
As for the this-dot or this-arrow wondering. I've
been tought to be explicit in order to avoid errors
believing to be in one scope but actually being
elsewhere. Perhaps it's just a bad habit. :)
I generally name class variables with a leading underscore. That way I
don't get confused between class variables and method parameters (or,
ugh, global variables).

class X
{
private:
int _count;
int _thing;
double _whatever;
public:
...
};

john
Feb 25 '07 #6

P: n/a
John Harrison wrote/skrev/kaita/popisal/schreibt :
>As for the this-dot or this-arrow wondering. I've
been tought to be explicit in order to avoid errors
believing to be in one scope but actually being
elsewhere. Perhaps it's just a bad habit. :)

I generally name class variables with a leading underscore. That way I
don't get confused between class variables and method parameters (or, ugh,
global variables).

class X
{
private:
int _count;
int _thing;
double _whatever;
public:
...
};

Thanks for that. Perhaps i'll use it too. However, i had
one more question. You wrote this.

"You can just say:
number = 5;
and it will use the number local to the class.
If you want to use the global number you would use:
number = 5;"

Did you mean that? I was expecting
this->number or number in the first case
and ::number in the latter.

--
Všnligen Kerstin Viltersten
(The Cool Giraffe)
Feb 25 '07 #7

P: n/a

"The Cool Giraffe" <gi******@viltersten.comskrev i meddelandet
news:54*************@mid.individual.net...
John Harrison wrote/skrev/kaita/popisal/schreibt :
>>As for the this-dot or this-arrow wondering. I've
been tought to be explicit in order to avoid errors
believing to be in one scope but actually being
elsewhere. Perhaps it's just a bad habit. :)

I generally name class variables with a leading underscore. That way I
don't get confused between class variables and method parameters (or,
ugh, global variables).

class X
{
private:
int _count;
int _thing;
double _whatever;
public:
...
};


Thanks for that. Perhaps i'll use it too.
You will have to think about this one first. :-)

While technically allowed to use a single leading underscore in class member
names, those names are reserved in the global namespace for use by the
implementation. This means that _whatever could be either a local class
member, or a global name for compiler specific extension. Is that a good
convention?
However, i had
one more question. You wrote this.

"You can just say:
number = 5;
and it will use the number local to the class.
If you want to use the global number you would use:
number = 5;"
This works fine, if there is only one name. The first one visible will be
chosen.
>
Did you mean that? I was expecting
this->number or number in the first case
and ::number in the latter.
This way you specify which one you intended to use. Often it is obvious from
the context which one you should use. In that case, adding scope resolution
to everything would make me stop and wonder why you have chosen to do so. I
think that it would add more confusion than clarity.

If you don't name your classes X, it will be more obvious if they could hold
a 'number' or not.
Bo Persson
Feb 25 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.