Questions tagged [class-members]

This tag refers to members of a class in object-oriented language. These are fields, constructors, destructors, methods and, usually in higher-level languages, properties and events.

refers to members of a class in object-oriented language. These are fields, constructors, destructors, methods and, usually in higher-level languages, properties and events.

171 questions
144
votes
10 answers

Should I prefer pointers or references in member data?

This is a simplified example to illustrate the question: class A {}; class B { B(A& a) : a(a) {} A& a; }; class C { C() : b(a) {} A a; B b; }; So B is responsible for updating a part of C. I ran the code through lint and it…
markh44
  • 5,344
  • 5
  • 25
  • 33
90
votes
3 answers

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

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…
Joseph Mansfield
  • 100,738
  • 18
  • 225
  • 303
89
votes
5 answers

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

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. I thought…
Logical Fallacy
  • 2,619
  • 4
  • 22
  • 37
44
votes
2 answers

Initialization Order of Class Data Members

In the following code, when the ctor of X is called will the ctor of A or B be called first? Does the order in which they are placed in the body of the class control this? If somebody can provide a snippet of text from the C++ standard that talks…
Nikhil
  • 2,050
  • 6
  • 31
  • 50
37
votes
6 answers

How can I access a classmethod from inside a class in Python

I would like to create a class in Python that manages above all static members. These members should be initiliazed during definition of the class already. Due to the fact that there will be the requirement to reinitialize the static members later…
Volker
  • 373
  • 1
  • 3
  • 5
37
votes
5 answers

GCC issue: using a member of a base class that depends on a template argument

The following code doesn't compile with gcc, but does with Visual Studio: template class A { public: T foo; }; template class B: public A { public: void bar() { cout << foo << endl; } }; I get the…
Jesse Beder
  • 30,017
  • 18
  • 97
  • 140
21
votes
4 answers

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

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…
Mark A. Donohoe
  • 23,825
  • 17
  • 116
  • 235
15
votes
9 answers

Object oriented design suggestion

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: …
pocoa
  • 3,749
  • 9
  • 35
  • 44
13
votes
1 answer

Private inheritance: name lookup error

I have the following code example that doesn't compile: #include namespace my { class base1 { // line 6 }; class base2: private base1 { }; class derived: private base2 { public: // The…
anatolyg
  • 23,079
  • 7
  • 51
  • 113
12
votes
2 answers

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

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…
Oliv
  • 16,492
  • 1
  • 24
  • 63
12
votes
3 answers

VS IntelliSense - IFluentInterface / IHideObjectMembers trick does not work. Why?

The IHideObjectMembers trick (a.k.a IFluentInterface) can be used e.g. in fluent interface implementations to hide System.Object members from IntelliSense. (If you don't know this trick, you can read up on it via the above link; I'm just repeating…
12
votes
2 answers

How to initialize the reference member variable of a class?

Consider the following code C++: #include 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() << "…
banarun
  • 2,239
  • 1
  • 21
  • 40
11
votes
4 answers

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

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…
yolo
  • 2,655
  • 6
  • 34
  • 60
10
votes
1 answer

Creating a function handle to an overloaded `end` function

MATLAB allows overloading various operators for custom classes. One of the unlisted overloadable operators is end, as can be learned from \matlab\lang\end.m: % END(A,K,N) is called for indexing expressions involving the object A % when END is…
Dev-iL
  • 22,722
  • 7
  • 53
  • 89
10
votes
3 answers

Array as a Class Member

I'm designing a dynamic buffer for outgoing messages. The data structure takes the form of a queue of nodes that have a Byte Array buffer as a member. Unfortunately in VBA, Arrays cannot be public members of a class. For example, this is a no-no…
Blackhawk
  • 5,605
  • 3
  • 25
  • 53
1
2 3
11 12