C++ - Access global variables using Scope Resolution Operator (SRO).


IncludeHelp 03 July 2016

SRO (Scope Resolution Operator - :: ) is an operator which is used to access global variable and define class functions outside of the class, in this code snippet we will access global variables inside the main function using SRO (::).

C++ Code Snippet - Access Global Variables using Scope Resolution Operator (SRO)

/*C++ - Access global variables using 
Scope Resolution Operator (SRO).*/

#include <iostream>
using namespace std;

//global variables
int a=10, b=20;

int main(){
	
	int a=100, b=200;
	
	cout<<"Local variables  a: "<<a<<", b: "<<b<<endl;
	cout<<"Global variables a: "<<::a<<", b: "<<::b<<endl;

}

    Local variables  a: 100, b: 200
    Global variables a: 10, b: 20



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.