member-functions

C++ volatile member functions

拈花ヽ惹草 提交于 2019-11-27 00:42:16
问题 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

Effective C++ Item 23 Prefer non-member non-friend functions to member functions

折月煮酒 提交于 2019-11-26 18:41:56
While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first hand with the web browser example made some sense, however convenience functions( named the nonmember functions like this in the book) in that example change the state of the class, don't they? So, first question, should not they be members then? Reading a bit further, he considers the STL functions and indeed some functions which are not implemented

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

和自甴很熟 提交于 2019-11-26 16:49:48
问题 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.

Nonstatic member as a default argument of a nonstatic member function

最后都变了- 提交于 2019-11-26 09:25:49
问题 struct X { X():mem(42){} void f(int param = mem) //ERROR { //do something } private: int mem; }; Can anyone give me just one reason as to why this is illegal in C++?! That is to say, I know that it is an error, I know what the error means, I just can\'t understand why would this be illegal! 回答1: Your code (simplified): struct X { int mem; void f(int param = mem); //ERROR }; You want to use a non-static member data as default value for a parameter of a member function. The first question which

Effective C++ Item 23 Prefer non-member non-friend functions to member functions

a 夏天 提交于 2019-11-26 06:32:12
问题 While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first hand with the web browser example made some sense, however convenience functions( named the nonmember functions like this in the book) in that example change the state of the class, don\'t they? So, first question, should not they be members then?

What are all the member-functions created by compiler for a class? Does that happen all the time?

丶灬走出姿态 提交于 2019-11-25 23:49:00
问题 What are all the member-functions created by compiler for a class? Does that happen all the time? like destructor. My concern is whether it is created for all the classes, and why is default constructor needed? 回答1: C++98/03 If they are needed, the compiler will generate a default constructor for you unless you declare any constructor of your own. the compiler will generate a copy constructor for you unless you declare your own. the compiler will generate a copy assignment operator for you

Operator overloading : member function vs. non-member function?

空扰寡人 提交于 2019-11-25 22:44:28
问题 I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand, overloaded operator declared as a friend is symmetric because we pass two arguments of the same type and hence, they can be compared. My question is that when i can still compare a pointer\'s lvalue to a reference, why are friends preferred? (using an