effective C++ warning mingW

Hello, I use CodeBlocks/MingW32 on Windows XP. I turned on the "allow effective c++ warnings (thanks Scott Meyers) option in the compiler/linker options menu, and the result was this:

warning: 'class SoundEffects' has pointer data members
warning: but does not override 'SoundEffects(const SoundEffects&)
warning: or 'operator=(const SoundEffects&)'

This SoundEffects class is a stand-alone basic class, with no children or parents and no const member functions as it isn't passed by constant reference (yet), so I'm wondering what the method is to satisfy the effective c++ warning in this instance. Any and all responses welcome, and thanks in advance! -B
if you don't declare a copy constructor or operator= in a class then the compiler will make one for you in which it just uses operator= on all data members.

You have pointer data members, if you use the standard operator= on pointers it just makes that pointer point towards the same data as the variable on the right hand side of the operator, so you would end up with several objects which all use the same memory, and if you delete one of these objects it will delete the data pointed to by the pointer data members in that object, which other objects need.

Declare your own operator== and copy constructors which allocate new data then copy the contents over, giving each object of that type its own seperate memory for the pointer members.

I hope this was clear...I don't think I explained it very well.
Last edited on
I did this:

1
2
SoundEffects(const SoundEffects &); // copy constructor
SoundEffects & operator = (const SoundEffects &); // override 


and the Scott Meyers warnings have been quieted for now. When you wrote

Declare your own operator==


did you really mean ==, or just =? Thanx for the response! -B
Ok, good, but have you defined them? And I believe that he did mean the assignment operator and not the comparison operator.
if you don't declare a copy constructor or operator= in a class then the compiler will make one for you in which it just uses operator= on all data members.


I just figured out I didn't know exactly how the default operator=/copy constructor work. I think this illustrates it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

class theDataEntry
{
public:
  int x;
  theDataEntry(){this->x=0;}
  theDataEntry(const theDataEntry& other){this->x=other.x; std::cout << "Data copy constructor called\n"; }
  theDataEntry& operator=(const theDataEntry& other){this->x=other.x; std::cout << "Data operator= called\n"; return *this;}
};

class theClass
{
public:
  theDataEntry y;
};

int main()
{ theClass A,B;
  A=B;
  theClass C=A;
  std::cin >> A.y.x;
  return 0;
}

In other words, the synthesized copy constructor calls the copy constructors of all data members; the synthesized operator= calls the operator= of all data members.

[Edit:] Warning: if you have copy constructor only, but no operator=, the automatic operator= will be synthesized for you. Also, if you have operator=, but no copy constructor, the copy constructor will be synthesized for you (rather than use the operator=). To check that, simply comment out the operator= of theDataEntry.
Last edited on
Topic archived. No new replies allowed.