【问题标题】:How to get the length of array while overloading the = operator如何在重载 = 运算符时获取数组的长度
【发布时间】:2023-03-12 09:57:01
【问题描述】:
class myclass{
//definitions here
};
myclass e;
int myarray[10];
/*
Do something...
*/
e = myarray;

为了使e = myarray 成为可能,我重载了 = 运算符。而且我必须得到传入数组长度的长度。

template <class T>
int getarrlen(T& arr)
{
    return sizeof(arr) / sizeof(arr[0]);
}

myclass::operator=(int obj[]) {
    int len=getarrlen(obj);
    //Do something...
}

但是getarrlen(obj)的返回值始终是1。

那么,重载函数中如何获取obj[]的长度呢? 顺便说一句,我也试过int size = *(&amp;arr + 1) - arr;,也没用。

更新0: 为此:

template<typename T1, int size>
int getarrlen(T1(&)[size]) { return size; }

我有一个C2784 compiler-error in Visual Studio...奇怪... 更新1: @AlgirdasPreidžius 的链接提供的代码适用于主要功能,但不适用于我的代码:( 另外,为了更明显,我试过这个:

#include<iostream>
using namespace std;
int x[10];
//Begin of the copied code
template <std::size_t N>
struct type_of_size
{
    typedef char type[N];
};

template <typename T, std::size_t Size>
typename type_of_size<Size>::type& sizeof_array_helper(T(&)[Size]);

#define sizeof_array(pArray) sizeof(sizeof_array_helper(pArray))
//End
void myv(int a[]) {
    const std::size_t n = sizeof_array(a); // constant-expression!
    cout << n << endl;
}

int main() {
    int a[20] = {1,2,3,4,5};
    myv(a);
}

代码不起作用。而且我尝试在void myv(int a[]) { 上方添加template &lt;typename T, std::size_t Size&gt;,但它也不起作用......

【问题讨论】:

  • 您推导数组长度的模板错误。看看这个问题:How does this “size of array” template function work?
  • 这行不通,您还需要将数组 size 作为模板参数。如何做到这一点已在互联网上展示并很容易找到。
  • myclass::operator=(std::pair&lt;int*, size_t&gt; ptr_and_len)
  • @AlgirdasPreidžius MSCV 提供的链接中的答案似乎没有问题:Demo on CompilerExplorer
  • operator= 使用与 sizeof 相同的模板重载(在修复后者之后...)

标签: c++ arrays operator-overloading


【解决方案1】:

我准备了一个解决方案,让您可以按照问题顶部所示的方式完成作业。

我用相同的机制实现了赋值运算符和复制构造函数。我现在只为 C 风格的 int 数组工作,因为 std::vector&lt;int&gt; 类包含整数。

我们需要使用模板,以便编译器有机会推断类型。

该软件已经过 Microsoft Visual Studio Community 2019 测试 版本 16.5.2。

请看:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

struct Test {
    // The data. At the moment fixed to "int"
    std::vector<int> data{};

    // Default constructor
    Test() {}

    // Copy constructor
    template<typename T>
    Test(T& t) {

        // Number of Elements
        size_t size = (sizeof (decltype(t))) / (sizeof (std::remove_extent<std::remove_reference<decltype(t)>::type>::type));
        // Copy the data
        data.clear();
        std::copy_n(&t[0], size, std::back_inserter(data));
    }

    // Assignment operator
    template<typename T>
    Test &operator =(T& t) {

        // Number of Elements
        size_t size = (sizeof(decltype(t))) / (sizeof(std::remove_extent<std::remove_reference<decltype(t)>::type>::type));
        // Copy the data
        data.clear();
        std::copy_n(&t[0], size, std::back_inserter(data));
        return *this;
    }
};

int main() {

    // Some simple Lambda to print the contents of an STL container
    auto print = [](const auto& container) -> void { std::copy(container.begin(), container.end(),
        std::ostream_iterator<std::decay<decltype(*container.begin())>::type>(std::cout, " ")); std::cout << '\n'; };

    // Define an Plain C-Style array
    int a[10] = {0,1,2,3,4,5,6,7,8,9};

    // Define instance of class
    Test t1;
    // Assignment
    t1 = a;

    print(t1.data);

    // Define instance of another class and use copy constructor
    Test t2(a);

    print(t2.data);
    return 0;
}

【讨论】:

  • 我不建议为任意类型重载operator =。相反,您可能应该重载它仅用于引用数组。
  • 我永远不会这样做,但它正在回答 OP 的问题。您应该评论 OP 的问题。 . .
  • 我的评论不是关于 OP 的问题(这是合理的,尽管对数组和指针之间的区别感到困惑)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多