C++ 有没有可能通过;这";默认情况下?

C++ 有没有可能通过;这";默认情况下?,c++,parameter-passing,this,C++,Parameter Passing,This,默认情况下是否可以传递此 这是我目前拥有的 class A { public: template<typename T> void dowithT(T t) {} }; class B { public: A a; B() { //Calling 'dowithT' with 'this' a.dowithT(this); } }; 不幸的是,我不能使用模板,所以我的第一个解决方案不是一个选项 这可

默认情况下是否可以传递

这是我目前拥有的

class A
{
public:
    template<typename T>
    void dowithT(T t) {}
};

class B
{
public:
    A a;

    B()
    {
        //Calling 'dowithT' with 'this'
        a.dowithT(this);
    }
};
不幸的是,我不能使用模板,所以我的第一个解决方案不是一个选项

这可能吗

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

TL;不,这是不可能的

在每个类中都不是相同的类型,您无法对其进行泛化,因此不,不可能

另外,如果从非成员函数调用
doWithT()
,那么
这将是什么<代码>空PTR


这就是为什么这是不可能的。您必须使用
模板

不,这是不可能的。
这个
当用作接受
T*
的函数的参数(无论是否为模板)时,它都没有什么特别之处,它只是一个指针,就像其他任何指针一样。

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

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

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();
    }
};
模板
甲级
{
公众:
A(T):T(T){}
void dowithT()
{

cout它可以继承
a
类型的成员,而不是
B
,并使用

如果您不能将类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>();
    }
};
A类
{
受保护的:
模板
void dowithT()
{
T*callerthis=静态_转换(this);
//callerthis是继承对象的“this”指针

cout通过使用私有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;

}
#包括
#包括
甲级
{
公众:
模板
无效道伊特(T*T){

std::cout如果希望能够传递其他值,则可以在类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);
    }
};
如果你只需要通过
这个
就更简单了

    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;
}
#包括
使用名称空间std;
#包括
模板
类函数构造函数{
私人:
虚拟空间abstr()
{}
公众:
typedef void(CallerType::*CallerTypeFunc)();
函数*constructFunction(CallerTypeFunc func)
{
CallerType*newMe=动态\u转换(此);
返回新函数(std::bind(func,newMe));
}       
};
A类:公共职能
{
受保护的:
公众:
A();
A(函数*func):函数(*func)
{}
};
//现在创建ressource类
//它们提供通过类A的对象调用的函数
B类:公共函数构造函数
{
void foo()
{
库特
{
空条()
{

难道你不需要把
这个
指针作为参数来传递吗?它总是存在于这个类的上下文中。试着直接使用它。@MukulGupta:
这个
问题是针对
类B
,默认情况下
类A
不可用。顺便说一句,我认为你应该重新考虑你的类设计。“我写了一个类,需要从函数的调用方传递“this”。-这听起来真是个坏主意…@cornstales:My bad@Christian Hackl:我使用“this”作为参数,因为我已经封装了创建:
函数(std::bind(func,res)
,其中“res”是“this”的意思"指向此处的指针我的想法与此相似,但我不确定。您添加的内容显示了基本问题非常好。这很有效,是的,但如前所述,我不能将模板与类Aclass一起使用。A类似于我代码中的一个类,它是可调用的,可以加载函数,因此我可以选择其中一个,它可以带来自己的资源。继续简而言之:A必须始终是相同的类型,以便我可以在A的数组中使用其全部功能。如果存在where模板,则不可能将它们全部写入arrayok。这可能会起作用,但我还需要传递函数指针类型,我需要弄清楚如何在template@Meph:有什么用如果每个调用者都有一个方法,比如说“foo()”,Dowitt()应该调用它,那么你可以直接使用
调用者this->foo()
我可以将你的代码改编成我的代码,在我处理了所有关于先前存在的模板的错误之后……我终于让代码运行了(我很快会给出自己的答案).Hmm.你准确地复制了代码吗?我从一个完整的工作程序中复制/粘贴了它。不,我没有复制你的代码,但我使用了你使用的代码
#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;
}