This question is using a class not a structure to pass in to a method... it changes the results that way.
- Passing a class by reference, the method can changes those values.
- Passing a class by value, the method should NOT be able to change the values.
- But here Passing a class by value, the values are changed. WHY?
#include "pch.h" //header from MSVS c++ console app
#include <iostream>
class C_PetList
{
public:
double *mpdVal;
C_PetList()
{
mpdVal = new double[20];
}
~C_PetList() //Placing the destructor in here gives the same results from my other code. All my values are set to -1.4568159901474629e+144
{
if (mpdVal)
{
delete mpdVal;
mpdVal = nullptr;
}
}
};
class C_Pet
{
public:
C_Pet() {}
~C_Pet() {}
void EvalPetList(C_PetList c_PetList)
{
// do something
for (int i = 0; i < 20; i++)
{
c_PetList.mpdVal[i] += 300;
}
}
void ProcessPetList(C_PetList c_PetList)
{
EvalPetList(c_PetList); //passed by value
}
};
int main()
{
C_Pet c_Pet;
C_PetList c_PetList;
for (int i = 0; i < 20; i++)
{
c_PetList.mpdVal[i] = (double)i;
}
c_Pet.ProcessPetList(c_PetList);
}