member-functions

callback from c++ to objective c

不打扰是莪最后的温柔 提交于 2019-12-03 07:24:42
问题 I have ViewController in objective-c and most of my code is c++ (.mm). I'd like to setup some callbacks to member functions from obj-c (in c++) and call them from c++. Something like this (it's very simplifyed): @interface MyClass { } -(void)my_callback; @end @implementation MyClass -(void)my_callback { printf("called!\n"); } -(void)viewDidLoad { // setup_callback( "to my_callback ?" ); } @end and: void setup_callback(void(*func)()) { func(); } this is not correct of course. Any advice how

Can C++ struct have member functions?

我怕爱的太早我们不能终老 提交于 2019-12-03 02:36:50
问题 I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default. However, my lecturers have just told me that structs cannot contain member functions. But I have seen many threads on the internet where people include member functions in structs and specifically say that it is alright to

Member function call in decltype

纵饮孤独 提交于 2019-12-03 01:44:52
The following code: struct A { int f(int); auto g(int x) -> decltype(f(x)); }; Fails to compile with the error: error: cannot call member function 'int B::f(int)' without object If I change it to: struct A { int f(int); auto g(int x) -> decltype(this->f(x)); }; I get another error: error: invalid use of 'this' at top level What is wrong with either of these? I am using gcc 4.6 Currently you can only access 'this' and members of the class inside the function body, but this is likely to be changed soon: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207 Here are the magic words:

callback from c++ to objective c

旧街凉风 提交于 2019-12-02 20:53:28
I have ViewController in objective-c and most of my code is c++ (.mm). I'd like to setup some callbacks to member functions from obj-c (in c++) and call them from c++. Something like this (it's very simplifyed): @interface MyClass { } -(void)my_callback; @end @implementation MyClass -(void)my_callback { printf("called!\n"); } -(void)viewDidLoad { // setup_callback( "to my_callback ?" ); } @end and: void setup_callback(void(*func)()) { func(); } this is not correct of course. Any advice how can I do it, please? You have a few options. Using blocks You may use blocks to convey your callback work

Can C++ struct have member functions?

徘徊边缘 提交于 2019-12-02 16:34:42
I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default. However, my lecturers have just told me that structs cannot contain member functions. But I have seen many threads on the internet where people include member functions in structs and specifically say that it is alright to do so. My lecturers seem adamant that structs by definition cannot have functions, so what is going on

C++ How do you pass a member function into a functor parameter?

前提是你 提交于 2019-12-02 13:53:48
问题 I am using TRI DDS - here is the prototype for the function I am trying to call: template<typename T , typename Functor > dds::sub::cond::ReadCondition::ReadCondition ( const dds::sub::DataReader< T > & reader, const dds::sub::status::DataState & status, const Functor & handler ) So I have a class that looks a bit like this (with load of irrelevant stuff omitted): MyClass test{ public: test(){... mp_reader = ...}; // not complete start_reader() { dds::sub::cond::ReadCondition rc(*mp_reader,

How to apply sizeof() operator to non-static class member methods?

大城市里の小女人 提交于 2019-12-02 06:48:36
问题 struct MyClass { int foo () { return 0; } }; unsigned int size = sizeof(MyClass::foo); // obviously error Can we apply sizeof() to member methods from outside the class ? Do we need to declare object to get it ? Edit : I know that above code will give error (that's why word 'obviously'). Wanted to know if we can at all apply the sizeof() to a member method. I don't want to describe the use case for that in length. 回答1: You cannot obtain the size of a member-function , but you can obtain the

How to apply sizeof() operator to non-static class member methods?

做~自己de王妃 提交于 2019-12-02 05:33:31
struct MyClass { int foo () { return 0; } }; unsigned int size = sizeof(MyClass::foo); // obviously error Can we apply sizeof() to member methods from outside the class ? Do we need to declare object to get it ? Edit : I know that above code will give error (that's why word 'obviously'). Wanted to know if we can at all apply the sizeof() to a member method. I don't want to describe the use case for that in length. You cannot obtain the size of a member-function , but you can obtain the sizeof a pointer-to-member-function : int size = sizeof( &MyClass::foo ); The same goes for non-member

Undefined reference when inline specifier used with class member

▼魔方 西西 提交于 2019-12-02 04:01:44
I have some member functions in a class. When I use the inline specifier, the compiler complains of undefined reference. I have tried: Using 'inline' to precede the function definition in the class header file only. Using 'inline' to precede the function declaration in the class .cpp (where the member functions are specified) file only. Doing both of the above at the same time. Obviously, one of these ways is the correct thing to do, and the others are not correct. However trying each option did not get me a program which compiled. Here is what I am trying to do: .hpp file: class A{ void func(

C++ How do you pass a member function into a functor parameter?

可紊 提交于 2019-12-02 03:54:48
I am using TRI DDS - here is the prototype for the function I am trying to call: template<typename T , typename Functor > dds::sub::cond::ReadCondition::ReadCondition ( const dds::sub::DataReader< T > & reader, const dds::sub::status::DataState & status, const Functor & handler ) So I have a class that looks a bit like this (with load of irrelevant stuff omitted): MyClass test{ public: test(){... mp_reader = ...}; // not complete start_reader() { dds::sub::cond::ReadCondition rc(*mp_reader, dds::sub::status::DataState::any(), do_stuff()); // This does not work } void do_stuff() {...} private: