I have a lot of C# Code that I have to write in C++. I don't have much experience in C++.
I am using Visual Studio 2012 to build. The project is an Static Library in C++ (not in C++/CLI).
I know this is a basic question, but I have been reading around, and I got rather confused. In the C# code they were using double[] arrays in several places, from the way they were using them, I saw the best way to replace them in C++ was using vector.
There are some properties in C# returning copies of the arrays, I want to do the same in C++.
Note: myArray is member of MyClass.
C#
public double[] MyArray
{
get
{
/*** Some Code Here ***/
double[] myCloneArray= (double[])myArray.Clone();
return myCloneArray;
}
}
I want to accomplish something similar in C++, but minimizing the amount of copies created. Would this be correct?
C++
vector<double> MyClass::GetMyArray()
{
/*** Some Code Here ***/
vector<double> myCloneArray= vector<double>(myArray);
return myCloneArray;
}
I just want one copy of myArray to be created. Since I am not returning a reference, It is my understanding that when returning myCloneArray, a copy of it will be created. Is this always true?
I wanted to make sure, so I was reading on internet, but I am confused because some people says that some compilers don't do that.
I want to make sure I am always sending a copy, and not the same vector. I am working right now with a compiler, but ultimately my code will be build in other compilers as well, so I need to be sure this is not compiler-dependant
Is this the best way I can go? or could i reduce the code, like this:
C++
vector<double> MyClass::GetMyArray()
{
/*** Some Code Here ***/
return myArray;
}
And the copy constructor of vector (myArray) will be called?