1

Consider the following simple program. It just defines two variables A and B from MyStruct and then initializes A.

How can I copy A to B with new pointers for B?

If I use assignment operator, firstMember of B will be assigned with firstMember of A and whenever I change the value of B.firstMember[i], the value of A.firstMember[i] will change. I already know that it can be done with a function, but is there any simpler way? Assuming that MyStruct can have lots pointers, writing a function doesn't seem to be good.

typedef struct MyStruct
{
    int * firstMember;
    int secondMember;
} MyStruct;

int main()
{
    MyStruct A;
    MyStruct B;

    Initialize(A);            // initializes A such that A.firstMember[0] != 5
    B = A;
    B.firstMember[0] = 5;
    cout<<A.firstMember[0];   // prints 5

    return 0;
}
user229044
  • 232,980
  • 40
  • 330
  • 338
mrmowji
  • 934
  • 8
  • 29
  • 2
    Use a copy constructor/overloaded assignment operator – vsoftco May 30 '14 at 02:21
  • Overload the assignment operator (`operator=`)with the desired behavior. http://stackoverflow.com/questions/4421706/operator-overloading – Benjamin Lindley May 30 '14 at 02:22
  • 1
    http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Bryan Chen May 30 '14 at 02:23
  • 3
    @Mowji Or, you could just not use pointers and switch to things like `std::vector` – yizzlez May 30 '14 at 02:33
  • @Mowji let's be very clear that the best solution is to only use member variables which do not require any custom copying/deletion code . Anything you can do with `int *firstMember`, you can almost certainly rewrite to use a well-behaved member. As well as keeping your code simple and clear, this technique helps your compiler make efficient output. – M.M May 30 '14 at 05:11
  • @MattMcNabb Can you suggest any alternatives beside vectors? Thanks. – mrmowji May 30 '14 at 11:31
  • no, use `vector`. You could roll your own half-assed vector but that would be a waste of time. – M.M May 30 '14 at 11:57

2 Answers2

1

This falls into the "Rule of 3". Once you start managing raw pointers inside a class, you probably want to define a custom contructor, destructor, and assignment operator for this very reason.

Yeraze
  • 3,269
  • 4
  • 28
  • 42
0

First of all, I don't understand why among two members, one is a pointer, and other is not. I am presuming that the first member which is a pointer points to a single integer. Here's an improved code snippet for the structure:

struct MyStruct
{
    int * firstMember;
    int secondMember;
    MyStruct& operator = (const MyStruct& src) {
      *firstMember = *src.firstMember;
      return *this;
    }
    MyStruct(int a = 0) {
      firstMember = new int(a);
      secondMember = a;
    }
    MyStruct(const MyStruct& src) {
        // deep copy
        firstMember = new int(*src.firstMember);
        secondMember = src.secondMember;
    }
    ~MyStruct() {
        if (firstMember)
          delete firstMember;
    }
};

Note, struct is a class with all members declared as public by default. Rather than having Initialize in C-way, better to use C++ proper construct like constructor, and you need to have copy constructor and assignment operator too.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69