473,385 Members | 2,005 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.

How to state the scope of an variable

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
7 1549
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
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

"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
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
>
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
4
by: John Smith Jr. | last post by:
Could someone enlighten me what the difference between viewstate[" and session[" variables in terms of use, performance, and rule of thumb, from what I understand viewstate has overhead to it, and...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
5
by: joeblast | last post by:
I have a Web service that gets the financial periods and hold a reference to a disconnected dataset built at initialization. Web methods work on the dataset inside the web service. Everything is...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.