[英]dereferencing void pointers to objects
Given the following piece of code as a minimum working example:给定以下代码作为最低工作示例:
#include <iostream>
class Object_A
{
public:
int attribute_a,attribute_b;
Object_A(int a,int b){
this->attribute_a = a;
this->attribute_b = b;
}
int object_method(){
std::cout << "Hello from Object A: " << std::endl;
return (this->a + this->b);
}
};
class Object_B
{
public:
int attribute_c, attribute_d;
Object_B(int c,int d){
this->attribute_c = c;
this->attribute_d = d;
}
int object_method(){
std::cout << "Hello from Object B" << std::endl;
return (this->c + this->d);
}
};
class Object_Omega
{
public:
Object_A alpha;
Object_B beta;
Object_Omega (Object_A first, Object_B second):
alpha(first),
beta(second)
{}
int foobar(int a){ return a; }
};
void according_to_user_input(bool input,Object_Omega obj)
{
void * either_A_or_B;
if (input){ either_A_or_B = & obj.alpha; }
else { either_A_or_B = & obj.beta; }
/* problem here */
for(int i = 0; i < either_A_or_B->object_method(); i++)
{
std::cout << i << std::endl;
}
}
int main()
{
/* this happens somewhere */
Object_A new_A = Object_A(1,2);
Object_B new_B = Object_B(3,4);
Object_Omega W = Object_Omega(new_A, new_B);
according_to_user_input(true/*false*/, W);
return 0;
}
I wish to use a void
pointer (eg, inside the function according_to_user_input
) but I am struggling to comprehend the proper way to do this.我希望使用一个
void
指针(例如,在 function 中according_to_user_input
_to_user_input ),但我很难理解正确的方法来做到这一点。 Similar questions have been asked [1] , [2] but I did not manage to make this work in the case of the values being pointed towards from the pointer being custom objects.已经提出了类似的问题[1] , [2]但在从指针指向的值是自定义对象的情况下,我没有设法完成这项工作。
Given the user input I wish to invoke the object_method()
either from A
or from B
through the Omega
object.鉴于用户输入,我希望通过
Omega
object 从A
或B
调用object_method()
。 Unfortunately the code that I've been working on has this showcased behavior as is and not as an inheritance relationship between the objects in order to avoid the ugly void
C-like and definitely not C++ -like scenario.不幸的是,我一直在处理的代码按原样展示了这种行为,而不是作为对象之间的 inheritance 关系,以避免丑陋的
void
C-like 并且绝对不是 C++ -like 场景。
Thank you in advance!先感谢您!
You cannot indirect through a pointer to void
.您不能通过指向
void
的指针间接。 And there is no need for a pointer to void
.并且不需要指向
void
的指针。
The simplest solution is this:最简单的解决方案是这样的:
int number = 1;
std::cout << input
? obj.alpha.object_method(number)
: obj.beta.object_method(number);
For the edited question: The above still works, but you can get rid of the repetition of the complex code using a function template.对于已编辑的问题:上述方法仍然有效,但您可以使用 function 模板摆脱复杂代码的重复。 Here is an example using a lambda:
这是使用 lambda 的示例:
auto do_stuff = [](auto& either_A_or_B) {
for(int i = 0; i < either_A_or_B.object_method(); i++)
{
std::cout << i << std::endl;
}
};
input
? do_stuff(obj.alpha)
: do_stuff(obj.beta);
PS Avoid using C-style casts. PS 避免使用 C 风格的强制转换。
This is not a good use for void*
and if, like you comment, you can't make use of inheritance and the actual problem is more than just console output, then you could still make it generic with a function template这不是
void*
的好用处,如果像您评论的那样,您不能使用 inheritance 并且实际问题不仅仅是控制台 output,那么您仍然可以使用 ZC1C425264E68384F1AB450 模板使其通用
template <class T>
void according_to_user_input(T &obj)
{
std::cout << obj.object_method();
}
and use it like so并像这样使用它
if (input)
according_to_user_input<Object_A>(W.alpha);
else
according_to_user_input<Object_B>(W.beta);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.