473,612 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1575
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*****@mailcit y.com (Razvan) wrote in message news:<15******* *************** ****@posting.go ogle.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 SomeNamespaceAB C;
func_from_names pace_ABC();
}

func_from_names pace_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 SomeNamespaceAB C;
func_from_names pace_ABC();
}

func_from_names pace_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
3908
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 table1"; var obj=new ActiveXObject("ADODB.Recordset");
6
2268
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. For example, say I have the following script <?php constant myvar = "my original string";
52
2276
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. Which answer is the better practice; /*EX9.C*/ #include<stdio.h> #define MAX 5
5
2082
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):
2
3190
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. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately obvious why this would be occuring. There are no modifications occuring within the loop and there is...
10
1872
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 namespace) or built-in (in the predefined __builtin__ names module Python provides." I have trouble reading this sentence. First, I don't understand if the word 'enclosing' is a verb or an adjective. The whole flow of the sentence seems...
6
2823
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 ../../libpython2.5.a -lm -ldl -lpthread -lutil testeval.cpp In file included from testeval.cpp:1: .../../Include/Python.h:8:22: pyconfig.h: No such file or directory In file included from ../../Include/Python.h:57, from
0
2090
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 software, this announcement will be of interest to you - particularly our Shareware and Search Engine Profile Exchanges - Even our Clarion 3rd Party Profile Exchange displays over 60 NON-Clarion products of interest to Developers including...
27
1684
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
8171
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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...
1
8253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6081
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
5536
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
4047
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...
0
4110
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2554
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
0
1414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.