检测虚拟方法(detecting virtual methods)

编程入门 行业动态 更新时间:2024-10-24 05:22:21
检测虚拟方法(detecting virtual methods)

我有一个类解析文档并调用派生类来通知在流中找到的某些事件。 因为解析器必须处理编码并将文档的一部分重写为字符串,所以如果虚拟方法没有被覆盖,我希望避免重写。 伪代码:

class Parser { virtual void Callback ( string ) { // Do nothing } private: void ParseFile() { // Parse the file in the files encoding // Checks and more checks // Found sth. So need to convert to a string a call the // callback, but if there is no callback what's the point // in wasting cycles? if ( Callback ) // In the world of C we could check for a func pointer Callback( /* with the string */ ) } } class User : public Parser { void Callback ( string ) { // This string means sth. me } } class NonUser : public Parser { // I don't care about Callback so I won't implement it }

在C代码中,我们将编写一个函数并将指向该函数的指针传递给实现。 在内部,它会检查指针是否指向某个东西并调用它。 如何使用派生类实现相同的功能? 即。 在浪费时间/ CPU周期/内存构造甚至可能不需要的字符串之前,检查虚拟方法是否实际存在于派生类中。

编译时间检查,提供便携,是理想的,但我认为这与v表有关,所以运行时检查也可以。

经过更多的研究后,很明显v-table已经出局,而且这几乎是不可能的。 我会略微改变我的问题,问上面是否可能使用某种类型的,也许是模板,欺骗?

I have a class which parses a document and calls a derived class to notify certain events found in the stream. Because the parser has to handle encodings and re write parts of the document into strings, I would like to avoid re-writing if the virtual method has not been over ridden. pseudocode:

class Parser { virtual void Callback ( string ) { // Do nothing } private: void ParseFile() { // Parse the file in the files encoding // Checks and more checks // Found sth. So need to convert to a string a call the // callback, but if there is no callback what's the point // in wasting cycles? if ( Callback ) // In the world of C we could check for a func pointer Callback( /* with the string */ ) } } class User : public Parser { void Callback ( string ) { // This string means sth. me } } class NonUser : public Parser { // I don't care about Callback so I won't implement it }

In C code, we would write a function and pass a pointer to that function to the implementation. Internally it would check if the pointer points to something and call it. How can the same be implemented with derived classes? ie. check if the virtual method actually exists in a derived class before wasting time/CPU cycles/memory constructing a string that may not even be required.

Compile time checking, providing it's portable, would be ideal, but I'm thinking this has something to do with v-tables so run time checking may also work.

After more research it has become apparent that v-tables are out, and that this is pretty much impossible. I'll change my question slightly to ask is the above possible using some type of, maybe template, trickery?

最满意答案

您可以将参数设置为std::future ,并使用简单的派生方法。

或者代替字符串,传入代理或可以提供它的函数。

或者转动表格,不传递任何东西,并在Base中有一个成员函数,它返回创建的字符串。

另一种方法是为该特性提供虚拟谓词函数:在您的基类中它返回false,其他返回true,因此您可以在实际工作之前调用它。 或者它可以为实现返回一个指向函数的指针,如果它可能在静态函数中可行的话。 (这可以作为另一个参数传递......)

读你研究结论vtables会很有趣。

如果您可以在编译时告诉目标,模板只会帮助您。 您所说的问题意味着处理仅在运行时出现的信息。 这需要一些实际传递的指针或其他信息。 但是你可能会想出一个对问题的精炼描述。

You could make the parameter a std::future, and go with the simple derived approach.

Or instead of string, pass in a proxy or a function that can provide it.

Or turn the table around, pass nothing and have a member function in Base that returns the string as it would be created.

Another approach is to have a virtual predicate function to the feature: in your base class it returns false, others return true, so you can call it before the real work doing. Alternatively it could return a pointer-to-function for the implementation, if it's likely to be doable in a static function. (And this can be passed as another param...)

It would be interesting to read you research concluding vtables out.

Templates only help you if you can tell the target at compile time. The problem as you stated it implies processing information that appears only at runtime. And that requires some pointers or other information actually passed around. But you might come up with a refined description of the problem.

更多推荐

本文发布于:2023-07-05 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   detecting   methods   virtual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!