without reference-pointer

hi everybody is it posibble to run this code without using neither pointer nor reference

#include <iostream>
using namespace std;

double &f();

double val = 100.0;

int main()
{
double x;

cout << f() << '\n';

x = f(); // assign value of val to x
cout << x << '\n';

return 0;
}

double &f()
{
return val; // return reference to val
}
Variable val is a global variable. So it is visible in the translation unit until it will not be hidden by a declarationn with the same name. So you could write much simpler

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

double val = 100.0; 

int main() 
{ 
   double x; 

   x = val; // assign value of val to x 

   cout << x << '\n'; 

   return 0; 
} 


Or if you want to use a function then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> 
using namespace std; 

double val = 100.0; 

inline double f() { return ( val ); }

int main() 
{ 
   double x; 

   cout << f() << '\n'; 

   x = f(); // assign value of val to x 
   cout << x << '\n'; 

   return 0; 
} 


closed account (zb0S216C)
Namespaces were created for exactly this reason:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace Global
{
    double &Value()
    double Value_;
}

inline double &Global::Value()
{
     return(Global::Value_);
}

int main()
{
    Global::Value() = /*...*/;
}

Wazzak
thank you friends
Topic archived. No new replies allowed.