member

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

自古美人都是妖i 提交于 2019-11-27 11:31:02
问题 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); } 回答1: Your code is quite correct (it works), but you can use the

Trailing underscores for member variables in C++

时光毁灭记忆、已成空白 提交于 2019-11-27 10:59:46
问题 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

C++ member variable aliases?

你离开我真会死。 提交于 2019-11-27 10:39:06
问题 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

Can I access new methods in anonymous inner class with some syntax?

送分小仙女□ 提交于 2019-11-27 08:42:34
Is there any Java syntax to access new methods defined within anonymous inner classes from outer class? I know there can be various workarounds, but I wonder if a special syntax exist? For example class Outer { ActionListener listener = new ActionListener() { @Override void actionPerformed(ActionEvent e) { // do something } // method is public so can be accessible public void MyGloriousMethod() { // viva! } }; public void Caller() { listener.MyGloriousMethod(); // does not work! } } MY OWN SOLUTION I just moved all methods and members up to outer class. Once the anonymous class instance has

function pointer to a class member

蹲街弑〆低调 提交于 2019-11-27 08:26:28
问题 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 回答1: Since real_func is not a

What kind of prefix do you use for member variables?

走远了吗. 提交于 2019-11-27 07:05:32
No doubt, it's essential for understanding code to give member variables a prefix so that they can easily be distinguished from "normal" variables. But what kind of prefix do you use? I have been working on projects where we used m_ as prefix, on other projects we used an underscore only (which I personally don't like, because an underscore only is not demonstrative enough). On another project we used a long prefix form, that also included the variable type. mul_ for example is the prefix of a m ember variable of type u nsigned l ong. Now let me know what kind of prefix you use (and please

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

早过忘川 提交于 2019-11-27 06:34:12
问题 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? 回答1: 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

Declaring a member function in JS

巧了我就是萌 提交于 2019-11-27 06:15:37
问题 I've tried two ways to declare a member function in JS: function init() { var name = "Mozilla"; function displayName() { alert(name); } } a = new init(); a.displayName() And function init() { var name = "Mozilla"; displayName = function() { alert(name); } } a = new init(); a.displayName() The first method told me that displayName() is undefined . The way I see it a variable of type Function with nae displayName is created, and thus it should work. Any one care to explain why it didn't work?

How to calculate offset of a class member at compile time?

余生颓废 提交于 2019-11-27 05:49:50
问题 Given a class definition in C++ class A { public: //methods definition .... private: int i; char *str; .... } Is it possible to calculate the offset of a class member at compile time using C++ template meta-programming? The class is not POD, and can have virtual methods, primitive and object data member. 回答1: Based on Matthieu M.'s answer but shorter and with no macros: template<typename T, typename U> constexpr size_t offsetOf(U T::*member) { return (char*)&((T*)nullptr->*member) - (char*

C++ static template member, one instance for each template type?

十年热恋 提交于 2019-11-27 05:06:06
Usually static members/objects of one class are the same for each instance of the class having the static member/object. Anyways what about if the static object is part of a template class and also depends on the template argument? For example, like this: template<class T> class A{ public: static myObject<T> obj; } If I would cast one object of A as int and another one as float , I guess there would be two obj , one for each type? If I would create multiple objects of A as type int and multiple float s, would it still be two obj instances, since I am only using two different types? Static