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.)

169 questions
2
votes
2 answers

Java inheritance checking values for multiple variables

If I have a hierarchy situation like this: class foo1{ Foo2 foo2; } class foo2 { List foo3; } class foo3 { } class foo4 extends foo3 { Foo5 foo; } class foo5 { double value; } I want to get that final double value but to…
Richard
  • 5,840
  • 36
  • 123
  • 208
2
votes
0 answers

C++ class member variable resetting to 0

I'm trying to increment and decrement the index of a list of readings, however, everytime I run update(), m_notifIndex gets set back to zero. I'm sorry if this type of question has been asked before, but after looking for the answer for an hour, I…
sadmansk
  • 21
  • 1
  • 1
2
votes
1 answer

How to declare member variable of subclass in Theos

For example, when I declare subclass of existing class, I can write as below in theos: %subclass NEWCLASS: EXISTINGCLASS - (void)overridemethod { //code } %new(v@:) - (void)newmethod { //code } %end But I don't know how to declare member or…
user3336535
2
votes
5 answers

Java non-static member variable initialization

This is probably a 'duplicate', but I'm not sure how to search for this question... I am initializing a non-static member variable at the declaration line: public class A { private B b = new B(); ... } I am doing it instead of initializing…
barak manos
  • 29,648
  • 10
  • 62
  • 114
2
votes
6 answers

Local variables or member variables in Java

I have a question about local and member variables in Java. The situation is: Sometimes if I define a local variable, that variable has to be passed around into several levels of method calls. I often think, why should I just define a member…
user697911
  • 10,043
  • 25
  • 95
  • 169
2
votes
4 answers

Java Access Abstract Instance variables

I have an abstract class with a variable like follows: public abstract class MyAbstractClass { int myVariable = 1; protected abstract void FunctionThatUsesMyVariable(); } Then when I go to instantiate my class through the following code,…
btalb
  • 6,937
  • 11
  • 36
  • 44
2
votes
4 answers

C++ : forbid a class to change the value pointed to by a pointer member variable

I apologize if the title sounds abstruse, let me make that simple with an example : class A { public: A(int *flag) : flag_(flag) {} void foo(); private: void bar(); int *flag_; }; The question is: can I prevent this class from…
Seub
  • 2,451
  • 4
  • 25
  • 34
1
vote
3 answers

How to foreach through part of an objects member variables that are arrays?

I am trying to create a foreach that will go through some variables within an object. At the moment it is just class jabroni { var $name = "The Rock"; var $phrases = array ("The rock says", "Im gonna put the smackdown on you", "Bring it on…
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119
1
vote
2 answers

What is happening in the background when a member variable is initialized within a class but not with the help of the constructor?

Could anyone explain me the following C# sample code? public class MyTestClass { private int x = 100; private int y; public MyTestClass { y = 200; } } I understand that when MyTestClass is instantiated, the…
gvg
  • 23
  • 3
1
vote
1 answer

Why does the address value of a member variable declared as an object in a class always change? in c++

I am studying about the class of c++. I am writing a class named User. The member variable was declared as an object in the class. The function that returns the address of this object is getAddr(). For your information, the getAddr() function also…
nonalias
  • 11
  • 1
1
vote
1 answer

Inferring type and class when passing a pointer to data member as a non-type template argument

I need to pass a pointer to data member, its type and the class it's a member of, to a template struct. The following works: template struct Member {}; struct Struct { int x; }; Member
1
vote
1 answer

C++: How to make the compiler optimize memory access in case when a pointer of a member variable is passed elsewhere

[edit: Here is the motivation: passing a pointer of a variable to an external function may accidentally break some optimization for "adjacent" variables, because of the possibility to get pointers to the adjacent variables calculated from the…
zwhconst
  • 1,352
  • 9
  • 19
1
vote
1 answer

Java: Calling a parent's method with child's member variable

Let's say I have an abstract parent class that has member variables which are used in a method. public abstract class Person{ public String jobTitle; public void printJob(){ System.out.println(jobTitle); } } If I now have two…
megabam5
  • 21
  • 1
1
vote
1 answer

Sorting function for a vector of objects based on different fields

I have a vector of objects: struct Student{    string name;    string id;    string major; int age; }; vector s; Is there any way to write ONE general (maybe template) function to sort this vector (or array) based on different…
user3112666
  • 1,689
  • 1
  • 12
  • 12
1
vote
1 answer

Safely declaring a RecyclerView object: as a field of the activity or as a local variable of the onCreate() method?

Initial code: public class SummaryActivity extends AppCompatActivity { private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); …