扩展类 [英] Extending a class

查看:151
本文介绍了扩展类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑回答:虽然我原来的问题没有解释我的需要,正如康拉德·鲁道夫提供的答案解决他们,他(意外或设计)基本上为我写了我正在写的东西!类本身没有扩展,但它的功能扩展通过使类知道新的函数,允许它(类)处理更广泛的问题。我衷心感谢一个很好的答案,反过来也是一个伟大的教程,即使它需要一些昂贵的光吗?

EDIT Answered: Although my original question didn't explain my needs in exactly the way the answer provided by Konrad Rudolph addressed them, He (by accident or design) essentially wrote for me what I was trying to write! The class itself does not get extended, but has it's functionality extended by making the class aware of new functions which allow it (the class) to handle a wider array of issues. My grateful thanks for an excellent answer which in turn is also a great tutorial, even if it does require some ahemm light? reading of books by me ;-).

我相信这是一个非常基本的问题,但我试图自学C ++,以及我的参考书使用提供我不明白的答案。

I am sure that this a very basic question, but I am trying to self teach C++, and the reference books which I am using are providing answers which I do not understand.

对于我的项目,我定义一个 class error_handler {} 处理异常消息和错误号从程序的各个点。它将使用各种成员函数将错误记录到文件和/或将消息打印到屏幕。

For my project I am defining a class error_handler {} which processes exception messages and error numbers from the programs various points. It will log the error to a file and/or print the message to the screen using various member functions.

我的目标是创建它,使它有自己的。 h和.cpp文件,如果我以后需要扩展它只需添加一个新的成员函数来处理一些更加晦涩的错误处理类型。

My goal is to create this so that it has it's own .h and .cpp files, and if I later need to extend it simply add a a new member function to handle some more obscure type of error manipulation.

我的问题是这是:

由于在 error.h 中我有代码:

class error_handler
{
  // ctor dtor copy logger screen functions defined
}

error_handler.cpp 中填写这些定义,

我如何简单地添加一个新的函数到类?我不希望子类,只是扩展它。

How can I simply simply add a new function to the class? I do not wish to sub class it, simply extend it.

对于一个用例场景假设error_handler是一个类,在一个专有包中定义,我不是免费的修改源直接,但允许在单独的代码中扩展它。

For a use case scenario assume that error_handler is a class defined in a proprietary package where I am not free to modify the source directly, but am allowed to extend it in separate code.

编辑:回答和评论到目前为止似乎表明我想做一些语言isn' t意味着。如果是这样,那么,我在这里学到了一些东西。在希望这不是这种情况下,我会留下一个问题,打开一点,看看什么可能漫游到这个帖子......

Answers and comments thus far seem to indicate I am trying to do something the language isn't meant to do. If so then so be it, I have learned something here. In the hopes that this is not the case I will leave the question open for a bit to see what else might wander onto this post......

推荐答案

这在很大程度上取决于这些功能是什么。鉴于您的应用程序,我将继续,假设您要添加的函数是为了处理其他错误类型。

It largely depends on what these functions are. Given your application, I’ll go ahead and assume that the functions you want to add are there to handle additional error types.

为了简单,让我们假设每个函数(让我们称之为 handle )只是传递一个错误对象,它会分析并处理或不处理。它会返回一个 bool 来表示这一点。

To keep it simple, let’s assume that each function (let’s call it handle) simply gets passed an error object which it analyses and either handles, or doesn’t handle. It returns a bool to indicate this.

> error_handler class:

Then you could have the following (simplified) error_handler class:

class error_handler {
public:
    using handler_t = bool (*)(error const&);
    std::vector<handler_t> handlers;

    // …

    void add_handler(handler_t handler) {
        handlers.push_back(handler);
    }

    void handle_error(error const& error) {
        for (auto const& handler : handlers)
            if (handler(error))
                break;
    }
};

这是假设您的错误由 / code>(而且你使用的是C ++ 11 - 语法稍有不同)。

This is assuming that your errors are represented by an instance of class error (and that you’re using C++11 – the syntax changes slightly otherwise).

现在,这个类定义是 fixed 。您可以添加其他处理程序,在其他文件中定义,只需调用add_handler`。以下是两个示例:

Now, this class definition is fixed. You can add other handlers, defined in other files, simply by calling add_handler`. Here are two examples:

// Simple logging "handler". Needs to be added first!

extern error_handler global_error_handler;

bool log_handler(error const& error) {
    std::cerr << error.message() << '\n';
    return false; // We only log, we don’t handle.
}

global_error_handler.add_handler(log_handler);





// Handler for missing dog food

extern error_handler global_error_handler;

errno_t const MISSING_DOG_FOOD = 42;

bool dog_food_missing(error const& error) {
    if (error.code() != MISSING_DOG_FOOD)
        return false;

    global_dog_food_container.add(some_food());
    return true;
}

global_error_handler.add_handler(dog_food_missing);

这篇关于扩展类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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