English 中文(简体)
如何在C++标准图书馆班上超载已超载的经营者?
原标题:Ways to overload an already overloaded operator in a C++ standard library class?

我试图执行自己的矩阵类别(2个阵列),该类别使用<>操作者获取其内容和std:unique_ptr<>加以控制——我知道解决这一问题的方法比较容易/更好,但我希望尽可能这样做。

我可以超负荷[]运营商的舱位,但不能 st:unique_ptr,因为它已经这样做。

matrix m(5,5, 1);     //create a 5x5 matrix filled with 1 s
matrix[row];          //this uses my overloaded [] and returns a unique_ptr
matrix[row][column];  //this uses unique_ptr s overloaded [], can t do my own checks

是否有通过[]经营者超负荷或拦截一栏指数的恰当途径?

我认为,为以下几个阶段创造了一个包裹:unique_ptr<>这从中继承,但我更喜欢使用一个纯粹的阶级。

问题回答

由于非常好的原因,你不能直接这样做,但你可以做一个总结,使之达到look和感觉,就像你能够描述所回的价值一样。 此处使用<代码>的Im:array<int,16>,作为您的矩阵级的备份。

auto const ptr = std::make_unique<std::array<int,16>>();
At{ptr}[7] = 8;
assert(At{ptr}[7] == 8);

// the following should not compile:
At at{ptr};
at[7] = 12;

这是可能的、简明的、执行:

template <typename T>
class At {
  private:
    T& value;
  public:
    At(std::unique_ptr<T> const& ptr) : value{*ptr} {}
    template <typename I>
    decltype(auto) operator[](I const i) const&& {
      return value[i];
    }
};

请注意:At:operator[ ] 仅涉及最高价值参考文献,从而强烈阻止升值,且用户不希望保留 周围的物体。 这不是完美的,因为如果你真想的话,你可以找到一条解决办法。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签