[英]C++: Passing an array to a constructor and saving it in the this object
我想在C ++中执行以下JavaScript代码:
class Example {
constructor(myArr) {
this.myArr = myArr;
}
test() {
console.log(myArr[0])
}
}
var myArr = [1,2,3,4]
var example = new Example(myArr)
example.test()
// prints `1`
我的C ++尝试:
#include <iostream>
class Example {
public:
int myArr[4];
Example(int myArr[4]);
void test(void);
};
Example::Example(int myArr[4]) {
//this.myArr = myArr;
this->myArr = myArr;
//myArr = myArr;
}
void Example::test(void) {
std::cout << this->myArr[0];
}
int main() {
int myArr[4] = {1,2,3,4};
Example *example = new Example(myArr);
example->test();
}
我真的不明白我在做什么错。 我尝试了很多不同的事情,例如使用this->
代替this.
,但我总是会出错。 例如,当前版本给我:
In constructor 'Example::Example(int*)':
12:17: error: incompatible types in assignment of 'int*' to 'int [4]'
我真的不明白我在做什么错。 我在代码中的任何地方都将数组指定为int myArr[4]
,我不记得在任何地方传递int*
。
#include <iostream>
#include <vector>
class Example {
public:
std::vector<int> myArr;
Example(const std::vector<int> & myArr);
void test();
};
Example::Example(const std::vector<int>& myArr) {
this->myArr = myArr;
}
void Example::test() {
std::cout << this->myArr[0];
}
int main() {
std::vector<int> myVec{1,2,3,4};
Example example1(myVec);
// Or directly
Example example2({1, 2, 3, 4});
// And is you don't have choice and use raw array
int myArr[4] = { 1, 2, 3, 4 };
Example example3(std::vector<int>(myArr, myArr + 4));
example1.test();
example2.test();
example3.test();
}
传递向量时,建议您使用参考,以避免向量的无用复制
另外,在此示例中,对Example
使用动态分配也Example
。 而且,当您使用动态分配时,请不要忘记delete
对象(或者最好使用智能指针)
个人建议:
vector
, array
, list
, map
,...。它们简化了复制和标准操作 下面是原始数组版本,至少适用于c ++ 11,但我建议使用std::vector
或std::array
:
#include <iostream>
class Example {
public:
int * myArr = nullptr;
std::size_t length = 0;
Example(const int * const myArr, std::size_t length);
Example(const Example & old); // To respect rule of 5
Example(Example && old); // To respect rule of 5
Example &operator=(const Example &rhs); // To respect rule of 5
Example &operator=(Example &&rhs); // To respect rule of 5
~Example();
void test();
};
Example::Example(const int * const myArr, std::size_t length) {
this->myArr = new int[length];
this->length = length;
for(std::size_t i = 0; i < length; ++i) {
this->myArr[i] = myArr[i];
}
}
// Copy constructor
Example::Example(const Example &old) {
*this = old;
}
// Move constructor
Example::Example(Example &&old) {
*this = std::move(old);
}
// Copy assignement operator
Example &Example::operator=(const Example & rhs)
{
length = rhs.length;
for(std::size_t i = 0; i < length; ++i) {
myArr[i] = rhs.myArr[i];
}
return *this;
}
// Move assignement operator
Example &Example::operator=(Example &&rhs)
{
myArr = rhs.myArr;
length = rhs.length;
rhs.myArr = nullptr;
rhs.length = 0;
return *this;
}
void Example::test() {
std::cout << this->myArr[0];
}
Example::~Example()
{
if (myArr != nullptr)
delete myArr;
}
int main() {
int myArr[4] = {1,2,3,4};
Example example1(myArr, 4);
example1.test();
}
我觉得与您分享一种更面向C ++的方法可能是个好主意:
#include <iostream>
#include <array> // uses same amount of memory as a standard array
// but has a set size unlike std::vector
class Example {
public:
std::array<int, 4> myArr;
Example(const std::array<int, 4>& myArr);
void test(void);
};
Example::Example(const std::array<int, 4>& myArr)
: myArr{myArr} // initialize myArr directly using a member
// initializer list.
{}
void Example::test(void) {
std::cout << myArr[0]; // no need for this-> as it is implicit
}
int main() {
// prefer brace initialization
std::array<int, 4> myArr{1, 2, 3, 4};
Example example{myArr}; // avoid pointer so that delete does not have
// to be called explicitly
example.test();
// example and myArr will have their destructors
// called automatically when they go out of scope
// as per RAII
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.