member

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

不羁的心 提交于 2019-11-28 01:48:47
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 the code, but I would like to switch on this. Konrad Rudolph Since C++11, you can use standard std::is_empty trait: https://en.cppreference.com/w/cpp/types/is_empty If you are on paleo-compiler diet, there is a trick: you can derive from this class in another empty and check whether sizeof(OtherClass) == 1 . Boost does this in its is_empty type

I can't reach any class member from a nested class in Kotlin

心不动则不痛 提交于 2019-11-27 22:44:02
I want to access a member of the MainFragment class from PersonAdapter class but none of them are available. I tried making both the classes and the members public and private also but so far nothing worked. I guess I'm missing something obvious but I just can't figure it out. class MainFragment : Fragment() { lateinit var personAdapter: PersonAdapter lateinit var personListener: OnPersonSelected private var realm: Realm by Delegates.notNull() lateinit var realmListener: RealmChangeListener<Realm> override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState:

what is a member vs. a property

回眸只為那壹抹淺笑 提交于 2019-11-27 21:07:23
问题 A friend who is new to OO programming asked me the difference between a Member and Property, and I was ashamed to admit that I couldn't give him a good answer. Since properties can also be objects themselves, I was left with a general description and list of exceptions. Can somebody please lay out a good definition of when to consider something a member vs. a property? Maybe I'm bastardizing the concept, or is it just that a member is just the internal name I use, and the property is what's

Singleton Class in C++

痞子三分冷 提交于 2019-11-27 18:58:03
问题 I have used singleton calss following the example: singleton class But i get the error as "Unresolved external symbols" this is the code i tried out: #include<iostream> using namespace std; class singleton { int value; static singleton *instance; protected: singleton() { value=0; } public: static void initialize() { if(instance==NULL) singleton(); else cout<<"An instance of singleton already exist..."; } static singleton& getInstance() { return *instance; } int getValue() { return value; } };

default visibility of C++ class/struct members

半腔热情 提交于 2019-11-27 17:57:50
In C++, why is private the default visibility for members of classes, but public for structs? C++ was introduced as a superset of C. Structs were carried over from C, where the semantics of their members was that of public. A whole lot of C code exists, including libraries that were desired to work with C++ as well, that use structs. Classes were introduced in C++, and to conform with the OO philosophy of encapsulation, their members are private by default. a_m0d Because a class is a usual way of doing object orientation, which means that member variables should be private and have public

Member variables in ES6 classes

懵懂的女人 提交于 2019-11-27 17:09:22
问题 Is there any way to use the ECMAScript6 class notation to declare either a static class variable or a default value for an instance variable? Without class what I have in mind would be written as function MyClass(arg) { if(arg) this.arg = arg; } MyClass.classVariable = 42; MyClass.prototype.arg = "no arg specified"; The most obvious ES6-like notation in my opinion would have been class MyClass { constructor(arg) { if(arg) this.arg = arg; } static let classVariable = 42; let arg = "no arg

Get default value of class member

杀马特。学长 韩版系。学妹 提交于 2019-11-27 16:51:26
问题 Let's assume I have a class ClassWithMember class ClassWithMember { int myIntMember = 10; } How do I get the default value 10 of the myIntMember member by System.Type? I'm currently struggling around with reflections by all I retreive is the default value of int (0) not the classes default member (10).. 回答1: You can try something like this: var field = typeof(ClassWithMember).GetField("myIntMember", BindingFlags.Instance | BindingFlags.NonPublic); var value = (int)field.GetValue(new

Why I can access member functions even after the object was deleted?

廉价感情. 提交于 2019-11-27 15:58:11
I'm new to C++ and from what I learned so far when you call delete on a pointer that points to something created on the heap then whatever is pointed by that pointer gets erased and the memory is freed, right? However when I tried this on a simple class: class MyClass { int _Id; public: MyClass(int id) : _Id(id) { std::cout << "$Constructing the damn thing! " << _Id << std::endl; } ~MyClass() { std::cout << "?Destructing the damn thing! " << _Id << std::endl; } void Go_XXX_Your_Self() { std::cout << "%OooooooooO NOOOOOO! " << _Id << std::endl; delete this; } void Identify_Your_Self() { std:

How can I declare a member vector of the same class?

可紊 提交于 2019-11-27 14:33:53
Why on earth does the following piece of code work? struct A { std::vector<A> subAs; }; A is an incomplete type, right? If there was a vector of A*s I would understand. But here I don't understand how it works. It seems to be a recursive definition. WhiZTiM This paper was adopted into C++17 which allows incomplete types to be used in certain STL containers. Prior to that, it was Undefined Behavior. To quote from the paper: Based on the discussion on the Issaquah meeting, we achieved the consensus to proceed* with the approach – “Containers of Incomplete Types”, but limit the scope to std:

C++ class member function callback

情到浓时终转凉″ 提交于 2019-11-27 14:33:29
I have the following problem. I have a function from an external library (which cannot be modified) like this: void externalFunction(int n, void udf(double*) ); I would like to pass as the udf function above a function member of an existing class. Please look at the following code: // External function (tipically from an external library) void externalFunction(int n, void udf(double*) ) { // do something } // User Defined Function (UDF) void myUDF(double* a) { // do something } // Class containing the User Defined Function (UDF) class myClass { public: void classUDF(double* a) { // do