member

what is a member vs. a property

為{幸葍}努か 提交于 2019-11-28 22:32:46
A friend who is new to OO programming asked me the difference between a Member and Property, and I was ashamed to admit that I couldn't give him a good answer. Since properties can also be objects themselves, I was left with a general description and list of exceptions. Can somebody please lay out a good definition of when to consider something a member vs. a property? Maybe I'm bastardizing the concept, or is it just that a member is just the internal name I use, and the property is what's exposed to other objects? I don't think that not knowing the answer to this question has affected the

Pointer to class member as template parameter

大兔子大兔子 提交于 2019-11-28 22:17:28
问题 Is it possible to have non-type template parameter which is actually a pointer to a class member? What I'm looking to do is something like the following: struct Person { Dog dog; }; template <?? ptr> struct Strange { // ... }; typedef Strange<&Person::dog> weird; My work so far leads me to believe that nothing of the sort is possible, but I'm curious if anyone has can say otherwise. 回答1: From the standard: A non-type template-parameter shall have one of the following (optionally cv-qualified)

How to initialize a shared_ptr that is a member of a class?

。_饼干妹妹 提交于 2019-11-28 18:36:45
I am not sure about a good way to initialize a shared_ptr that is a member of a class. Can you tell me, whether the way that I choose in C::foo() is fine, or is there a better solution? class A { public: A(); }; class B { public: B(A* pa); }; class C { boost::shared_ptr<A> mA; boost::shared_ptr<B> mB; void foo(); }; void C::foo() { A* pa = new A; mA = boost::shared_ptr<A>(pa); B* pB = new B(pa); mB = boost::shared_ptr<B>(pb); } Your code is quite correct (it works), but you can use the initialization list, like this: C::C() : mA(new A), mB(new B(mA.get()) { } Which is even more correct and as

Trailing underscores for member variables in C++

百般思念 提交于 2019-11-28 17:46:58
I've seen people use a trailing underscore for member variables in classes, for instance in the renowned C++ FAQ Lite . I think that it's purpose is not to mark variables as members, that's what "m_" is for. It's actual purpose is to make it possible to have an accessor method named like the field, like this: class Foo { public: bar the_bar() { return the_bar_; } private: bar the_bar_; } Having accessors omit the "get_" part is common in the STL and boost, and I'm trying to develop a coding style as close to these as possible, but I can't really see them using the underscore trick. I wasn't

C++ member variable aliases?

北战南征 提交于 2019-11-28 17:39:37
I'm pretty sure this is possible, because I'm pretty sure I've seen it done. I think it is awesome, but I will gladly accept answers along the lines of "this is a terrible idea because ____". Say we have a basic struct. struct vertex { float x, y, z; }; Now, I want to implement aliases on these variables. vertex pos; vertex col; vertex arr; pos.x = 0.0f; pos.y = 0.5f; pos.z = 1.0f; col.r = 0.0f; col.g = 0.5f; col.b = 1.0f; arr[0] = 0.0f; arr[1] = 0.5f; arr[2] = 1.0f; Ideally the third syntax would be indistinguishable from an array. That is, if I sent arr as a reference parameter to a function

If I delete a class, are its member variables automatically deleted?

。_饼干妹妹 提交于 2019-11-28 17:33:00
I have been researching, and nothing relevant has come up, so I came here. I am trying to avoid memory leaks, so I am wondering: Say I have class MyClass with member int s a and b , and an int array c , which are filled in a member function: class MyClass { public: int a, b; int c[2]; void setVariables() { a, b = 0; for (int i = 0; i < 2; i++) { c[i] = 3; } } }; int main(int argc, char* argv[]) { MyClass* mc = new MyClass(); mc->setVariables(); delete mc; } Now, after I call delete mc , will a , b , and all the contents of c be deleted as well? Or will I have to do that explicitly in the

function pointer to a class member

五迷三道 提交于 2019-11-28 14:27:06
I am trying to do some like this: class A { void *(*func)(void *); A(void *(*function)(void *)){ func = function; } } class B { void *real_func(void *); A ptr; B() :ptr(&real_func) { ... } } But I get this error: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Someone knows how to initialize the function pointer to a function member in the same class??? Thanks! Carlos Since real_func is not a static member function, its type cannot be void *(*)() . Instead, it is void *(B::*)() so you need to declare

How to check if a member name (variable or function) exists in a class, with or without specifying type?

时光怂恿深爱的人放手 提交于 2019-11-28 14:03:06
This Q is an extension of: Is it possible to write a C++ template to check for a function's existence? Is there any utility which will help to find: If a member name exists inside a class or not? This member can be a variable or a method. Specifying the type of the member should be optional With std::experimental::is_detected and std::experimental::disjunction you could do this: //check for a type member named foo template <typename T> using foo_type_t = typename T::foo; //check for a non-type member named foo template <typename T> using foo_non_type_t = decltype(&T::foo); template <typename T

this pointer and member function address

[亡魂溺海] 提交于 2019-11-28 12:37:32
问题 I'm trying to get the address of a member function, but I don't know how. I would appreciate if somebody could tell me what I'm doing wrong. As you can see in my example below, neither (long)&g nor (long)&this->g work and I can't figure out the correct syntax: /* Create a class that (redundantly) performs data member selection and a member function call using the this keyword (which refers to the address of the current object). */ #include<iostream> using namespace std; #define PR(STR) cout <

What is the default value of a member in an array?

泄露秘密 提交于 2019-11-28 11:52:59
I instantiate an array like this: int array[] = new int[4]; What are the default values for those four members? Is it null, 0 or not exists? It's 0. It can't be null, as null isn't a valid int value. From section 7.6.10.4 of the C# 5 specification: All elements of the new array instance are initialized to their default values (§5.2). And from section 5.2: The default value of a variable depends on the type of the variable and is determined as follows: For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (§4.1.2). For a