如何在重载=运算符的情况下获取数组的长度?

问题描述 投票:2回答:1
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 = *(&arr + 1) - arr; 也没有用。

更新0。为了这个

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

我得到了一个 在Visual Studio中出现C2784编译器错误。... 奇怪...更新1.从@AlgirdasPreidžius的链接中提供的代码在主函数中可以使用,但在我的代码中却无法使用。来自@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);
}

代码不工作。我还试着添加了 template <typename T, std::size_t Size> 以上 void myv(int a[]) { 但也没有成功......

c++ arrays operator-overloading
1个回答
0
投票

我准备了一个解决方案,允许你做如你问题顶部所示的赋值。

我实现了一个赋值操作符和一个具有相同机制的复制构造函数。我将只为C-Style工作 int 阵列,因为 std::vector<int> 类中包含ints。

我们需要使用模板,这样编译器才有机会推导出类型。

该软件已被Microsoft Visual Studio Community 2019Version 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.