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);}
}
Related
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 ??
This question already has answers here:
How to call a function by its name (std::string) in C++?
(4 answers)
Closed 1 year ago.
I am very kindergartener to C++. Hope someone can help me out with my problem.
Assume there is a function defined in one class.
void __foo__(int x, int y){
//do something
}
In another class, there is a char pointer that holds the value of the function name.
static const char *func = "__foo__";
How do I call the function by using the "func" like func(0, 0)?
C++ doesn't have reflection, so you have to write the code to call the function based on the name yourself
void fall_function_based_on_name(const char* func_name, classWithMethod* self, lint x, int y) {
if (strcmp(func_name, "__foo__")==0)
self->__foo__(x, y);
else
throw std::logic_error("method name not found");
}
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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ member-function pointer
How to invoke pointer to member function when it's a class data member?
I've only recently started using C++, so I apologize if the following contains any trivial mistakes, or if I missed an easier solution. I would like to achieve something like this:
class ClassA {
typedef double (ClassA::*CondFunc)();
public:
ClassA(int x, int y) {
value_ = x;
switch (y) {
case 0:
condFunc_ = &ClassA::condA;
break;
case 1:
condFunc_ = &ClassA::condB;
default:
break;
}
}
~ClassA();
int value_;
CondFunc condFunc_;
double condA() { return 2.0*value_; }
double condB() { return 4.0*value_; }
void Test() {
int a = condFunc_(); // compile error
}
};
but get a compile error in Test(). Please note that this is a vastly simplified function and is not supposed to make any sense. I've searched this forum and elsewhere for answers, but am still not sure whether defining/calling such non-static member function pointers is even possible. The only plausible hint/solution I've come across employs a static wrapper function to achieve something similar. I'd be grateful for any help/clarifications.
You have to call the member pointer function like this:
int a = (this->*condFunc_)();