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
3
votes
2 answers
Put a parenthesis after member variable to initialize?
I have seen people put a parenthesis after the member variable in the initialization list. I wonder why would people do that?
For example, I have a STL container in header file:
class A{
public:
A();
...
private:
vector…

HoKy22
- 4,057
- 8
- 33
- 54
3
votes
11 answers
C++: Copy constructor: Use getters or access member vars directly?
I have a simple container class with a copy constructor.
Do you recommend using getters and setters, or accessing the member variables directly?
public Container
{
public:
Container() {}
Container(const Container& cont) //option…

cbrulak
- 15,436
- 20
- 61
- 101
2
votes
5 answers
Inherited class initializing a custom made class using non-default constructor
So I have searched all over the place and I can not seem to find the answer to this specific question. I am using a winXP with cygwin and gcc 3.4.4 cygming special.
Problem:
I have a class that works as an interface with some abstract methods and…

tomasgudm
- 87
- 3
2
votes
1 answer
C++ Class Member Variables Resetting After Loop?
After getting help on this question, I was led to do better debugging. In that process, I discovered that my problem is this:
While working in C++, attempting to set a class' member variable to a value works, but not while looping. I have reduced my…

Gaffi
- 4,307
- 8
- 43
- 73
2
votes
4 answers
C++ Object references as member variables of objects
I'm kinda new to C++ and I've run into a problem with setting up the main architecture. I learned how to use this specific architecture in C#, but I can't get it to work in C++.
My problem is as follows: I have 2 objects. I want these objects to…

Maxim Schoemaker
- 131
- 3
- 9
2
votes
0 answers
C++ Thread_local non-static member of class?
I'm trying to have one member variable of a class thread local. However it seems that thread_local in a class has to also be static..? But static would mean that all instances of that class share that variable. I want it so there can be different…

Alasdair
- 13,348
- 18
- 82
- 138
2
votes
2 answers
Aren't class member varialbes as bad as global variable?
We all know the following code is bad because of the use of global variable:
a = 3.14
def inc():
global a
a += 1
inc()
b = a
It is so bad that in python one has to intentionally declare global to "break" the ban. But in the name of OO, how…

lunchbreak
- 131
- 1
- 6
2
votes
4 answers
How do member variables work with specialized class templates?
I'm trying to write a very simple specialized class template that has a member variable and can print that member variable differently in specialized situations. I know the example is pretty useless, but it illustrates the question pretty well.
When…

tjwrona1992
- 8,614
- 8
- 35
- 98
2
votes
1 answer
how to declare a var of the type of a member var of some struct?
I want to get codes like this:
struct Order_t {
time_point order_time;
// some other fileds
};
template
void onTimer( time_point tp_now ) {
auto tp0 = …

Leon
- 1,489
- 1
- 12
- 31
2
votes
1 answer
C++ state machine, inherited class with member values with incorrect syntax
I don't know if my question title makes sense, so apologies in advance for that. so... I'm trying to implement a state machine for a little game I'm trying to make using C++ and SFML.
I have a GameLoopObject abstract class which needs a renderwindow…

Caedendi
- 37
- 6
2
votes
1 answer
std::map of polymorphic member variables pointers
I'm working on implementing a map of member variables pointers associated with a string key.
All variables range from a base class "BaseA"
When accessing the variables from the map, it is only required to use the base class methods (getDesc () in…

SNJ
- 171
- 7
2
votes
3 answers
C++ Initialize const class member variable in header file or in constructor?
I just wanted to ask what's the best practice for initializing const class member variables in C++, in the header file or in the constructor?
Thanks : )
In the header file:
.h file:
class ExampleClass{
public:
ExampleClass();
private:
const…

JilReg
- 382
- 1
- 3
- 16
2
votes
3 answers
mem_fn to function of member object
I was tinkering around with the std::mem_fn and couldn't manage to bind it to data/functions of an member of a struct (one layer deeper).
I hope that the code shows the problem better than I can describe it, because I'm not familiar with the…

Thomas B.
- 691
- 4
- 15
2
votes
1 answer
F# make member value based on member function
I have made a member function in a class. Afterwards I want to make a member value that is set to the result of this member function.
type MyType() =
member this.drawFilledPlanet(xCoord:int, yCoord:int, pWidth:int, pHeight:int, color) =
let…

Nulle
- 1,301
- 13
- 28
2
votes
1 answer
Referencing a pointer in a C++ member function
I'm writing a member function that uses a member variable pointer as an iterator. However I want to reference the pointer within the function purely for readability's sake. Like so:
/* getNext will return a pos object each time it is called for each…

Rhys van der Waerden
- 3,526
- 2
- 27
- 32