class-members

Preferred way of class member initialization?

≯℡__Kan透↙ 提交于 2020-01-03 12:19:01
问题 class A { public: int x[100]; }; Declaring A a will not initialize the object (to be seen by garbage values in the field x ). The following will trigger initialization: A a{} or auto a = A() or auto a = A{} . Should any particular one of the three be preferred? Next, let us make it a member of another class: class B { public: A a; }; The default constructor of B appears to take care of initialization of a . However, if using a custom constructor, I have to take care of it. The following two

Preferred way of class member initialization?

匆匆过客 提交于 2020-01-03 12:18:06
问题 class A { public: int x[100]; }; Declaring A a will not initialize the object (to be seen by garbage values in the field x ). The following will trigger initialization: A a{} or auto a = A() or auto a = A{} . Should any particular one of the three be preferred? Next, let us make it a member of another class: class B { public: A a; }; The default constructor of B appears to take care of initialization of a . However, if using a custom constructor, I have to take care of it. The following two

How can i modify variables in static member function?

烈酒焚心 提交于 2019-12-31 05:29:13
问题 I have a code below, i want to modify class's variables in static function but there is some error. How can i fix it with "this" pointer? There is no access to "this" pointer for static members in class,on the other hand I am trying to make an access to class variables in Static member function, therefore i am looking for a way to use "this" pointer of class "me" to do it. class me { public: void X() { x = 1;} void Y() { y = 2;} static void Z() { x = 5 ; y = 10; } public: int x, y; }; int

How can I initialize C++ object member variables in the constructor?

大憨熊 提交于 2019-12-27 16:49:47
问题 I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O On StackOverflow, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this: class MyClass { public: MyClass(int n); private: AnotherClass another(100); // this constructs AnotherClass

Accessibility of data member in member function before declaration of data member

柔情痞子 提交于 2019-12-25 05:14:14
问题 Consider this code: class Test { public: Test() { i = 0; } private: int i; }; Data member 'i' is used even before it is declared/defined. Should this not be a compilation error ? (It compiled fine!!!) 回答1: The rule is that member functions defined in the class definition are compiled as if they were defined immediately after the class definition. 回答2: No it shouldn't, within the context of the class definition, all members, data members or functions have complete visibility. 回答3: where is the

Accessibility of data member in member function before declaration of data member

試著忘記壹切 提交于 2019-12-25 05:14:06
问题 Consider this code: class Test { public: Test() { i = 0; } private: int i; }; Data member 'i' is used even before it is declared/defined. Should this not be a compilation error ? (It compiled fine!!!) 回答1: The rule is that member functions defined in the class definition are compiled as if they were defined immediately after the class definition. 回答2: No it shouldn't, within the context of the class definition, all members, data members or functions have complete visibility. 回答3: where is the

What is the lifetime of the class data member which const reference to a rvalue?

只愿长相守 提交于 2019-12-22 08:57:59
问题 Generally this discussion is up to the local function variable only: void foo (const int &i) { // use i till foo() ends } foo(3); But, does this rule applies to the class member also ? struct A { const int &a; A () : a(3) {} // version 1 A (const int &i) : a(i) {} // version 2 }; Now A used as, { return ()? new A : new A(3) : new A(some_local_variable); } Will the contents of a remain same through out the life time of the all 3 new ly allocated A ? 回答1: The C++03 standard ( Section "12.2/5

Can we access a member of a non-existing union?

别来无恙 提交于 2019-12-22 06:45:30
问题 In the c++ standard, in [basic.lval]/11.6 says: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:[...] an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),[...] This sentence is part of the strict-aliasing rule. Can it allow us to

In .NET, can you use reflection to get all non-inherited methods of a class?

梦想与她 提交于 2019-12-21 06:57:28
问题 Because of this issue here, I'm trying to write a custom JsonConverter that handles cases where you subclass a list or a collection, then add extra properties to it. As such, one approach would be to ignore all base-class properties and only serialize those in the defined class. (Technically this won't work because if you subclass that subclass you break the serialization, but it did make me wonder...) Is it possible via reflection (well I know the answer is 'yes' because Reflector does

Multiple Inheritance Template Class

假装没事ソ 提交于 2019-12-19 06:29:19
问题 class messageA { }; class messageB { }; template<class T> class queue { public: virtual ~queue() {} void submit(T& x) {} }; class A : public queue<messageA>, public queue<messageB> { }; int main() { A aa; aa.submit(messageA()); aa.submit(messageB()); } My first thought is, the above code should be fine, as class A will contains 2 overloaded submit functions, which will accept messageA and messageB object. However, the compiler gives me the following error : May I know why there is an