member

Accessing a protected member variable outside a class

Deadly 提交于 2019-12-04 23:41:47
I'm querying for the ID of a field by accessing a class function which someone has already put in place. The result is a object returned with protected member variables. I'm struggling to see how I can access the member variable values outside the class. Just add a "get" method to the class. class Foo { protected $bar = 'Hello World!'; public function getBar() { return $this->bar; } } $baz = new Foo(); echo $baz->getBar(); Pawka Accessing protected or private variables from public is incorrect (thats why they are protected or private). So better is to extend class and access required property

Java: Accessing private fields directly from another instance of the same class

丶灬走出姿态 提交于 2019-12-04 17:43:03
问题 I'm writing a equals(Object obj) function for a class. I see that it is possible to access the private fields of obj from the caller. So instead of using a getter: Odp other = (Odp) obj; if (! other.getCollection().contains(ftw)) { } I can just access the field directly: Odp other = (Odp) obj; if (! other.collection.contains(ftw)) { } Is this bad practice? 回答1: No, it's not. The reason that private variables and methods are not accessable from other classes is to allow you to change the

Why does a non-static field not act as a GC root?

谁说我不能喝 提交于 2019-12-04 12:36:44
问题 As I know static fields (along with Threads, local variables and method arguments, JNI references) act as GC roots. I cannot provide a link that would confirm this, but I have read a lot of articles on it. Why can't a non-static field act as a GC root? 回答1: First off, we need to be sure we're on the same page as to what a tracing garbage collection algorithm does in its mark phase. At any given moment, a tracing GC has a number of objects that are known to be alive, in the sense that they are

Accessing member variables through boost lambda placeholder

…衆ロ難τιáo~ 提交于 2019-12-04 12:35:08
I'm trying to print the second member variable of all items in an stl map using a lambda expression map<int, int> theMap; for_each(theMap.begin(), theMap.end(), cout << bind(&pair<int, int>::second, _1) << constant(" ")); but this is not compiling. I essentially want to de-reference the placeholder. Any idea what I'm missing here? Thanks in advance! std::map will add const to its key; this is to prevent messing up the ordering. Your pair should be: std::pair<const int, int> Like dirkgently suggests, use the value_type to always get the correct type. The verbosity is alleviated with a typedef:

C++ Member Variables

一曲冷凌霜 提交于 2019-12-04 10:26:07
问题 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 scope resolution operator. void A::setNumber(int number) { A::number = number; } Method 3 : Instead, denote all member variables with 'm' or '_' (this is my preferred method). void A::setNumber(int number) { mNumber = number; } Is this just personal

第24课经典的问题

拥有回忆 提交于 2019-12-04 09:38:36
经典的问题(上) 当程序中存在多个对象的时候,如何确定这些对象的析构顺序? 关于析构的疑问 单个对象 创建时 构造函数的调用顺序 1. 调用 父类的构造过程 (后续再说) 2. 调用 成员变量的构造函数 (调用顺序与声明顺序相同)---------有可能某个类,它里面的成员是其他类的对象。此时就调用成员变量的构造函数。 3. 调用 类自身 的构造函数 析构函数与对应构造函数的调用顺序相反 多个对象析构时 同样遵循 析构顺序与构造顺序相反 实例分析 构造与析构顺序 #include <stdio.h> class Member { const char* ms; public: Member(const char* s) { printf("Member(const char* s): %s\n", s); ms = s; } ~Member() { printf("~Member(): %s\n", ms); } }; class Test { Member mA; Member mB; public: Test() : mB("mB"), mA("mA") { printf("Test()\n"); } ~Test() { printf("~Test()\n"); } }; Member gA("gA"); int main() { Test t; return 0; }

Scope of Java class static member

大憨熊 提交于 2019-12-04 09:17:38
I am developing a web application using Java/JSP. One class, say Student, has a static member static private int _nextID; I want to know, if this _nextID is the same among all user sessions? Or each session has its own _nextID? andersoj In the simple case , you can assume that multiple instances of Student will share the same static nextID field. However, one should think beyond the simple case (or document that you haven't). In this case, it's fine unless the instance id fields derived from the nextID counter propagate out into a larger application where the IDs are meant to be unique. In

In C++14 can a constexpr member change a data member?

自闭症网瘾萝莉.ら 提交于 2019-12-04 09:06:57
In C++14, since constexpr are not implicitly const anymore, can a constexpr member function modify a data member of a class: struct myclass { int member; constexpr myclass(int input): member(input) {} constexpr void f() {member = 42;} // Is it allowed? }; Yes they are, I believe this change started with proposal N3598: constexpr member functions and implicit const and eventually became part of N3652: Relaxing constraints on constexpr functions which changed section 7.1.5 paragraph 3 what is allowed in the function body from a white-list: its function-body shall be = delete, = default, or a

c++ Function member pointer

不想你离开。 提交于 2019-12-04 07:02:12
问题 I have read several posts about this, but can't seem to find exactly what i am looking for with example code if anyone could give me some help i would highly appreciate it. in my header i have: void addEvent(void (*func)(Pack *)); void triggerEvents(Pack * ); std::list<void(*)(Pack *)> eventList; and in cpp file void DNetwork::addEvent(void (*func)(Pack *)){ eventList.push_back(func); } void DNetwork::triggerEvents(Pack * pack){ for (std::list<void (*)( Pack *)>::iterator it = eventList.begin

Can I transform an object and access the private data members in C++?

前提是你 提交于 2019-12-04 04:25:42
I want to access a private data member in a class. There is no member function in the class to access the private data member. It is private. I want to take the class and some how crack it open. One method was to copy the declaration of the class, make the private member public and call the new class class something_else. Then I do a reinterpret cast and copy the original object. This works. But I want something more elegant ... or perhaps generic ... or just another way. What options are there? Can I use void*? Can I memcpy the class into another empty class? What are ways to do this?? % I am