标签: member-pointers

编译错误:未解决的重载函数类型

我尝试使用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();
}
Run Code Online (Sandbox Code Playgroud)

它给了我

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::*’ …
Run Code Online (Sandbox Code Playgroud)

c++ templates compilation member-pointers

4
推荐指数
1
解决办法
2167
查看次数

通过指向类成员的指针调用函数

在以下代码中:

class foo
{
public:
    void foo_function() {};
};

class bar
{
public:
    foo foo_member;

    void bar_function(foo bar::*p_foo)
    {
        // what is the corrct sintax for following:
        this->*p_foo->foo_function(); // expression must have a pointer type??
    }
};


int main()
{
    foo foo_obj;
    bar bar_obj;
    typedef foo bar::*p_foo;
    p_foo blah = &bar::foo_member;
    bar_obj.bar_function(blah);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使bar :: bar_function工作的正确语法是什么?

c++ member-pointers

3
推荐指数
1
解决办法
64
查看次数

如何在C++中将成员变量指针转换为泛型类型

我的应用程序中的代码与此类似:

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);
Run Code Online (Sandbox Code Playgroud)

问题是 - MYSTERYTYPE应该是什么?尽管如果你通过printf输出它并且A :: b被打印得很好,那么void*和int都不会工作.

澄清:是的,&A :: b是在C++下定义的.是的,我正试图获得班级成员的抵消.是的,我很棘手.

编辑:哦,我可以使用offsetof().不管怎么说,还是要谢谢你.

c++ pointers class member-pointers

2
推荐指数
1
解决办法
4683
查看次数

在c ++中有&func或class :: func的用途吗?

这似乎不一致.为什么我们使用&Example :: func而不是Example :: func?是否有用于Example :: func或&exampleFunction?看起来我们不能对函数进行引用,因此排除了Example :: func.我想不出一种使用&exampleFunction的方法,因为exampleFunction已经返回一个指针.

#include <iostream>
class Example {
public:
    void func() { std::cout <<"print me\n"; }
};
void exampleFunction() { std::cout << "print me too\n"; }
typedef void (Example::*ExampleFunc_t)(); 
typedef void (*ExampleFunction_t)();
int main()
{
    Example e;
    ExampleFunc_t     f  = &Example::func;
    ExampleFunction_t f2 = exampleFunction;
    (e.*f)();
    f2();
    return 0;
} 
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers member-pointers

2
推荐指数
1
解决办法
349
查看次数

为什么不能将范围解析与成员指针取消引用一起使用?

考虑一个简单的例子:

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();
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,foo在调用 bar 成员函数时可以在对象上使用范围解析,但无法显式声明成员函数指针的范围。我同意在使用时应禁止该语法,->*因为运算符有时可能会以意想不到的方式重载,但我无法理解在取消引用时阻止显式范围解析背后的原因.*

我正在尝试禁用指向基类的虚函数的成员指针的虚拟分派。

c++ virtual-functions member-pointers dereference language-lawyer

2
推荐指数
1
解决办法
368
查看次数

奇怪的语法:范围运算符后的星号(::)?

帮助我理解以下代码片段:

(foo.h中)

class Foo
{
   public:
        typedef void (MyType::*Handler)(SomeOtherType* t);

        Foo(Handler handler) : handler_(handler) { }

   private:
        Handler handler_;
};
Run Code Online (Sandbox Code Playgroud)

(mytype.h)

class MyType
{
     public:
          MyType() { }
          void fun1() { }
          void fun2() { }    
};
Run Code Online (Sandbox Code Playgroud)

foo.h中的typedef究竟是什么声明?我可以看到它是某种函数指针,但星号的意义是什么?它似乎是取消引用一个类型(??)并以某种方式试图将新typedef指针"附加"到MyType类型(?!?).

有人可以在这里说清楚吗?真的很困惑:S

c++ function-pointers member-pointers

1
推荐指数
1
解决办法
684
查看次数

成员变量指针

对于给定的结构:

struct foo
{
    void fooFunc(){}
    int fooVar = 0;
};
Run Code Online (Sandbox Code Playgroud)

我可以为函数创建一个调用wapper:std::mem_fn( &foo::fooFunc )这样我就可以将它传递给另一个方法并在一个对象上调用它.
我想知道是否有类似的调用包装器,但是对于成员变量.

例如,我在这里使用指向成员变量的指针,但我想使用一个调用包装器:

void bar( std::function< void( foo ) > funcPtr, int foo::* varPtr )
{
    foo myFoo;
    funcPtr( myFoo );
    foo.*varPtr = 13;
}
Run Code Online (Sandbox Code Playgroud)

c++ wrapper mem-fun member-pointers c++11

1
推荐指数
1
解决办法
660
查看次数

成员指针的类型未被识别为&lt;值类型&gt;

下面的代码示例尝试提取foo先前输入到函数中的结构的成员指针的值类型serialize()

这可能需要进一步解释:我想最终迭代结构体的每个成员foo,并根据每个成员指针的值类型应用某些操作。看一看:

演示

#include <cstdio>
#include <utility>
#include <tuple>
#include <typeinfo>

/* Struct with two int members */

struct foo
{
    int a_;
    int b_;
};

/* Saves member pointers */

template<auto Ptr>
struct proxy
{
    decltype(Ptr) mem_ptr_;
};

/* Conglomerate of all member ptrs of a struct */

template <typename T>
struct type_descriptor;

template <>
struct type_descriptor<foo>
{
    using type = std::tuple<proxy<&foo::a_>, proxy<&foo::b_>>;
};

/* Extract member ptr value type */

template<typename T> …
Run Code Online (Sandbox Code Playgroud)

c++ templates typeid member-pointers template-meta-programming

1
推荐指数
1
解决办法
62
查看次数