默认情况下是否可以传递"this"?

Is it possible to pass "this" by default?

本文关键字:this 情况下 是否 默认      更新时间:2023-10-16

默认情况下是否可以通过this

以下是我目前拥有的

class A
{
public:
    template<typename T>
    void dowithT(T t) {}
};
class B
{
public:
    A a;
    B()
    {
        //Calling 'dowithT' with 'this'
        a.dowithT(this);
    }
};

此函数要求每次从函数的调用方传递this。所以我想知道是否有一种方法可以封装这个任务,这样就不需要将this传递给dowithT

我试着做这样的事情:

class A
{
public:
    // '= this' doesn't compile
    template<typename T>
    void dowithT(T t = this) {}
};
class B
{
public:
    A a;
    B()
    {
        //Calling 'dowithT' without 'this'
        a.dowithT();
    }
};

不幸的是,我不能使用模板,所以我的第一个解决方案不是一个选项。

这可能吗?

编辑:下面我用自己的实现给出了一个具体的答案。还有一些我最终想要的东西。

TL;DR不,这是不可能的。

this在每个类中都不是相同的类型,你不能将其泛化,所以不,不可能。

此外,如果从非成员函数调用doWithT()this会是什么?nullptr

这就是为什么这是不可能的。你必须使用template

B不具有类型为A成员,而是可以A继承并使用类似于"奇怪的重复模板模式"的

如果你不能让A类成为模板,你仍然可以这样做:

class A
{
    protected:
    template <class T>
    void dowithT()
    {
         T* callerthis = static_cast<T*>(this);
         // callerthis is the "this" pointer for the inheriting object
         cout << "Foo";
    }
};
class B : public A
{
    public:
    B()
    {
        dowithT<B>();
        // Or A::dowithT<B>();
    }
};

dowithT()必须只能由继承类调用(因此我将其设为protected),模板参数是调用者自己的类型,否则将破坏所有内容。

您可以通过使用私有mixin类来提供不带参数的dowithT方法来实现您想要的目标:

#include <iostream>
#include <typeinfo>
class A
{
public:
    template<typename T>
    void dowithT(T* t) {
      std::cout << "Hello, World" << typeid(*t).name() << std::endl;
    }
};
template<class Owner>
  struct calls_a
  {
    void dowithT()
    {
      auto p = static_cast<Owner*>(this);
      p->a.dowithT(p);
    }
  };
class B
  : private calls_a<B>
{
    friend calls_a<B>;
    A a;
public:
    B()
    {
        //Calling 'dowithT' with 'this'
        dowithT();
    }
};
int main()
{
  B b;
}

不,这是不可能的。当this用作采用T*(模板与否)的函数的参数时,它并没有什么特别之处,它只是一个与其他指针一样的指针。

this Athis B不同。在第一个代码中,this指代调用者,而在第二个代码中this指代callee。因此,你想做的事情实际上是不可能的。

这里有一种可能性,可能适合也可能不适合您的需求:

template<typename T>
class A
{
public:
    A(T t) : t(t) {}
    void dowithT()
    {
         cout << "Foo";
    }
private:
    T t;
};
class B
{
    public:
    A<B*> a;
    B() : a(this)
    {
        a.dowithT();
    }
};

如果你想传递其他值,你可以在类B中使用一个充当中继的私有方法,并使用常量nullptr作为特殊值:

class B
{
public:
    A a;
    B()
    {
        //Calling 'dowithT' with 'this'
        innerdo();
    }
private:
    void innerdo(B *p = nullptr) {
        if (p == nullptr) p = this;
        a.dowithT(p);
    }
};

如果你只需要通过this,那就更简单了

    void innerdo() {
        a.dowithT(this);
    }

在尝试了你提到的各种事情之后,我想自己给出问题的答案/解决方案,以澄清一些细节:

#include <iostream>
using namespace std;
#include <functional>
template <typename CallerType>
class AFunctionConstructor{
private:
    virtual void abstr()
    {}
public:
    
    typedef void(CallerType::*CallerTypeFunc)();
    function<void()>* constructFunction(CallerTypeFunc func)
    {
        CallerType* newMe = dynamic_cast<CallerType*> (this);
        return new function<void()>(std::bind(func,newMe));
    }       
};
class A : public function<void()>
{
protected:
    
     
public:
    A();
    A(function<void()>* func) : function<void()>(*func)
    {}
};

//  now create ressource classes
//  they provide functions to be called via an object of class A
class B : public AFunctionConstructor<B>
{
    void foo()
    {
        cout << "Foo";
    }
public:
    A a;
    B() : a(constructFunction(&B::foo)) {}
};
class C : public AFunctionConstructor < C >
{
    void bar()
    {
        cout << "Bar";
    }
public:
    A a;
    C() : a(constructFunction(&C::bar)) {}
};
int main()
{
    B b;
    C c;
    b.a(); 
    c.a();
    cout << endl;
    A* array[5];
    array[0] = &b.a;        //different functions with their ressources
    array[1] = &c.a;        
    array[2] = &b.a;
    array[3] = &c.a;
    array[4] = &c.a;
    for (int i = 0; i < 5; i++)     //this usability i wanted to provide
    {
        (*(array[i]))();
    }
    getchar();
    return 0;
}

输出:

FooBar

FooBarFooBarBar

这是我所能写的关于例子的。但我想这是不安全的代码。我偶然发现了其他可能的更简单的方法来实现这一点(std::function和lambdas的其他用途(我可能试图在这里部分重新发明))。

起初,我曾试图通过";这个";到function<void()>*AFunctionConstructor::constructFunction(CallerTypeFunc func)中的绑定函数不过,我现在通过动态升级来了解。此外,AFunctionConstructor的功能最初应该在a.的构造函数中实现