默认情况下是否可以传递"this"?
Is it possible to pass "this" by default?
默认情况下是否可以通过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 A
与this 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.的构造函数中实现
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- 在这种情况下,在"this"前面加上星号有什么作用?
- 默认情况下是否可以传递"this"?
- 为什么在这种情况下是this->属性而不是this.attribute(C++OOP)?
- 在不调用未定义行为的情况下检查成员函数中的this == NULL
- 在什么情况下'this'指针传递给类方法?