member

Declaring a member function in JS

吃可爱长大的小学妹 提交于 2019-11-28 11:32:48
I've tried two ways to declare a member function in JS: function init() { var name = "Mozilla"; function displayName() { alert(name); } } a = new init(); a.displayName() And function init() { var name = "Mozilla"; displayName = function() { alert(name); } } a = new init(); a.displayName() The first method told me that displayName() is undefined . The way I see it a variable of type Function with nae displayName is created, and thus it should work. Any one care to explain why it didn't work? Thanks To create something like a member function you need to add it to the protoype of the constructor

C++same class as member in class

淺唱寂寞╮ 提交于 2019-11-28 10:00:10
问题 I have a class that should have a class of the same type as its member. My declaration is the following: class clsNode { private: clsNode m_Mother; public: void setMother(const clsNode &uNode, int index); }; C++ tells me "The object shows a type qualifier that is not compatible with the member function. I don't know where I went wrong. 回答1: The reason is that the type of the member m_Mother has incomplete type at the point it is declared. If you think about it. If it would have worked, you

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

半世苍凉 提交于 2019-11-28 09:09:55
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 class is not POD, and can have virtual methods, primitive and object data member. Based on Matthieu M.'s answer but shorter and with no macros: template<typename T, typename U> constexpr size_t offsetOf(U T::*member) { return (char*)&((T*)nullptr->*member) - (char*)nullptr; } And it's called like this: struct X { int a, b, c, d; } std::cout << "offset of c in X == " <<

Constant Member Functions

被刻印的时光 ゝ 提交于 2019-11-28 07:23:47
问题 After reading this, it is my understanding that declaring a method as const prevents it from accidentally modifying the class's member variables. Are const methods commonly used? Should they be used for everything that shouldn't modify member variables? 回答1: Yes, const should always be used, when appropriate. It lets your compiler check your application logic, statically asserting const-correctness for free! Some people even say that const should be the default, and you should be forced to

Get a pointer to object's member function

坚强是说给别人听的谎言 提交于 2019-11-28 05:49:33
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_function of an object? Thanks. typedef int (Some_class::*some_fcn_ptr)(double*, double*, int, int, void

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

蹲街弑〆低调 提交于 2019-11-28 04:31:57
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? The initialization of non-static members could not be done like this prior to C++11. If you compile with a

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

醉酒当歌 提交于 2019-11-28 03:23:34
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 If I create a class MyClass and it has some private member say MyOtherClass, is it better to make MyOtherClass a pointer or not? you should generally declare it as a value in your class. it will be local, there

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

对着背影说爱祢 提交于 2019-11-28 02:28:39
问题 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 { public List<AudioSource> audioSounds = new List<AudioSource>(); public double minTime = 0.5; public static void playSound(AudioClip sourceSound, Vector3 objectPosition, int volume, float audioPitch, int dopplerLevel) { bool playsound = false;

how to pass a non static-member function as a callback?

倖福魔咒の 提交于 2019-11-28 01:56:55
io_iterator_t enumerator; kern_return_t result; result = IOServiceAddMatchingNotification( mNotifyPort, kIOMatchedNotification, IOServiceMatching( "IOFireWireLocalNode" ), serviceMatchingCallback, (void *)0x1234, & enumerator ); serviceMatchingCallback((void *)0x1234, enumerator); if i declare serviceMatchinCallback as static then it works, but i do not want it to be static. Is there a way to pass it a non-static callback function? Thank you You could keep it static, but use the userdata to store the this pointer in addition to whatever other userdata you want (by packing them into a structure

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

柔情痞子 提交于 2019-11-28 01:50:41
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: #pragma once #include "CubeGeometry.h" using namespace std; class ProxyPiece { public: ProxyPiece