Static member funciton call non-static function
-
I have a static function and I want it to call a non-static function. The computer is telling me that I can not do this just by calling the non-static function outright. Is there a way to call the nonstatic function from the static function?
-
I have a static function and I want it to call a non-static function. The computer is telling me that I can not do this just by calling the non-static function outright. Is there a way to call the nonstatic function from the static function?
If you need to, then the function should not be static. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
If you need to, then the function should not be static. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
If it should not be static, so there are any problem. You should modify your function, that can accept an argument (this pointer). So that, in the function, you can call none static function from this pointer. For example:
class A{
static void Func(A* pA)
{
pA->NoneStaticFunc();
}
} -
If it should not be static, so there are any problem. You should modify your function, that can accept an argument (this pointer). So that, in the function, you can call none static function from this pointer. For example:
class A{
static void Func(A* pA)
{
pA->NoneStaticFunc();
}
}This is plain stupid. If you can only call a method when you have an instance of the class, the method should not be static. It in essence won't be static, it can only be called when you have an instance of the class to call it with, even if you don't call it from the class. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
This is plain stupid. If you can only call a method when you have an instance of the class, the method should not be static. It in essence won't be static, it can only be called when you have an instance of the class to call it with, even if you don't call it from the class. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
No. I don't think so. According to C++ principle, static members should be shared with every instances of class. But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case.
nguyenvhn wrote: According to C++ principle, static members should be shared with every instances of class Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not. nguyenvhn wrote: But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case. We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static. If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
nguyenvhn wrote: According to C++ principle, static members should be shared with every instances of class Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not. nguyenvhn wrote: But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case. We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static. If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
:)Oh oh, In fact, you are right. I have nothing to protest. Of course, a static member has class scope, so that it can call only others static members. But in this case, the guy need a solution to adapt with his problem (it may be by historic). Because he needed to call a none static member, it was evident that there had to be exist an instance of class. It is very clear that we shoud avoid such case but a trick in porogamming makes the life more funny;P and I like to solve that stuck:-D It is so interesting in reasoning with you.;P
-
nguyenvhn wrote: According to C++ principle, static members should be shared with every instances of class Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not. nguyenvhn wrote: But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case. We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static. If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
In general, I agree with Christian on this. Christian Graus wrote: A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice. And Microsoft encourages this practice :) with Windows callback functions. The callback (if implemented as part of a C++ class) has to be a static member, but in general, what's done is that there's a programmer-supplied value (usually void*) that can be passed as well as the function address. In those cases the approach is to pass the address of an instance of the class, as has been previously suggested. Steve S Developer for hire