Accessing global variable with this-> pointer in C++? [duplicate] - c++

This question already has answers here:
How to refer to a global variable which has the same name as a local variable in C++?
(3 answers)
What is the meaning of prepended double colon "::"?
(9 answers)
Closed 1 year ago.
#include <stdio.h>
void val();
int v=10;
class test{
public:
int v=11;
void val()
{
printv();
}
private:
void printv()
{
int v=12;
printf("V: %d",this->v);
}
};
int main()
{
test obj;
obj.val();
return 0;
}
I am getting 11 as output but I need to access the global variable, How can I get it ??
Similarly are there any ways to access global variables with the same name as local variables in C & Python ??

Related

Why does calling a functor with an undeclared variable work? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
class foo {
public:
bool operator () (int & i) {
return true;
}
};
int main() {
foo(WhyDoesThisCompile);
return 0;
}
When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.
Why is this? I tested it on clang 4.0.0.
You are not invoking the functor.
You are declaring a foo, called WhyDoesThisCompile.
Yes, despite the parentheses.
I guess you meant this:
foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
// of invocation of op()
// type
// `foo`
… which doesn't.

c++ oop program doesn't give expected result [duplicate]

This question already has answers here:
Returning a reference to a local variable in C++
(3 answers)
Closed 6 years ago.
Consider the following piece of program:
class cls
{
int vi;
public:
cls(int v=37)
{
vi=v;
}
friend int& f(cls);
};
int& f(cls c)
{
return c.vi;
}
int main()
{
const cls d(15);
f(d)=8;
cout<<f(d);
return 0;
}
When I run it, the output is
15
but I don't understand why 15, because I thought it should've outputed 8, because of the
f(d)=8
function, which from what I understand makes the c.vi=8, but I might be wrong and the function probably does something else entirely, so then I ask, what is the purpose or what does the
friend int& f(cls);
function do?
Your program has Undefined Behavior - you are returning a dangling reference to local variable of a function (argument is a local variable as well).

using a member variable before its declaration? [duplicate]

This question already has answers here:
Do class functions/variables have to be declared before being used?
(5 answers)
Closed 6 years ago.
is it possible to use a member variable of a class before declaring it?
here is my code.
using namespace std;
class Computer
{
public:
Computer()
{
processor_speed = 0;
}
~Computer();
void setspeed (int);
int getspeed (void);
private:
int processor_speed;
};
/*Computer::Computer()
{
processor_speed = 0;
} */
Computer::~Computer()
{
cout<<"this is a destructor"<<endl;
}
void Computer:: setspeed(int p)
{
processor_speed = p;
}
int Computer::getspeed(void)
{
return processor_speed;
}
int main(void)
{
Computer obj;
cout<<"processor speed is "<<obj.getspeed()<<endl;
obj.setspeed(100);
cout<<"processor speed is now "<<obj.getspeed()<<endl;
return 0;
}
as u can see here i was able to use variable processor_speed before declaring it.
i saw a similar question here: Do class functions/variables have to be declared before being used?
but i was not able to understand the reason why this code work.
Thanks
Yes, you can do it.
A member variable is in scope for member functions of your class even if it is textually after its first point of use. The compiler translates your code in several "passes". One could think of it as getting all member variables first, and only then translating member functions, with all declarations in place.
Note that this is not allowed for "free-standing" global and static variables inside translation units: a declaration must precede the first use, otherwise you get an error.

Pass 2D array of pointers by reference to function [duplicate]

This question already has answers here:
How do I pass a reference to a two-dimensional array to a function?
(5 answers)
Reference to a Two-Dimesional Array
(2 answers)
Closed 8 years ago.
I have a problem. I dont know how to pass 2D array of pointers to fuction by reference.
class SomeClass
{
//body of class
}
void somefunction(SomeClass ***array)
{
//body of function
}
int main()
{
SomeClass * array[10][10]
someFunction(array?????)
}
Anyone know how to pass this array by reference??
Literally
void somefunction(SomeClass *(&array)[10][10])
{
}
int main()
{
SomeClass *array[10][10];
someFunction(array);
}

how to refer to the global namespace [duplicate]

This question already has an answer here:
c++ use default functions in class with same name
(1 answer)
Closed 8 years ago.
Inside a method isGood I want to use function isGood from the global namespace. How do I avoid to have isGood interpreted as the same method instead of the global function ?
bool isGood(){ return_if_it_is_good;}
class X{
int a;
bool isGood(){return isGood(a);}
}
Call with the :: operator :
bool isGood(){ return_if_it_is_good;}
class X{
int a;
bool isGood(){return ::isGood(a);}
}