class-members

C++11 allows in-class initialization of non-static and non-const members. What changed?

与世无争的帅哥 提交于 2019-12-17 02:33:43
问题 Before C++11, we could only perform in-class initialization on static const members of integral or enumeration type. Stroustrup discusses this in his C++ FAQ, giving the following example: class Y { const int c3 = 7; // error: not static static int c4 = 7; // error: not const static const float c5 = 7; // error: not integral }; And the following reasoning: So why do these inconvenient restrictions exist? A class is typically declared in a header file and a header file is typically included

Java only static members in main class

旧巷老猫 提交于 2019-12-13 06:41:01
问题 Is it bad practice to, in the class which contains the main method, declare all members of that class as static? If so, why? Is it better to, in the main method, create a new instance of the enclosing class, and run the program from the constructor so to speak? EDIT: (clarification) I know the concept about static and singletons and generally when to use it. But this question regards specifically the main-class of a program. @Andrew Tobilko (who apparently removed his answer..) seems to have

Is order in memory guaranteed for class private members in C++?

守給你的承諾、 提交于 2019-12-11 15:34:51
问题 class my_class_t { private: uint64_t field1; uint64_t field2; }; Is order of field1 and field2 guaranteed in memory by C++ Standard? UPD. Answers said that field2 it is, but &field2 may be not equal to &field1 + 1 . How to ensure that field2 will be immediately after field1 ? 回答1: They are guaranteed to have increasing addresses with respect to each other ([class.mem]/13): Nonstatic data members of a (non-union) class with the same access control (Clause [class.access]) are allocated so that

Default constructor for a class with a reference data member?

不想你离开。 提交于 2019-12-10 10:09:03
问题 I have a class MyClass in which I need to create a std::array of std::vector in the default constructor. However, this class has a data member which is a reference (of type Something ) which also needs to be initialized in the constructor and I cannot do this in a default constructor. How should I solve this? class MyClass{ public: MyClass(); //Cannot instantiate s?? MyClass(Something& s); Something& s; } MyClass array[10]; // MyClass needs a default constructor but a default // constructor

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

喜欢而已 提交于 2019-12-05 19:38:09
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 ? The C++03 standard ( Section "12.2/5 Temporary objects" ) answers your question aptly: The temporary to which the reference is bound or the

Default constructor for a class with a reference data member?

不羁岁月 提交于 2019-12-05 18:59:10
I have a class MyClass in which I need to create a std::array of std::vector in the default constructor. However, this class has a data member which is a reference (of type Something ) which also needs to be initialized in the constructor and I cannot do this in a default constructor. How should I solve this? class MyClass{ public: MyClass(); //Cannot instantiate s?? MyClass(Something& s); Something& s; } MyClass array[10]; // MyClass needs a default constructor but a default // constructor won't be able to initialize s A class with a reference member needs to set the reference in its

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

你。 提交于 2019-12-05 09:32:27
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 access the inactive member of a non existing union? As in: struct A{ int id :1; int value :32; }; struct

Self-referencing inside class definition

喜欢而已 提交于 2019-12-04 03:26:19
问题 How do I reference class object inside class definition? Could you advice me how you would do it? Or more specifically how do you pass class object inside decorator of class method? Here is a simple example, I'm trying to pass second method I'm declaring to decorator of first one. def decorate(w): def _wrap(f): def _call(*args, **kwargs): return w(f(*args, **kwargs)) def _call return _wrap class A(): @dec(A.w) def f(): return 2 def w(f): return fr + 5 As expected exception is raised NameError

Object oriented design suggestion

佐手、 提交于 2019-12-03 15:46:45
问题 Here is my code: class Soldier { public: Soldier(const string &name, const Gun &gun); string getName(); private: Gun gun; string name; }; class Gun { public: void fire(); void load(int bullets); int getBullets(); private: int bullets; } I need to call all the member functions of Gun over a Soldier object. Something like: soldier.gun.fire(); or soldier.getGun().load(15); So which one is a better design? Hiding the gun object as a private member and access it with getGun() function. Or making

C++ define class member struct and return it in a member function

末鹿安然 提交于 2019-12-03 06:29:17
My goal is a class like: class UserInformation { public: userInfo getInfo(int userId); private: struct userInfo { int repu, quesCount, ansCount; }; userInfo infoStruct; int date; }; userInfo UserInformation::getInfo(int userId) { infoStruct.repu = 1000; return infoStruct; } but the compiler gives error that in defintion of the public function getInfo(int) the return type userInfo is not a type name. You need to change the order of the members of UserInformation and put struct UserInfo above the declaration of getInfo . The compiler complains that it can't work out the signature for getInfo