class-members

Object oriented design suggestion

浪子不回头ぞ 提交于 2019-12-03 06:13:29
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 it a public member? Or I can encapsulate all these functions would make the implementation harder:

How can i modify variables in static member function?

≡放荡痞女 提交于 2019-12-02 10:13:29
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 main() { me M; M.X(); M.Y(); M.Z(); return 0; } I got this error : invalid use of member ‘me::x’ in static

Initializing a member class of an object using a non-default constructor in C++

寵の児 提交于 2019-12-02 06:52:11
问题 I have a specific situation where I've got an object that I want to use the boost random number generators on, and it has lead to a greater question which I cannot seem to answer. Here is the example code of what I'm trying to produce. First, my header: Class MyObject { protected: double some variable; boost::random::mt19937 rgenerator; boost::uniform_real<double> dist_0_1; boost::variate_generator< boost::mt19937&, boost::uniform_real<double> > rand01 } Now what I want to do is: Class

Self-referencing inside class definition

不问归期 提交于 2019-12-01 17:56:05
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: name 'A' is not defined As a result of my investigation i learned that globals() doesn't contain A

How to initialize the reference member variable of a class?

不问归期 提交于 2019-12-01 16:23:32
Consider the following code C++: #include<iostream> using namespace std; class Test { int &t; public: Test (int &x) { t = x; } int getT() { return t; } }; int main() { int x = 20; Test t1(x); cout << t1.getT() << " "; x = 30; cout << t1.getT() << endl; return 0; } It is showing the following error while using gcc compiler est.cpp: In constructor ‘Test::Test(int&)’: est.cpp:8:5: error: uninitialized reference member ‘Test::t’ [-fpermissive] Why doesn't the compiler directly call the Constructor? That is because references can only be initialized in the initializer list. Use Test (int &x) : t(x)

How to initialize the reference member variable of a class?

孤街浪徒 提交于 2019-12-01 15:25:06
问题 Consider the following code C++: #include<iostream> using namespace std; class Test { int &t; public: Test (int &x) { t = x; } int getT() { return t; } }; int main() { int x = 20; Test t1(x); cout << t1.getT() << " "; x = 30; cout << t1.getT() << endl; return 0; } It is showing the following error while using gcc compiler est.cpp: In constructor ‘Test::Test(int&)’: est.cpp:8:5: error: uninitialized reference member ‘Test::t’ [-fpermissive] Why doesn't the compiler directly call the

Name Lookup and class scope

删除回忆录丶 提交于 2019-12-01 10:53:23
Why is it that the return type of setVal is of type string and the parameter type is of type double typedef string Type; Type initVal(); class Exercise { public: typedef double Type; Type setVal(Type); Type initVal(); private: int val; }; Type Exercise::setVal(Type parm) { val = parm + initVal(); return val; } When member functions are defined in namespace scope C++ provides special name lookup rules for unqualified names that follow the function’s declarator-id (3.4.1/8). Such names are looked up in class scope before they are looked up in namespace scope. Since the return type in an

Name Lookup and class scope

非 Y 不嫁゛ 提交于 2019-12-01 08:07:58
问题 Why is it that the return type of setVal is of type string and the parameter type is of type double typedef string Type; Type initVal(); class Exercise { public: typedef double Type; Type setVal(Type); Type initVal(); private: int val; }; Type Exercise::setVal(Type parm) { val = parm + initVal(); return val; } 回答1: When member functions are defined in namespace scope C++ provides special name lookup rules for unqualified names that follow the function’s declarator-id (3.4.1/8). Such names are

Private inheritance: name lookup error

怎甘沉沦 提交于 2019-11-30 13:56:19
问题 I have the following code example that doesn't compile: #include <stdio.h> namespace my { class base1 { // line 6 }; class base2: private base1 { }; class derived: private base2 { public: // The following function just wants to print a pointer, nothing else! void print(base1* pointer) {printf("%p\n", pointer);} }; } The error that gcc prints is: test.cpp:6: error: `class my::base1' is inaccessible test.cpp:17: error: within this context Now, i can guess what the problem is: when looking at

Could we access member of a non-existing class type object?

这一生的挚爱 提交于 2019-11-29 07:39:34
问题 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. Does it allow us to