member-functions

C++ volatile member functions

北城余情 提交于 2019-11-28 05:16:51
class MyClass { int x, y; void foo() volatile { // do stuff with x // do stuff with y } }; Do I need to declare x and y as volatile or will be all member variables treated as volatile automatically? I want to make sure that "stuff with x " is not reordered with "stuff with y " by the compiler. EDIT: What happens if I'm casting a normal type to a volatile type? Would this instruct the compiler to not reorder access to that location? I want to pass a normal variable in a special situation to a function which parameter is volatile. I must be sure compiler doesn't reorder that call with prior or

Does every c++ member function take `this` as an input implicitly?

≡放荡痞女 提交于 2019-11-28 02:29:14
问题 When we create a member function for a class in c++, it has an implicit extra argument that is a pointer to the calling object -- referred as this . Is this true for any function, even if it does not use this pointer. For example, given the class class foo { private: int bar; public: int get_one() { return 1; // Not using `this` } int get_bar() { return this->bar; // Using `this` } } Would both the functions ( get_one and get_bar ) take this as an implicit parameter, even though only one of

How to list the functions/methods of a javascript object? (Is it even possible?)

别来无恙 提交于 2019-11-27 23:50:07
问题 This question is intentionally phrased like this question. I don't even know if this is possible, I remember vaguely hearing something about some properties not enumerable in JS. Anyway, to cut a long story short: I'm developing something on a js framework for which I have no documentation and no easy access to the code, and it would greatly help to know what I can do with my objects. 回答1: I think this is what you are looking for: var obj = { locaMethod: function() { alert("hello"); }, a: "b"

How to directly bind a member function to an std::function in Visual Studio 11?

旧时模样 提交于 2019-11-27 22:53:05
I can easily bind member functions to a std::function by wrapping them with a lambda expression with capture clause. class Class { Class() { Register([=](int n){ Function(n); }); } void Register(std::function<void(int)> Callback) { } void Function(int Number) { } }; But I want to bind them directly, something like the following. // ... Register(&Class::Function); // ... I think according to the C++11 standard, this should be supported. However, in Visual Studio 11 I get these compiler errors. error C2440: 'newline' : cannot convert from 'int' to 'Class *' error C2647: '.*' : cannot dereference

error: default argument given for parameter 1

*爱你&永不变心* 提交于 2019-11-27 20:01:25
问题 I'm getting this error message with the code below: class Money { public: Money(float amount, int moneyType); string asString(bool shortVersion=true); private: float amount; int moneyType; }; First I thought that default parameters are not allowed as a first parameter in C++ but it is allowed. 回答1: You are probably redefining the default parameter in the implementation of the function. It should only be defined in the function declaration. //bad (this won't compile) string Money::asString

What are the rules for function pointers and member function pointers to Standard functions?

狂风中的少年 提交于 2019-11-27 18:38:34
问题 What are the existing rules for taking function pointers or member function pointers to Standard functions? For example, something like auto p = &std::string::size; Is this legal? Would it be more or less legal if I explicitly requested the correct type, so it would function even if there was an additional implementation-added overload of std::string::size ? 回答1: Using the "correct" type doesn't make things better: Except for the virtual functions all functions in the standard C++ library can

Get memory address of member function?

馋奶兔 提交于 2019-11-27 18:33:59
How do I get the absolute address of a member function in C++? (I need this for thunking.) Member function pointers don't work because I can't convert them to absolute addresses ( void * ) -- I need to know the address of the actual function in memory, not simply the address relative to the type. There exists a syntax to get the address of the member function in MSVC (starting from MSVC 2005 IMHO). But it's pretty tricky. Moreover, the obtained pointer is impossible to cast to other pointer type by conventional means. Though there exists a way to do this nevertheless. Here's the example: //

Why can some operators only be overloaded as member functions, other as friend functions and the rest of them as both?

廉价感情. 提交于 2019-11-27 18:19:11
Why can some operators only be overloaded as member functions, other as non-member "free" functions and the rest of them as both? What is the rationale behind those? How to remember which operators can be overloaded as what (member, free, or both)? Dietmar Kühl The question lists three classes of operators. Putting them together on a list helps, I think, with understanding why a few operators are restricted in where they can be overloaded: Operators which have to be overloaded as members. These are fairly few: The assignment operator=() . Allowing non-member assignments seems to open the door

Why doesn't the program crash when I call a member function through a null pointer in C++?

一世执手 提交于 2019-11-27 02:39:14
问题 #include "iostream" using namespace std; class A { public: void mprint() { cout<<"\n TESTING NULL POINTER"; } }; int main() { A *a = NULL; a->mprint(); return 0; } I am getting output as "TESTING NULL POINTER". Can anyone please explain why this program is printing the output instead of crashing. I checked it on Dev C++ and aCC compiler both gave same result. 回答1: You're not using any member variables of A - the function is completely independent of the A instance, and therefore the generated

How can C++ virtual functions be implemented except vtable? [duplicate]

假装没事ソ 提交于 2019-11-27 02:12:02
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: A question about virtual mechanism in C++ Is using vtable the only way to implement virtual member functions mechanism in C++? What other ways exist? 回答1: Another known mechanism is type dispatch functions. Effectively, you replace the vtable pointer by a typeid (small enum). The (dyanmic) linker collects all overrides of a given virtual function, and wraps them in one big switch statement on the typeid field.