将类传递给函数,而不是对象 [英] passing classes to functions, not objects

查看:73
本文介绍了将类传递给函数,而不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以对课程进行哪些操作?我知道


A级{};

typeid(A);


有效,但还有什么可以你做?有没有办法将类作为

参数传递给函数?班级指针?这是怎么回事


A级{};

void(A :: *)();


我正在寻找更多的准全面讨论。干杯!

What sort of operations can one do with classes? I know

class A{};
typeid(A);

works, but what else can you do? Is there a way to pass a class as a
parameter to a function? class pointers? how does this relate to

class A{};
void (A::*)();

I''m looking for more of a quasi-comprehensive discussion. Cheers!

推荐答案

" Patrick Stinson" < AJ ***** @ gci.net>在消息中写道

新闻:10 ************* @ corp.supernews.com ...
"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
什么样的操作可以一个做什么课?我知道

A类{};
typeid(A);

有效,但你还能做些什么?有没有办法将类作为
参数传递给函数?班级指针?
What sort of operations can one do with classes? I know

class A{};
typeid(A);

works, but what else can you do? Is there a way to pass a class as a
parameter to a function? class pointers?




我不知道。模板参数可能会起到某种效果,但必须在编译时确定。

问题是你想用你的假设类指针做些什么。

实例化课程?调用(可能是静态的)方法吗?要在运行时执行此类操作,需要比目前提供的C ++
更广泛的RTTI。如果你想了解一个类型的成员,

他们的参数,然后在运行时调用它们,需要某种形式的

反射,而且C ++根本没有提供。


-

不可原谅



Not that I know of. Template parameters might serve to something in that
effect, but it has to be determined at compile time.
The question is what you want to do with your hypothetical class pointers.
Instantiate the class? Call (possibly static) methods on it? To do that kind
of thing at runtime, some more extensive RTTI is needed than what C++
currently provides. If you''d want to find out about the members of a type,
their parameters, and then call them, all at runtime, some form of
reflection is needed, and C++ doesn''t provide that at all.

--
Unforgiven



" Unforgiven" < JA ******* @ hotmail.com>在消息中写道

news:2q ************ @ uni-berlin.de ...

"Unforgiven" <ja*******@hotmail.com> wrote in message
news:2q************@uni-berlin.de...
" Patrick Stinson" < AJ ***** @ gci.net>在消息中写道
新闻:10 ************* @ corp.supernews.com ...
"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
可以用类做什么样的操作?我知道

A类{};
typeid(A);

有效,但你还能做些什么?有没有办法将类作为
参数传递给函数?班级指针?
不是我所知道的。模板参数可能会起到某种效果的作用,但必须在编译时确定。
问题是你想用你的假设类指针做什么。
实例化类?调用(可能是静态的)方法吗?要做到这一点
What sort of operations can one do with classes? I know

class A{};
typeid(A);

works, but what else can you do? Is there a way to pass a class as a
parameter to a function? class pointers?
Not that I know of. Template parameters might serve to something in that
effect, but it has to be determined at compile time.
The question is what you want to do with your hypothetical class pointers.
Instantiate the class? Call (possibly static) methods on it? To do that



在运行时这样的事情,需要比C ++目前提供的更广泛的RTTI。如果你想了解一个类型的成员,他们的参数,然后调用它们,所有在运行时,都需要某种形式的反射,并且C ++没有提供这一点。

-
Unforgiven


kind of thing at runtime, some more extensive RTTI is needed than what C++
currently provides. If you''d want to find out about the members of a type,
their parameters, and then call them, all at runtime, some form of
reflection is needed, and C++ doesn''t provide that at all.

--
Unforgiven




这是C ++而不是Java,在C ++类中不是对象,所以你没有

指针

到班级。你可以得到的最接近的是通过使用模板,通过

创建

a模板,使用类参数可以捕获一些类型

信息

关于作为对象的类。但是,这都是编译时的知识。


你关心的是什么特别的问题?


dave




This is C++ not Java, in C++ classes are not objects, so you don''t have
pointers
to classes. The closest you can get is through the use of templates, by
creating
a template with a class parameter you can capture some of the type
information
about the class as an object. However, this is all compile time knowledge.

What particular problem were you concerned with?

dave



" Patrick Stinson" < AJ ***** @ gci.net>在消息中写道

新闻:10 ************* @ corp.supernews.com ...
"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
什么样的操作可以一个做什么课?我知道

A类{};
typeid(A);

有效,但你还能做些什么?有没有办法将类作为
参数传递给函数?班级指针?这与

A类有什么关系;
void(A :: *)();

我正在寻找更多的准 - 全面的讨论。干杯!
What sort of operations can one do with classes? I know

class A{};
typeid(A);

works, but what else can you do? Is there a way to pass a class as a
parameter to a function? class pointers? how does this relate to

class A{};
void (A::*)();

I''m looking for more of a quasi-comprehensive discussion. Cheers!




您可以设计自己的一组反射类。这不容易,

但看起来它应该是可行的。 。 。也许。但是,你可能不得不在这个过程中重新实现整个语言。实际上,我在学校的这个本科生研究小组正在做那种只用Java的东西。我们正在扩展所有Java反射类

并用它做一些非常讨厌/疯狂的东西。尽管如此,我在我的

教育方面很少有任何实际的帮助,所以在这里,你需要在其他地方寻求建议。


只要记住计算机科学的第一条规则:添加一个级别的

间接可以解决任何问题。 < - 这是一半的舌头,一半

严重。



You could design your own set of reflection classes. It would not be easy,
but it seems like it should be doable . . . maybe. You might have to
reimplement the whole language in the process, though. Actually, the
undergrad research group I am with here at school is doing that kind of
thing, only with Java. We''re extending all of the Java reflection classes
and doing some pretty nasty/crazy stuff with it. I''m a little early in my
education to be of any real help with the effort though, so in this, you
would have to look elsewhere for advice.

Just remember the first rule of computer science: "Adding a level of
indirection can solve any problem." <-- This is half tongue in cheek, half
serious.


这篇关于将类传递给函数,而不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆