I wrote the following code: class A { public: int cnt; static void inc(){ d.cnt=0; } }; int main() { A d; return 0; } I have seen this question: How to call a non static member function from a static member function without passing class instance But I don't want to use pointer. Can I do it without using pointers? Edit: I have seen the following quest
我写了下面的代码: class A { public: int cnt; static void inc(){ d.cnt=0; } }; int main() { A d; return 0; } 我看到了这个问题: 如何在不传递类实例的情况下从静态成员函数中调用非静态成员函数 但我不想使用指针。 我可以不使用指针吗? 编辑: 我看到了以下问题: 如何从Java中的静态方法访问非静态成员? 为什么我不能做这样的事情? 不,从没有指向对象实例的指针
I'm trying to use the following: pthread_create(&searchThread[i], &threadAttribs[i], DoStuff, &ParallelParams[i]); If DoStuff is static it compiles but then I do not have access to any of the methods or variables that are part of the class that DoStuff is in. But if I keep DoStuff as a non-static method so that I can access everything else in the class I get the following comp
我试图使用以下内容: pthread_create(&searchThread [i],&threadAttribs [i],DoStuff,&ParallelParams [i]); 如果DoStuff是静态的,它会编译,但是我不能访问DoStuff所在类中的任何方法或变量。但是,如果我将DoStuff保留为非静态方法,以便可以访问其他所有内容该类我得到以下编译器错误: 错误:类型'void *(MyClass ::)(void *)'的参数不匹配'void *()(void)' 错误似乎指的是DoStuf
In C++, is it safe/portable to use static member function pointer for C API callbacks? Is the ABI of a static member function the same as a C function? It is not safe per the C++ standard. As stated in this SO posting: AC callback function implemented in C++ must be extern "C". It may seem to work as a static function in a class because class-static functions often use the same c
在C ++中,对C API回调使用静态成员函数指针是否安全/可移植? 静态成员函数的ABI是否与C函数相同? 每个C ++标准都不安全。 正如此SO帖子所述: 用C ++实现的AC回调函数必须是extern“C”。 它可能看起来像一个类中的静态函数,因为类静态函数通常使用与C函数相同的调用约定。 但是,这样做是一个等待发生的错误(请参阅下面的注释),所以请不要 - 通过外部“C”包装来代替。 根据Martin York在答案中提出的意见,在某些
Why can't static member function be declared const? I know a static function doesn't act on any particular instance of class. This means such function has no this pointer (passed implicitly as hidden argument) to any particular instance. But i don't understand why there is a compiler error when it is declared const, since it is not changing any particular instance. Secondly, why
为什么不能将静态成员函数声明为const? 我知道一个静态函数不会影响任何特定类的实例。 这意味着这样的函数没有这个指针(隐式地作为隐藏参数传递)给任何特定的实例。 但我不明白为什么有一个编译器错误时,它被声明为常量,因为它没有改变任何特定的实例。 其次,为什么不能将静态成员函数声明为虚拟的? 需要对此进行一些澄清。 为什么声明为const时会出现编译器错误 const意思是“ this是一个指向const对象的指
Here be a simple C++ class: class A { public: explicit A() : m_a(0) { } explicit A(int a) m_a(a) { } int getA() const { return m_a; } void setA(int a) { m_a = a; } private: int m_a; } This is what I know so far: when you declare an object of a class instance, memory gets allocated for that object. The allocated memory is equivalent to the memory of its members summed up.
这里是一个简单的C ++类: class A { public: explicit A() : m_a(0) { } explicit A(int a) m_a(a) { } int getA() const { return m_a; } void setA(int a) { m_a = a; } private: int m_a; } 这是我迄今所知道的: 当你声明一个类实例的对象时,会为该对象分配内存。 分配的内存相当于其成员的内存总结。 所以在我的情况下sizeof(A) = sizeof(int) = sizeof(m_a) 类A的所有成员函数都存储在内存
I have a class (B) which has a static member pointer to an object of another class (A). In one member function of the first class (B), I need a function pointer that points to a member function of the second class (A). class A { public: int DoubleValue(int nValue) { return nValue * 2; } }; class B { private: static A* s_pcA; public: void Something() { //
我有一个类(B),它有一个指向另一个类(A)的对象的静态成员指针。 在第一个类(B)的一个成员函数中,我需要一个指向第二个类(A)的成员函数的函数指针。 class A { public: int DoubleValue(int nValue) { return nValue * 2; } }; class B { private: static A* s_pcA; public: void Something() { // Here a need the function pointer to s_pcA->DoubleValue() } };
If each member function is only contained once per class (to be shared by all instances) what exactly is the purpose of declaring a member function static? Is it like a function being declared const, in that it modifies a particular type of data (in this case, static data members)? Normal member functions require a class instance to run. Static methods can be called directly without first cre
如果每个成员函数仅包含一次(由所有实例共享),那么声明一个成员函数的目的究竟是静态的呢? 它是否像一个声明为const的函数,因为它修改了特定类型的数据(在这种情况下是静态数据成员)? 普通成员函数需要运行一个类实例。 可以直接调用静态方法,而无需先创建类的实例。 正常方法: MyClass myClass; myClass.NormalMethod(); 静态方法: MyClass::StaticMethod(); 所以普通方法对于与类数据一起工作的函数来说
Is it possible to use a function pointer to point at a non-static member function? I would like to point at a member function that uses non-static objects and variables from the class...static member functions cannot do this... I have a member function that points at one of four static member functions. The problem is that the data to be used by each of the four member functions is non-static
是否有可能使用函数指针指向非静态成员函数? 我想指出一个使用非静态对象和类的变量的成员函数...静态成员函数不能这样做... 我有一个成员函数指向四个静态成员函数之一。 问题是四个成员函数中的每一个都使用的数据是非静态的,所以静态成员函数将不会访问它们... 我可以指向非静态成员函数吗? 此外,类实例是一个指针...这是我的代码: class CRoutine{ int m_index; ... BOOL (*CallRoutine(char opcod
For static member initialization I use a nested helper struct, which works fine for non templated classes. However, if the enclosing class is parameterized by a template, the nested initialization class is not instantiated, if the helper object is not accessed in the main code. For illustration, a simplified example (In my case, I need to initialize a vector). #include <string> #include
对于静态成员初始化,我使用嵌套的帮助器结构,对于非模板类非常适用。 但是,如果封闭类由模板参数化,则嵌套初始化类不会实例化,如果辅助对象在主代码中未被访问。 为了说明,一个简单的例子(在我的情况下,我需要初始化一个向量)。 #include <string> #include <iostream> struct A { struct InitHelper { InitHelper() { A::mA = "Hello, I'm A."; } };
I want to have a class with a private static data member (a vector that contains all the characters az). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data members of the class. It only gets run once (as the variables are read only and only need to be set once) and since it's a function of the c
我想要一个私有静态数据成员(包含所有字符az的向量)的类。 在java或C#中,我可以创建一个“静态构造函数”,它将在我创建该类的任何实例之前运行,并设置该类的静态数据成员。 它只运行一次(因为变量是只读的,只需要设置一次),并且由于它是该类的一个功能,它可以访问其私有成员。 我可以在构造函数中添加代码,检查vector是否已初始化,如果不是,则会初始化该代码,但会引入许多必要的检查,并且看起来不像是问题的最