member-pointers

Type of pointer to member from base class

狂风中的少年 提交于 2019-12-06 05:54:07
问题 I have a problem regarding member pointers. The following code fails to compile using both Oracle Solaris Studio 12.2's CC and cygwin GCC 4.3.4 but works with Microsoft Visual C++ 2010: struct A { int x; }; struct B : public A { }; template<typename T> class Bar { public: template<typename M> void foo(M T::*p); }; int main(int, char *[]) { Bar<B> bbar; bbar.foo(&B::x); return 0; } At the next to last line both compilers mentioned above fail to find a match for Bar<B>::foo(int A::*) . I wrote

Compile error: unresolved overloaded function type

天涯浪子 提交于 2019-12-05 18:03:55
I try to compile the following with g++ 4.7.2: template <typename T> struct A { struct B { T t; template<T B::*M> T get() { return this->*M; } }; B b; T get() { return b.get<&B::t>(); } }; int main() { A<int> a; a.get(); } It gives me test.cpp: In member function ‘T A<T>::get()’: test.cpp:15:23: error: expected primary-expression before ‘)’ token test.cpp: In instantiation of ‘T A<T>::get() [with T = int]’: test.cpp:22:8: required from here test.cpp:15:23: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int A<int>::B::*’ to binary ‘operator<’ Why? Thanks. You need

Member function pointer

丶灬走出姿态 提交于 2019-12-05 11:29:07
问题 If the following from the C++ FAQ Lite is true: "a function name decays to a pointer to the function" (as an array name decays to a pointer to its first element); why do we have to include the ampersand? typedef int (Fred::*FredMemFn)(char x, float y); FredMemFn p = &Fred::f; And not just: typedef int (Fred::*FredMemFn)(char x, float y); FredMemFn p = Fred::f; In the second case Fred::f is a function and can decay to a pointer to that function. I hope this question is not that stupid. 回答1:

Function member pointer with private base

别来无恙 提交于 2019-12-05 08:25:44
The following code yields a compile time error: ' base::print ' : cannot access private member declared in class ' base_der ' However, I have made the member public in the derived class. Why doesn't this work? #include <iostream> using namespace std; class base { public: int i; void print(int i) { printf("base i\n"); } }; class base_der : private base { public: using base::print; }; int main() { // This works: base_der cls; cls.print(10); // This doesn't: void (base_der::* print)(int); print = &base_der::print; // Compile error here } I think there are a few interacting problems contributing

Type of pointer to member from base class

杀马特。学长 韩版系。学妹 提交于 2019-12-04 11:08:44
I have a problem regarding member pointers. The following code fails to compile using both Oracle Solaris Studio 12.2's CC and cygwin GCC 4.3.4 but works with Microsoft Visual C++ 2010: struct A { int x; }; struct B : public A { }; template<typename T> class Bar { public: template<typename M> void foo(M T::*p); }; int main(int, char *[]) { Bar<B> bbar; bbar.foo(&B::x); return 0; } At the next to last line both compilers mentioned above fail to find a match for Bar<B>::foo(int A::*) . I wrote a simple test to confirm that the type of the expression &B::x is actually int A::* : // ... static

Nested data member pointer - not possible?

放肆的年华 提交于 2019-12-01 03:27:04
The foolowing reduced code sample does not do anything usefull but two subsequent assignments to a data member pointer. The first assignement works, the second one gives a compiler error. Presumably because its to a nested member. Question would be: Is it really just not possible to let a member pointer point to a nested member or am I missing any fancy syntax there? struct Color { float Red; float Green; float Blue; }; struct Material { float Brightness; Color DiffuseColor; }; int main() { float Material::* ParamToAnimate; ParamToAnimate = &Material::Brightness; // Ok ParamToAnimate =

Offset of pointer to member

[亡魂溺海] 提交于 2019-11-29 14:16:20
template<class T, typename U> ptrdiff_t foo(T U::* m) { // return offset } How I can get the offset of the field 'm' in this context? I would prefer to use am compile-time expression. Thanks in advance for any help. Best regards Sounds like you're looking for the offsetof() macro. @Michael J Thanks for your answer. This wasn't exactly what I was looking for, but it gives me the inspiration to doing that: template<class T, typename U> std::ptrdiff_t member_offset(U T::* member) { return reinterpret_cast<std::ptrdiff_t>( &(reinterpret_cast<T const volatile*>(NULL)->*member) ); } The simple

Offset of pointer to member

。_饼干妹妹 提交于 2019-11-28 08:18:11
问题 template<class T, typename U> ptrdiff_t foo(T U::* m) { // return offset } How I can get the offset of the field 'm' in this context? I would prefer to use am compile-time expression. Thanks in advance for any help. Best regards 回答1: Sounds like you're looking for the offsetof() macro. 回答2: @Michael J Thanks for your answer. This wasn't exactly what I was looking for, but it gives me the inspiration to doing that: template<class T, typename U> std::ptrdiff_t member_offset(U T::* member) {

Access to protected member through member-pointer: is it a hack?

試著忘記壹切 提交于 2019-11-27 17:41:23
We all know members specified protected from a base class can only be accessed from a derived class own instance. This is a feature from the Standard, and this has been discussed on Stack Overflow multiple times: Cannot access protected member of another instance from derived type's scope ; Why can't my object access protected members of another object defined in common base class? And others. But it seems possible to walk around this restriction with member pointers, as user chtz has shown me : struct Base { protected: int value; }; struct Derived : Base { void f(Base const& other) { //int n

Access to protected member through member-pointer: is it a hack?

时光毁灭记忆、已成空白 提交于 2019-11-26 22:35:34
问题 We all know members specified protected from a base class can only be accessed from a derived class own instance. This is a feature from the Standard, and this has been discussed on Stack Overflow multiple times: Cannot access protected member of another instance from derived type's scope ; Why can't my object access protected members of another object defined in common base class? And others. But it seems possible to walk around this restriction with member pointers, as user chtz has shown