Questions tagged [member-variables]

A member variable is a member of a type and belongs to that type's state. (Contrast with a "local variable", defined inside a method.)

159 questions
135
votes
5 answers

What is the difference between class and instance attributes?

Is there any meaningful distinction between: class A(object): foo = 5 # some default value vs. class B(object): def __init__(self, foo=5): self.foo = foo If you're creating a lot of instances, is there any difference in…
Dan Homerick
  • 3,828
  • 7
  • 26
  • 29
70
votes
9 answers

Private members in Python

How can I make methods and data members private in Python? Or doesn't Python support private members?
appusajeev
  • 1,653
  • 3
  • 16
  • 19
55
votes
7 answers

Python Class Members

I am just learning Python and I come from a C background so please let me know if I have any confusion / mix up between both. Assume I have the following class: class Node(object): def __init__(self, element): self.element = element …
darksky
  • 18,334
  • 56
  • 153
  • 244
54
votes
3 answers

Is returning references of member variables bad practice?

The following is said to be better than having First() and Second() as public members. I believe this is nearly as bad. // Example 17-3(b): Proper encapsulation, initially with inline accessors. Later // in life, these might grow into nontrivial…
user34537
35
votes
4 answers

Can member variables be used to initialize other members in an initialization list?

Consider the following (simplified) situation: class Foo { private: int evenA; int evenB; int evenSum; public: Foo(int a, int b) : evenA(a-(a%2)), evenB(b-(b%2)), evenSum(evenA+evenB) { } }; When i instanciate Foo like…
Pontomedon
  • 837
  • 9
  • 18
24
votes
2 answers

Is C++ static member variable initialization thread-safe?

According to following resources, in C++(Specially Visual C++) scoped static variable initialization isn't thread safe. But, global static variables are safe. Thread-safe static variables without…
Varuna
  • 1,148
  • 1
  • 17
  • 19
20
votes
6 answers

Is it okay to forgo getters and setters for simple classes?

I'm making a very simple class to represent positions in 3D space. Currently, I'm just letting the user access and modify the individual X, Y and Z values directly. In other words, they're public member variables. template
Maxpm
  • 20,568
  • 29
  • 91
  • 159
17
votes
5 answers

When and why to declare member variables on the heap C++

Ok, so I'm very new at C++ programming, and I've been looking around for a couple days for a decisive answer for this. WHEN should I declare member variables on the heap vs. the stack? Most of the answers that I've found have dealt with other…
Ben Dixon
  • 175
  • 1
  • 5
15
votes
1 answer

Adding new member variables to python objects?

I have started reading the "Beginning python from novice to professional" by Magnus Lie Hetland, and what struck me today is an ability of an objects to create new member variables, even though those member variables were not present in the class…
stgeorge
  • 453
  • 3
  • 7
  • 16
12
votes
7 answers

How to implement a read-only member variable in PHP?

When trying to change it,throw an exception.
user198729
  • 55,886
  • 102
  • 239
  • 342
11
votes
3 answers

How to invoke pointer to member function when it's a class data member?

struct B { void (B::*pf)(int, int); // data member B () : pf(&B::foo) {} void foo (int i, int j) { cout<<"foo(int, int)\n"; } // target method }; int main () { B obj; // how to call foo() using obj.pf ? } In above test code, pf is a…
iammilind
  • 62,239
  • 27
  • 150
  • 297
11
votes
5 answers

Suppress unused variable warning in C++ => Compiler bug or code bug?

Presently, I am using the following function template to suppress unused variable warnings: template void unused(T const &) { /* Do nothing. */ } However, when porting to cygwin from Linux, I am now getting compiler errors on g++…
WilliamKF
  • 36,283
  • 61
  • 170
  • 271
9
votes
4 answers

Is it possible to write copy constructors for classes with interface member variables in Java?

How would you write a copy constructor for a class with interface member variables? For instance: public class House{ // IAnimal is an interface IAnimal pet; public House(IAnimal pet){ this.pet = pet; } // my…
chessofnerd
  • 1,148
  • 17
  • 32
9
votes
2 answers

C++ init-list: using non-initialized members to initialize others gives no warning

Neither g++ (4.4 and 4.6) nor clang++ (3.2) nor coverity, with -Wall and -Wextra (+ some others) or -Weverything respectively gives me a warning for the following code snippet: class B { char *t2; char *t; public: B() : t2(t), t(new…
Patrick B.
  • 10,471
  • 7
  • 49
  • 85
8
votes
4 answers

C++: Reference/pointer to member variable as template parameter

To start, I have something like this: class Test { std::vector a, b; void caller(...) { callee(...); } void callee(...) { /* Do stuff with 'a' */ } } What I wanted is to have a function that does exactly the same as callee but for…
gmardau
  • 339
  • 1
  • 9
1
2 3
10 11