member-functions

get the real address(or index in vTable) of virtual member function

一个人想着一个人 提交于 2019-12-05 04:49:25
In c++ is there any way to get the real address of member function, or the index in vTable ? Updated: I don't know the INDEX in vTable and I don't know the address Here's why I want to know this: I want to hook the function ID3DXFont->DrawText of DirectX. If I know the index of the DrawText in the vTable, I can replace it to do the hook. But how to get the index? If it's able to get the the real address, I can search it in the vTable to get the index. And not particularly ID3DXFont->DrawText , maybe some other functions in the future, so I'm trying to write a generic hook function. Here's what

Member function pointer in C++ for_each

久未见 提交于 2019-12-05 01:32:04
问题 I'm developing a small Virtual Machine in C++ for a school project, which should work like dc command, and is composed of a Input Output element, a Chipset, a Cpu and Ram. I'm currently working on the chipset, in which I've implemented a little parsing class in order to be able to get some Asm instructions from standard input or file, and then push this instructions to the Cpu. The problem is: my instructions are sorted in a std::list, and I'd like to be able to push them each by each with a

Why can a static member function only be declared static inside the class definition and not also in its own definition?

不羁岁月 提交于 2019-12-04 16:43:12
问题 While implementing a class for creating/updating boxes on the screen, I wanted to add a static member function that makes sure no currently visible boxes overlap (taking its information from a static pointer array to all currently visible boxes) My initial code had the following structure: class Box { public: // ... static void arrangeOverlappingBoxes(); }; static void Box::arrangeOverlappingBoxes() { // ... } I was quite surprised that this generated an error C2724: 'static' should not be

static var in member function

谁说胖子不能爱 提交于 2019-12-04 11:24:55
bool SomeClass::Function( bool thankYou = true ) { static bool justAbool = false; // Do something with justAbool; ... } I have searched around but I can't find anything about this except globals vars or member functions itself. What does the above do, i.e. what happens, does justAbool keep its value after leaving the scope? Or does it 'remember' the value when it re-enters the scope? static when applied to a local variable gives that variable static storage duration . This means that the justAbool 's lifetime lasts to the end of the program rather than to the end of the invocation of the

Class member function as function pointer

余生长醉 提交于 2019-12-04 06:00:53
问题 I have a class and one of its member functions is actually a function pointer. That way the user can overwrite what does this function do. I unfortunately have some difficulties running this function. I use g++ to compile. Below is the code: #include <iostream> using namespace std; double fcn_mod(const double &phit){ return 1.2+phit; } class Object{ public: Object() { fcn_ptr = (double (*)(const double &)) &Object::fcn_default; } double (*fcn_ptr)(const double &) = NULL; private: double fcn

static member function with C language binding?

雨燕双飞 提交于 2019-12-04 03:16:41
The following C++ code compiles with Visual C++ and g++: struct S { static void foo(); }; extern "C" void S::foo() {} struct T { static void foo(); }; extern "C" void T::foo() {} auto main() -> int { S().foo(); T().foo(); } Is it valid? If it's valid, since the implementation may be in a separate translation unit, does that imply that a static member function always has the same calling convention as a C function (and if not, how does it not imply that)? C++11 7.5/4 "Linkage specifications" A C language linkage is ignored in determining the language linkage of the names of class members and

ref-qualified member functions as template arguments?

我怕爱的太早我们不能终老 提交于 2019-12-04 03:01:49
This compiles fine in clang 3.3: template <typename T> struct M; template <typename R, typename C, typename... A> struct M <R (C::*)(A...)> { }; template <typename R, typename C, typename... A> struct M <R (C::*)(A...) &> { }; but fails in gcc 4.8.1: [...] error: redefinition of ‘struct M <R (C::*)(A ...)>’ struct M <R (C::*)(A...) &> { }; ^ [...] error: previous definition of ‘struct M <R (C::*)(A ...)>’ struct M <R (C::*)(A...)> { }; ^ When used in different contexts, this results in all sorts of unexpected compiler behaviour like crashing or internal compiler errors. I understand ref

const type qualifier soon after the function name [duplicate]

柔情痞子 提交于 2019-12-04 01:24:11
This question already has answers here : Closed 4 years ago . Meaning of 'const' last in a function declaration of a class? (9 answers) In C++ sometimes I see declarations like below: return_type function_name( datatype parameter1, datatype parameter2 ) const { /*................*/} What does this const type qualifier exact do in this case? $9.3.1/3 states- "A nonstatic member function may be declared const, volatile, or const volatile. These cvqualifiers affect the type of the this pointer (9.3.2). They also affect the function type (8.3.5) of the member function; a member function declared

Template member functions with trailing return type, giving errors even if unused

偶尔善良 提交于 2019-12-03 15:07:24
I understand that template member functions are only generated if used. This is convenient if not all used types support such a function. However, this does not appear to work for functions with trailing return type specification. Below is a small experiment: // helper function for case A workaround template <typename A, typename T> auto F(T&& x) -> decltype(x.template f <A>()) { return x.template f <A>(); } // helper function for case B workaround template <typename A, typename T> auto G(T&& x) -> decltype(x.g()) { return x.g(); } template <typename T> struct S { // case A: not ok in GCC +

Why can a static member function only be declared static inside the class definition and not also in its own definition?

我与影子孤独终老i 提交于 2019-12-03 10:44:36
While implementing a class for creating/updating boxes on the screen, I wanted to add a static member function that makes sure no currently visible boxes overlap (taking its information from a static pointer array to all currently visible boxes) My initial code had the following structure: class Box { public: // ... static void arrangeOverlappingBoxes(); }; static void Box::arrangeOverlappingBoxes() { // ... } I was quite surprised that this generated an error C2724: 'static' should not be used on member functions defined at file scope. With some trial, google and error, I figured out that my