473,378 Members | 1,489 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,378 software developers and data experts.

Which scope is searcheg first ?

Hi,


I took a C++ test and I found the following question:

Q. Inside a class member function definition, which scope is searched
first when an unqualified variable is accessed?

1.Class static data
2.Global namespace data
3.Function local data
4.Current namespace data
5.Class member data


If I understand the question correctly I have to tell where
the compiler is looking for a variable when I use its symbol. To my
understanding the answer is "Function local data". That means that
inside a member function when I am using the variable myVar it will
first try to see if I have a local variable named myVar. If such a
variable does not exist then it will see if I have a member variable
myVar. If such one is not defined then it will look in the current
namespace and then in the global namespace.

So the 'scope' order is:
a. function local data
b. class member data (the same as static member data); I mean they are
both members of the class and the fact that the data is static or not
is irrelevant from this point of view (scope order evaluation)
c. current name space
d. global name space
Please correct me if I am wrong.

Regards,
Jul 22 '05 #1
6 1557
Razvan wrote:
I took a C++ test and I found the following question:

Q. Inside a class member function definition, which scope is searched
first when an unqualified variable is accessed?

1.Class static data
2.Global namespace data
3.Function local data
4.Current namespace data
5.Class member data


If I understand the question correctly I have to tell where
the compiler is looking for a variable when I use its symbol. To my
understanding the answer is "Function local data". That means that
inside a member function when I am using the variable myVar it will
first try to see if I have a local variable named myVar. If such a
variable does not exist then it will see if I have a member variable
myVar. If such one is not defined then it will look in the current
namespace and then in the global namespace.

So the 'scope' order is:
a. function local data
b. class member data (the same as static member data); I mean they are
both members of the class and the fact that the data is static or not
is irrelevant from this point of view (scope order evaluation)
c. current name space
d. global name space
Please correct me if I am wrong.


Seems fine. Basically, the rule is "from the most enclosed scope
to the global scope".

Victor
Jul 22 '05 #2
mi*****@mailcity.com (Razvan) wrote in message news:<15**************************@posting.google. com>...

[ ... ]
I took a C++ test and I found the following question:

Q. Inside a class member function definition, which scope is searched
first when an unqualified variable is accessed?

1.Class static data
2.Global namespace data
3.Function local data
4.Current namespace data
5.Class member data


Technically, none of the above. _Block_ local data is searched first.
Just for example:

int func() {
int a; // function local data

if ( something) {
float a; // block local data

a = 1; // unqualified variable access.

In this case, the function-local data is NOT used -- the block-local
data is what is accessed. For the most part, the rule is pretty
simple though: searching starts at the most local scope and progresses
outward to the least local scope.

There are exceptions to this though -- for an obvious one, a using
declaration can "teleport" the names from some namespace into the
local scope. Templates also have some oddities because searching
happens both in the scope where the template is instantiated AND the
scope where it is defined (I'm simplifying a bit, but hopefully I'm
portraying the basic idea). In the usual case, that's pretty
straightforward, but when/if you export a template, it can get
substantially more complex.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #3
Hello,


So, the correct order is:

a. block local data
b. function local data
c. class member data (the same as static member data); I mean they are
both members of the class and the fact that the data is static or not
is irrelevant from this point of view (scope order evaluation)
d. current name space
e. global name space

There are exceptions to this though -- for an obvious one, a using
declaration can "teleport" the names from some namespace into the
local scope. Templates also have some oddities because searching
For example:

void func ()
{

{
using namespace SomeNamespaceABC;
func_from_namespace_ABC();
}

func_from_namespace_ABC(); // here it fails !? the namespace is
not in scope any more ?!
}

That means the 'using' clause is valid for the current scope.
Right ?
happens both in the scope where the template is instantiated AND the
scope where it is defined (I'm simplifying a bit, but hopefully I'm


What are you trying to say about templates ? Can you give me
an example ? Or perhaps just point me to some documents on the matter.
Thanks,
Razvan
Jul 22 '05 #4

"Razvan"
void func ()
{

{
using namespace SomeNamespaceABC;
func_from_namespace_ABC();
}

func_from_namespace_ABC(); // here it fails !? the namespace is
not in scope any more ?!
}

That means the 'using' clause is valid for the current scope.
Right ?

Yeah, but Jerry spoke about a using-declaration and you give an example with
a using-directive and you have made your own term for it.

Fraser.
Jul 22 '05 #5
> Yeah, but Jerry spoke about a using-declaration and you give an example with
a using-directive and you have made your own term for it.


What is the difference between a 'using declaration' and a
'using directive' ?

Razvan
Jul 22 '05 #6
A using-directive brings into scope the contents of a namespace. A
using-declaration only brings into scope a name. The name could be that of
more than one entity e.g. a function and an POD type. A using-declaration
brings into scope only previously declared names. A using-directive brings
into scope everything declared before and after it.

Fraser.


"Razvan"
Yeah, but Jerry spoke about a using-declaration and you give an example with a using-directive and you have made your own term for it.


What is the difference between a 'using declaration' and a
'using directive' ?

Razvan

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
6
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. ...
52
by: Darklight | last post by:
Question: Write a function named sumarrays() that accepts two arrays that are the same size. The function should add each element in the array together and place the values in the thrid array. ...
5
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...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
10
by: John Salerno | last post by:
Here's a sentence from Learning Python: "Names not assigned a value in the function definition are assumed to be enclosing scope locals (in an enclosing def), globals (in the enclosing module's...
6
by: grbgooglefan | last post by:
I am compiling CPP program which uses CPython API functions from Python 2.5.1 source code First I compiled with this commanline, that time I got "pyconfig.h" not found. g++ -Os -I../../Include...
0
by: David Troxell - Encourager Software | last post by:
Product Scope 7 (http://www.encouragersoftware.com/) and Profile Exchanges enhanced with SetupCast publishing methods NOTE: If you are a software author, releasing commercial and/or shareware...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.