member-pointers

C++ Best way to have class member which is a pointer (or reference) to another class and can handle both const and non-const situations

不想你离开。 提交于 2021-02-11 12:42:48
问题 Consider the following sample code. It compiles and works as expected. However, if I add "const" to the beginning of the first line of main function, it will not compile, because B class takes a pointer to A , and with const being added, we will have a pointer to const A instead. Templatizing B is an option, but I wonder if there are cleaner/nicer solutions for this. I am open to various suggestions, including moving the B inside A if that would help (B is used by A only). #include <vector>

Why can't one use scope resolution with member pointer dereference?

馋奶兔 提交于 2020-01-05 06:52:22
问题 Consider a simple example: struct FooParent { virtual void bar() { } }; struct Foo: FooParent { void bar() { } }; int main() { Foo foo; void (Foo::*foo_member)() = &FooParent::bar; //(foo.*FooParent::foo_member)(); foo.FooParent::bar(); } As you can see one can use scope resolution on the foo object when calling bar member function while there is no way to explicitly declare the scope for member function pointer. I accept that the syntax should be prohibited when using ->* as the operator can

Nested data member pointer - not possible?

北城余情 提交于 2019-12-29 07:31:33
问题 The following reduced code sample does not do anything useful but two subsequent assignments to a data member pointer. The first assignment 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() {

Nested data member pointer - not possible?

被刻印的时光 ゝ 提交于 2019-12-29 07:31:11
问题 The following reduced code sample does not do anything useful but two subsequent assignments to a data member pointer. The first assignment 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() {

Calling derived class's methods from pointer to base class via reinterpret_casting the method pointer. Is this UB?

不羁的心 提交于 2019-12-24 06:34:52
问题 With a pointer to an object of a derived type assigned to a pointer of its base class, I've found that you can reinterpet_cast a method from the derived class to a pointer of the base class, even if the base class doesn't have any such function (virtual, hidden, or otherwise). And it can be dereferenced and called from there and it "just works". But I'd like to make sure it's not UB. Is this UB? Is it portable? Compilable Example: #include <cstdio> struct A { /* no foo method */ }; struct B :

Compile error: unresolved overloaded function type

笑着哭i 提交于 2019-12-22 08:49:52
问题 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 ‘

Function member pointer with private base

北战南征 提交于 2019-12-22 07:04:10
问题 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

How to cast member variable pointer to generic type in C++

梦想的初衷 提交于 2019-12-14 02:13:25
问题 I have code similar to this in my application: class A { public: int b; } class C { public: int d; } void DoThings (void *arg1, MYSTERYTYPE arg2); A obj_a; C obj_c; DoThings(&obj_a, &A::b); DoThings(&obj_c, &C::d); The question is - What should MYSTERYTYPE be? neither void* nor int work, despite the value &A::b being printed just fine if you output it through a printf. Clarifications: Yes, &A::b is defined under C++. Yes, I am trying to get the offset to a class member. Yes, I am being tricky

Comparison of Virtual Function Pointers in C++

邮差的信 提交于 2019-12-13 13:26:45
问题 Say I want to check to see whether a subclass has implemented one of it's parent's virtual functions (never mind whether this smells of bad architecture... it's an exercise). If I wanted to see if two regular functions were identical, I could just check &f == &g . // Plain old functions void f() {} void g() {} ... std::cout << "&f " << &f << "\n"; // "1" OK, for some reason func ptrs are converted std::cout << "&g " << &f << "\n"; // "1" to booleans when printed. I can dig it. std::cout << "

c++ pointer to member function, replacement for __closure

混江龙づ霸主 提交于 2019-12-11 10:16:27
问题 Some time ago, Borland have introduced in their BCB evironment an extension to C++ language. This extension is a __closure keyword. The question is, if it is possible to implement such functionality in plain C++ or C++11? If you are not familiar with __closure keyword, below code provides explanation in comments. Thanks in advance! Toreno #include <stdio.h> // __closure keyword is used here ! typedef void (__closure * MemberCallback)(int x, int y, int z); class A { private: MemberCallback