0

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);
}
jdl
  • 6,151
  • 19
  • 83
  • 132
  • 5
    You have a rule of three violation. You need to provide proper copy operations otherwise all of the copies share the same array – NathanOliver Jul 31 '19 at 18:52
  • *Passing a class by value, the method should NOT be able to change the values.* Not sure where you got that information from. It's not true (as your code proves). It is perhaps *desirable* to write your classes so that statement is true, but nothing means it has to be true. – john Jul 31 '19 at 18:53
  • 1
    The pointer `mpdVal` itself is copied, but what it points to is not. You could [copy the array it points to in a copy constructor](https://en.cppreference.com/w/cpp/language/rule_of_three). Or you could use a `std::vector` and forget about all this trouble. – HolyBlackCat Jul 31 '19 at 18:53
  • This is using a class not a structure to pass in to a method... it changes the results that way. – jdl Jul 31 '19 at 18:54
  • 1
    A class and a struct are the same thing in c++ – NathanOliver Jul 31 '19 at 18:55
  • 3
    @jdl The only difference between `class` and `struct` is the default member (and base class) access (`public` vs `private`). In fact, strictly speaking there are no strucures in C++, `struct` creates classes. – HolyBlackCat Jul 31 '19 at 18:55
  • What's happening here is that the original object and the copy passed by value are sharing information (the dynamically allocated array pointed to by `mpdVal`) so obviously any changes to that array are visible to all objects which share that array, It's up to you to write your class in such a way that this sharing does not happen (if that's what you want). – john Jul 31 '19 at 18:55
  • Pointers... That makes a lot more sense. –  Jul 31 '19 at 18:57
  • https://stackoverflow.com/q/388242/5910058 – Jesper Juhl Jul 31 '19 at 19:37

0 Answers0