C ++派生自本地类型 [英] C++ derive from a native type

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

问题描述

在某些C ++代码中,我使用整数存储大量变化的数据。
要分析我的程序,我想记录对某些变量的某些更改,例如某个值被分配的频率,以及该赋值是多余的(新值与旧值相同。)

In some C++ code, I use integers to store lots of changing data. To analyze my program, I want to log certain changes to some of the variables, such as how often a certain value is assigned to, and how often that assignment is redundant (the new value is the same as the old value.)

如果类型是Foo类,我只是派生一个新的LoggingFoo并将我的日志数据添加到我感兴趣的成员函数,然后调用父成员函数。
我必须更新我的代码以使用新类型,但只要我最初与typedefs一致,这是一个单行的变化。

If the type were a class Foo, I'd just derive a new LoggingFoo and add my logging data to the member function(s) I was interested in, and then call the parent member function. I'd have to update my code to use the new type, but as long as I was originally consistent with typedefs, that's a one-line change.

我的问题是,我想添加日志记录的变量是一个int。你不能从C ++中的内置类型派生(你能吗?)

My problem is that the variable I want to add logging to is an int. You can't derive from built in types in C++ (can you?)

我的问题是是否有一个聪明的方法从基本类型派生双,等)。

My question is whether there's a clever way to derive from the basic types (int, float, double, etc).

解决方案可能是定义有效的新类。它定义每个 int可以做,只是将该操作应用于私有int数据成员。
这个策略将工作,但也许其他人已经做了这样的类,我可以只使用fakeint.h头文件定义。在我自己实现它们之前,有没有这样的代理本地类包装器类型定义已经可用?

The solution may be to define a new class that effectively is an int.. it defines every operation an int can do and just applies that operation to a private int data member. This strategy will work, but perhaps others have already made such a class and I can just use a "fakeint.h" header file definition. Is there such a "proxy native class wrapper" type definitions already available somewhere before I implement them myself?

我实现当然我的代理int不能互换使用与一个int特别是因为现有的函数定义都期望一个int ..但对于我的应用程序,这是所有在一个内部循环,这是做很多简单的原生+ - * ^ =操作,不用作函数参数或任何东西。 p>

I do realize of course my proxy int can't be used interchangably with an int especially since existing function definitions all expect an int.. but for my app, this is all in an inner loop which is doing lots of simple native +-*^= operations, not used as function arguments or anything.

推荐答案

像这样...

template <typename T> class logging_type
{
private:
   T value;
public:
   logging_type() { }
   logging_type (T v) : value(v) { }  // allow myClass = T
   operator T () { return value; }  // allow T = myClass
   // Add any operators you need here.
};

这将创建一个可以在两个方向转换为原始类型的模板类。您需要为代码中该类型上使用的每个操作添加日志处理和重载运算符。

This will create a template class that's convertible to the original type in both directions. You'd need to add logging handling and overload operators for every operation used on that type in your code.

这可能不是您想要的,因为它是隐式的可转换为int(或任何你指定的类型),所以你的代码可能会将你的日志int转换为一个int,你会留下不完整的日志。你可以通过在构造函数中添加一个'explicit'关键字来防止这种情况发生,但你不能使用转换操作符做任何类似的事情。除非你可能让它私人...我没有试过。

This still might not be quite what you want, because it's implicitly convertible to int (or whatever type you specify), so your code might silently convert your logging int to an int and you'd be left with incomplete logs. You can prevent this in one direction by adding an 'explicit' keyword to the constructor, but you can't do anything similar with the conversion operator. Unless you perhaps make it private... I haven't tried. Doing either of those things will somewhat defeat the purpose though.

编辑:从c ++ 11开始,您可以 >为转换运算符添加显式

Edit: Since c++11, you can add explicit to conversion operators.

这篇关于C ++派生自本地类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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