473,668 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::doSo me () {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 1558
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::doSo me () {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::doSo me () {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 misunderstandin g 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******@vilte rsten.comwrote in message
news:54******** *****@mid.indiv idual.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::doSo me () {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******@vilte rsten.comwrote in message

<snip>
>void SomeClass::doSo me () {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******@vilte rsten.comskrev i meddelandet
news:54******** *****@mid.indiv idual.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
3154
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 function-prototype scope I think(might be wrong):
3
2006
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 + el2; if (value1 > value2) return 1;
8
3369
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 actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
4
14888
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 strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
4
3178
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 is passed with the page encrypted where session is stored locally in the aspnet_wp.exe process. Also, how do class member variables fit in the mix?
4
3531
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 myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such...
5
2592
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 fine so far. My problem is that when I call a web method to modify the dataset inside the Web service it is never updated and I get no errors. currentPeriods method will always give me 1 even if I call ChangeDataset and call currentPeriods...
1
25667
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
0
35215
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 the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.