Questions tagged [member]

A member is an element of an object in the object-oriented programming paradigm.

A member is an element of an object in the object-oriented programming paradigm. Member variables are often called fields, while member functions are also called methods.

1754 questions
32
votes
5 answers

Swift Error: Ambiguous reference to member 'subscript'

I'm new to coding and picked up some open source project to get the idea. I'm getting the error: Ambiguous reference to member 'subscript' in the code below: let pictures = ( selectedRestaurant["Pictures"] as! NSArray ) // Error let picture = (…
jonasdickel
  • 321
  • 1
  • 3
  • 4
32
votes
5 answers

How to access a private member inside a static function in PHP

I have the following class in PHP class MyClass { // How to declare MyMember here? It needs to be private public static function MyFunction() { // How to access MyMember here? } } I am totally confused about which syntax to…
anon355079
31
votes
3 answers

pointer to const member function typedef

I know it's possible to separate to create a pointer to member function like this struct K { void func() {} }; typedef void FuncType(); typedef FuncType K::* MemFuncType; MemFuncType pF = &K::func; Is there similar way to construct a pointer to a…
oldcig
  • 313
  • 1
  • 3
  • 4
29
votes
3 answers

C++ Static Const Member Variable Usage

Say that I have a class that requires a few constants to function. Several member functions require use of these constants. Use of #define is frowned upon since it can cause collisions. The constants are hex patterns of 8 or 16 bits and are…
It'sPete
  • 5,083
  • 8
  • 39
  • 72
27
votes
6 answers

How do you pass a member function pointer?

I am trying to pass a member function within a class to a function that takes a member function class pointer. The problem I am having is that I am not sure how to properly do this within the class using the this pointer. Does anyone have…
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
26
votes
4 answers

How to calculate offset of a class member at compile time?

Given a class definition in C++ class A { public: //methods definition .... private: int i; char *str; .... } Is it possible to calculate the offset of a class member at compile time using C++ template meta-programming? The…
Jun Yuan
  • 314
  • 1
  • 3
  • 5
24
votes
5 answers

Cycle in the struct layout that doesn't exist

This is a simplified version of some of my code: public struct info { public float a, b; public info? c; public info(float a, float b, info? c = null) { this.a = a; this.b = b; this.c = c; } } The…
alan2here
  • 3,223
  • 6
  • 37
  • 62
24
votes
5 answers

Why can’t this static inner class call a non-static method on its outer class?

I'm currently reading Effective Java by Joshua Bloch and I love it! But on page 112 (Item 24) Bloch writes: A static member class is the simplest kind of nested class. It is best thought of as an ordinary class that happens to be declared inside …
Robin Dos Anjos
  • 309
  • 2
  • 11
24
votes
6 answers

C++ Member Variables

Consider the following class: class A { A(); int number; void setNumber(int number); }; You could implement 'setNumber' in 3 ways: Method 1: Use the 'this' pointer. void A::setNumber(int number) { this->number = number; } Method 2: Use the…
Julian
  • 1,688
  • 1
  • 12
  • 19
23
votes
11 answers

Immutable Object with ArrayList member variable - why can this variable be changed?

I have got one class with various member variables. There is a constructor and there are getter-methods, but no setter-methods. In fact, this object should be immutable. public class Example { private ArrayList list; } Now I noticed the…
nano7
  • 2,455
  • 7
  • 35
  • 52
23
votes
5 answers

An object reference is required to access a non-static member

I'm having this error come up and I'm not sure why... I've tried to look it up, people are saying to create an object of the class or create the methods as static... but I'm unsure how. Here's my code below: public class SoundManager : MonoBehaviour…
James Eaton
  • 287
  • 1
  • 2
  • 8
23
votes
1 answer

C++ Access private static member from public static method?

Let's say I have a .hpp file containing a simple class with a public static method and a private static member/variable. This is an example class: class MyClass { public: static int DoSomethingWithTheVar() { TheVar = 10; …
SLC
  • 2,167
  • 2
  • 28
  • 46
22
votes
3 answers

How to call the __invoke method of a member variable inside a class

PHP 5.4.5, here. I'm trying to invoke an object which is stored as a member of some other object. Like this (very roughly) class A { function __invoke () { ... } } class B { private a = new A(); ... $this->a(); <-- runtime error…
Jules May
  • 753
  • 2
  • 10
  • 18
22
votes
7 answers

Can I access new methods in anonymous inner class with some syntax?

Is there any Java syntax to access new methods defined within anonymous inner classes from outer class? I know there can be various workarounds, but I wonder if a special syntax exist? For example class Outer { ActionListener listener = new…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
19
votes
4 answers

Is there an easy way to tell if a class/struct has no data members?

Hallo, is there some easy way in C++ to tell (in compile-time) if a class/struct has no data members? E.g. struct T{}; My first thought was to compare sizeof(T)==0, but this always seems to be at least 1. The obvious answer would be to just look at…
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101