簡體   English   中英

數組模板和內置數組類型之間有區別嗎?

[英]Is there a difference between array templates and built in array types?

我有一個名為“ Encrypt”的C ++類。 該類具有多個功能,包括構造函數,該構造函數與數組數據成員進行交互,並且還接受數組作為參數。 根據我正在閱讀的書,首選使用數組模板並創建如下所示的數組,而不是使用“內置”數組符號。

我的書怎么說創建一個數組:

std::array <int, 8> digits;

我過去如何創建數組:

int digits[8];

我將數組定義(我的第一個示例)包含為類的私有數據成員。 我包括一個接受數組的構造函數,並使用該數組將數據存儲在我的私有數據成員數組中。 我的構造函數原型如下所示:

Encrypt(std::array <int, 8>);

問題是,我的老師提供了一個.cpp驅動程序文件,該文件將數組傳遞給我的構造函數。 他聲明數組的方式如下:

int Long[]={1,2,3,4,5,6,7,8};

因此,他使用了舊的“內置”數組表示法。 他通過創建一個對象並提供數組基地址來調用我的構造函數,如下所示:

Encrypt objName(Long);

當我嘗試編譯程序時,出現以下錯誤:

error C2664: 'Encrypt::Encrypt(const Encrypt &)' : cannot convert argument 1 from 'int[8]' to '_int64'

所以我的猜測是,使用內置數組符號創建的數組與從模板構造的數組不同。 我不確定是否遺漏了一些東西,因為我不明白為什么老師會使用與書中所說的不兼容的數組(這是在線課程,所以我從書中自學)。

這是我要求的“ MCVE”-如果我遺漏了一些東西,請發表評論-我試圖將我的課程濃縮為僅重要的代碼。

我的.h文件中的內容:

class Encrypt
{
public:
   Encrypt(long long int);             // class constructor to store last 4 digits of input
   Encrypt(std::array <int, 8>);       // second class constructor to store first 4 digits
private:
    std::array <int, 8> digits;  // data member array to hold digit inputs and encrypted data
};

我的.cpp類實現文件的內容(剝離了不相關的代碼):

#include "Encrypt.h"
#include <iostream>
#include <array>

using namespace std;
// begin first constructor - accepts int input and stores last for digits
Encrypt::Encrypt(long long int input)  // constructor 1 - encrypts and stores data
{
   // removed code to store int argument into private data member array
} // end first constructor

// begin constructor 2 - accepts array and stores first 4 digits
Encrypt::Encrypt(array <int, 8> input)       
{
  // removed code that performs various operations on argument array and
  // private data member array
} // end of second constructor

由我的老師在.cpp“驅動程序”文件中提供的調用我的構造函數的代碼:

#include "Encrypt.h" // include definition of class Encrypt
#include "Decrypt.h" // include definition of class Decrypt
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
using std::atoi;


int main()
{  int Long[]={1,2,3,4,5,6,7,8};
int Negative[]={-8,7,6,5,4,3,2,1};
Encrypt eapp1(0), eapp2(40), eapp3(4560), eapp4(6145698),eapp5(-6), 
    eapp6(Long), eapp7(Negative); // create Encrypt objects

// more code to test program

return 0;
} // end of main

您不能隨意交換C樣式數組和<array>模板,它們是不同的東西。

但是您可以使用data()成員函數訪問基礎數組:

void Encrypt(int* a)
{
    // ....
}

int main(void)
{
    std::array <int, 8> digits;
    Encrypt(digits.data());

    int digits2[] = {1,2,3,4,5,6,7,8};
    Encrypt(digits2);
}

對於強類型,可以使用以下內容:

class Encrypt
{
public:
    Encrypt(const std::array<int, 8>& a) : mArray(a) {}
#ifdef OPTION1
    Encrypt(const int (&a)[8]) : mArray({a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]}) {}
#else
    Encrypt(const int (&a)[8]) { std::copy(std::begin(a), std::end(a), std::begin(mArray)); }
#endif

private:
    std::array<int, 8> mArray;
};

(您可以看到為什么std::array比原始數組更受歡迎)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM