Example from class

Hi,
We did this example from my course. It is just a into c++ so he was sort of telling asking if we knew how to do something that was beyond my course a little.
We ended up using a namespace as seen below. The question was how to distinguish between a globlal variable with the same name and the local variable declared in main.
Although it is supid to even have 2 variables with the same name in my oponion it is what the question was.
I suggested using a pointer to distinguish between memory locations of the variables with the same name ( I know nothing of pointers). My teacher said no no. Is that right ?
EDIT: forgot the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include
 <iostream> 
using
 namespace std; 
namespace
 mine 
{
 
int
 age = 10; 
}
 
void
 main() 
{
 
int age = 25; 

cout << mine:: age + age << endl;
 
 
 
switch(age) 
{
 


case 10: cout << "10" << endl; 
break; 

case 20: cout << "20" << endl; 
break; 
default : cout << "none" << endl; 
 
 
 
 
}
 
 
 
system(
"pause"); 
}
Last edited on
He's probably looking for the scope operator. (::)

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int foo = 5;

int main()
{
  int foo = 3;

  cout << foo;  // using the local foo... prints '3', 
  cout << ::foo;  // using the global foo... prints '5'
}
So if you use the scope operator that will go with the global variable? Like your example? In a way by default the scope operator referes to the global variable? What about my point question? Is that not anywhere close?
Thanks
So if you use the scope operator that will go with the global variable?


If you don't give a named scope, then yes it will refer to the global scope. Notice in your code example they are using the scope operator to refer to the 'mine' scope:

line 19:
cout << mine:: age + age << endl;

mine::age is referring to the age variable in the 'mine' namespace. Whereas age is referring to the local age variable.

You can put a class or a namespace name on the left side of the scope operator to specify which scope you want to look in. If you specify neither and leave the left side empty, that means you want to use global scope.

1
2
3
4
5
6
7
8
9
10
int age = 5;  // global
namespace mine {  int age = 10; }  // in a namespace

int main()
{
  int age = 15;  // local

  cout << ::age;  // prints global, '5'
  cout << mine::age;  // prints 'mine' namespace, '10'
  cout << age;  // prints local, '15' 



What about my point question? Is that not anywhere close?


Not really. You can't really use a pointer to solve this problem because you have no way to point to the appropriate variable. The only way to get a pointer to the right var is to use the scope operator -- and if you're using the scope operator then there's no reason to use a pointer.
Last edited on
Understood.
:)

Thanks.

I have another question. About arrays. Moving inserting items.
So, what if I was told that I needed to insert an item into the end of the array?
How would I acheive this ? I struggle with the arrays and inserting and removing things. Or moving everything up one position.
Do you know how to acheive thease things. Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	int A[5] = {1,2,3,4,5}; //set up an array

	int i = sizeof(A) / sizeof(A[0]); //calculate the number of elements
		  //by dividing the size of the whole array with the size of one element

	i = i -1; //reduce i by 1, since a n element array goes from A[0] to A[n-1]
	
	for(int j = i; j>=0; --j) //run through the array from highest to lowest element and overwrite each element 
	{			//with the element that has the same decreased by 1
		A[j] = A[j-1];	//NOTE: you also overwrite A[0] with A[-1] this way, so A[0] will contain some "random" data afterwards
	}	
}
Last edited on
OK, I will practice this concept. Thankyou all.
Topic archived. No new replies allowed.