static-functions

Is it acceptable to mix static and non static methods in one class?

我只是一个虾纸丫 提交于 2021-02-16 17:55:34
问题 I have a relatively simple question, and although there are many posts about it on Google, I cannot find a single one that simply answers the question. So the short question is "Is it acceptable to mix static and non static methods in one class?". I guess I am really asking "is it good practice to stick with one type of method", or "are there things to consider when using both". For example, If I was building a class to cope with food in my fridge, which of the following (or what else) would

Is it acceptable to mix static and non static methods in one class?

心已入冬 提交于 2021-02-16 17:54:54
问题 I have a relatively simple question, and although there are many posts about it on Google, I cannot find a single one that simply answers the question. So the short question is "Is it acceptable to mix static and non static methods in one class?". I guess I am really asking "is it good practice to stick with one type of method", or "are there things to consider when using both". For example, If I was building a class to cope with food in my fridge, which of the following (or what else) would

Static Virtual functions in c++

只愿长相守 提交于 2019-12-21 03:43:20
问题 I have a base class and a derived one and I want to change base functions while keeping them static as they should be passed to other functions as static. How can I do that? 回答1: The ATL framework gets around the limitation of no virtual statics by making the base class be a template, and then having derived classes pass their class type as a template parameter. The base class can then call derived class statics when needed, eg: template< class DerivedType > class Base { public: static void

Where would you use a friend function vs. a static member function?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 10:14:53
问题 We make a non-member function a friend of a class when we want it to access that class's private members. This gives it the same access rights as a static member function would have. Both alternatives would give you a function that is not associated with any instance of that class. When must we use a friend function? When must we use a static function? If both are viable options to solve a problem, how do we weigh up their suitability? Is there one that should be preferred by default? For

Use UI elements from static function

这一生的挚爱 提交于 2019-12-12 01:49:23
问题 I am building a qt application in which i am have to access ui elements. but i am getting error as invalid use of member 'foo::ui' in static member function The code is big so cant add here. Declaration of ui private: Ui::foo *ui; Initialization in Constructor foo::foo(QWidget *parent) : QMainWindow(parent), ui(new Ui::foo) { ui->setupUi(this); } Accessing in static function where it is giving error. ui->fp->setText("Some Text"); Static function declaration. static I eventCallback(PVOID i_pv

how to test static functions of C using google test

自闭症网瘾萝莉.ら 提交于 2019-12-11 11:55:46
问题 I have a C file contains some static functions, how to use google test to test those static function? header file: test.h int accessData(); source file: test.c static int value; static int getData() { return value; } int accessData() { if(value != 0) { return getData(); } return 0; } static function is called by global function, but how to test those static function using google test? 回答1: One way to achieve this is to #include the C source file into your test source. Then, the static

If statement failing to evaluate condition

断了今生、忘了曾经 提交于 2019-12-11 06:06:03
问题 I have a basic class that containers two enumerators, one for input and one for output. It has two member functions which are both static. The first function is just a static function that returns a value based on the input. It will call the second function which is a constexpr function template that will return the constexpr values. You can see the full class here. class Foo { public: enum Input { INPUT_0 = 0, INPUT_1, INPUT_2 }; enum Output { OUTPUT_0 = 123, OUTPUT_1 = 234, OUTPUT_2 = 345 }

Static functions in Linux device driver?

自作多情 提交于 2019-12-05 17:42:19
问题 Is there a reason why most function definition in device driver in linux code is defined as static? Is there a reason for this? I was told this is for scoping and to prevent namespace pollution, could anyone explain it in detail why static definition is used in this context? 回答1: Functions declared static are not visible outside the translation unit they are defined in (a translation unit is basically a .c file). If a function does not need to be called from outside the file, then it should

static member function and thread-safety

眉间皱痕 提交于 2019-12-05 17:39:04
问题 In C++, when you have local variables in a static member function, does it mean those local variables are also implicitly static or are they really local? example: static void myClass::somefunc(int someint) { int myint = someint; // is myint really a local variable or does it change due to the static qualifier at function level? } Also, different threads from a thread pool running this function, does myint need to be protected by a lock? assuming that all values passed to it are different and

PHP constructors and static functions

烈酒焚心 提交于 2019-12-04 17:40:26
问题 I'm a bit confused on how constructors work in PHP. I have a class with a constructor which gets called when I instantiate a new object. $foo = new Foo($args); __construct($params) is called in the class Foo and it executes the appropriate initialization code. However when I use the class to call a static function, the constructor is called again. $bar = Foo::some_function(); //runs the constructor from Foo This causes the constructor to execute, running the object initialization code that I