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.)
Questions tagged [member-variables]
169 questions
155
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
- 4,118
- 7
- 28
- 30
93
votes
10 answers
Private members in Python
How can I make methods and data members private in Python? Or doesn't Python support private members?

appusajeev
- 2,129
- 3
- 22
- 20
68
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
- 20,411
- 61
- 165
- 254
61
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
43
votes
5 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
- 937
- 9
- 18
27
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,198
- 1
- 18
- 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
- 24,113
- 33
- 111
- 170
19
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
- 195
- 1
- 1
- 5
17
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
- 483
- 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
- 61,774
- 108
- 250
- 348
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
- 68,093
- 33
- 169
- 336
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
- 41,123
- 68
- 193
- 295
10
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,219
- 1
- 20
- 40
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.
- 11,773
- 8
- 58
- 101
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
- 389
- 2
- 11