member

default visibility of C++ class/struct members

試著忘記壹切 提交于 2019-11-27 04:14:10
问题 In C++, why is private the default visibility for members of classes, but public for structs? 回答1: 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. 回答2: Because a class is a

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

落花浮王杯 提交于 2019-11-27 03:05:21
问题 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 use $MyMember = 0; and echo $MyMember or private $MyMember = 0; and echo $MyMember or $this->MyMember = 0; and echo $this->MyMember Can someone tell me how to do it? I am kind of not strong in OOPS. Can you do it in the first place? If not, how should I declare the member

What should happen to template class static member variables with definition in the .h file

爷,独闯天下 提交于 2019-11-27 01:56:47
问题 If a template class definition contains a static member variable that depends on the template type, I'm unsure of what the reliable behavior should be? In my case it is desirable to place the definition of that static member in the same .h file as the class definition, since I want the class to be general for many template data types that I don't currently know. I want only one instance of the static member to be shared throughout my program for each given template type. ( one for all MyClass

Get a pointer to object's member function

余生长醉 提交于 2019-11-27 00:43:37
问题 Here is the problem: 1) I have a class like so: class some_class { public: some_type some_value; int some_function(double *a, double *b, int c, int d, void *e); }; 2) Inside some_function , I use some_values from some_class object to get a result. 3) So, I have a concrete object and I want to get a pointer to this object some_function . Is it possible? I can't use some_fcn_ptr because the result of this function depends on the concrete some_value of an object. How can I get a pointer to some

Why can't we initialize class members at their declaration?

无人久伴 提交于 2019-11-27 00:28:45
问题 I wonder if there is a reason why we can't initialize members at their declaration. class Foo { int Bar = 42; // this is invalid }; As an equivalent of using constructor initialization lists. class Foo { int Bar; public: Foo() : Bar(42) {} } My personal understanding is that the above example is much more expressive and intentional. Moreover this is a shorter syntax. And I don't see any possibility of confusion with other language elements. Is there any official clarification about this? 回答1:

Class members that are objects - Pointers or not? C++

不羁岁月 提交于 2019-11-27 00:02:02
问题 If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not? What does it mean also to have it as not a pointer in terms of where it is stored in memory? Will the object be created when the class is created? I noticed that the examples in QT usually declare class members as pointers when they are classes. Regards Mark 回答1: If I create a class MyClass and it has some private member say MyOtherClass, is it better to make

How do I access static member of a class?

天大地大妈咪最大 提交于 2019-11-26 23:06:04
问题 I am trying to access static member of a class. my class is: class A { public static $strName = 'A is my name' public function xyz() { .. } .. } //Since I have bunch of classes stored in an array $x = array('A'); echo $x::$strName; I am getting error while printing. How can I print 'A is my name' 回答1: If A is a class, you can access it directly via A::$strName . class A { public static $strName = 'A is my name'; } echo A::$strName; // outputs "A is my name" Update: Depending on what you have

How to check if a member name (variable or function) exists in a class, with or without specifying type?

南笙酒味 提交于 2019-11-26 22:06:56
问题 This Q is an extension of: Is it possible to write a C++ template to check for a function's existence? Is there any utility which will help to find: If a member name exists inside a class or not? This member can be a variable or a method. Specifying the type of the member should be optional 回答1: With std::experimental::is_detected and std::experimental::disjunction you could do this: //check for a type member named foo template <typename T> using foo_type_t = typename T::foo; //check for a

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

孤街浪徒 提交于 2019-11-26 22:03:07
问题 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. 回答1: 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

“No appropriate default constructor available”--Why is the default constructor even called?

可紊 提交于 2019-11-26 22:00:58
问题 I've looked at a few other questions about this, but I don't see why a default constructor should even be called in my case. I could just provide a default constructor, but I want to understand why it is doing this and what it affects. error C2512: 'CubeGeometry' : no appropriate default constructor available I have a class called ProxyPiece with a member variable of CubeGeometry.The constructor is supposed to take in a CubeGeometry and assign it to the member variable. Here is the header: