I realize this question may be impossible, but I'm pretty confused so I'm drawing at straws.

I'm trying to use a class from a big library. If I do this:

TheClass imageIterator(image, region);

everything works fine. However, if I do this:

TheClass imageIterator;
imageIterator = TheClass(image, region);

I get a segfault. The two constructors look like this:

  m_NumberOfPixelsInRegion    = 0L;
  m_NumberOfSamplesRequested  = 0L;
  m_NumberOfSamplesDone       = 0L;
  m_Permutation = NULL;

TheClass::TheClass(const ImageType *ptr, const RegionType & region):Parent(ptr, region)
  m_NumberOfPixelsInRegion   = region.GetNumberOfPixels();
  m_NumberOfSamplesRequested = 0L;
  m_NumberOfSamplesDone      = 0L;
  m_Permutation = new RandomPermutation(m_NumberOfPixelsInRegion);

It seems to me like calling the default constructor before the second shouldn't change anything, as everything is done in the default constructor is overridden by the second constructor.

From this tiny bit of information does anyone see an issue with doing this?



6 Years
Discussion Span
Last Post by daviddoria

There is a third part the equation here: To illustrate consider this:

class AClass
    AClass(const int,const int) { std::cout<<"Constructor"<<std::endl;}
    AClass(const AClass&) { std::cout<<"Copy Constructor called"<<std::endl; }
    AClass& operator=(const AClass&) 
       { std::cout<<"Assignment operator called"<<std::endl; 
         return *this; 

int main()
AClass Object(1,1);        // Object constructed with constructor 

Object = AClass(2,2);      // ASSIGNMENT operator called.

So it the case that you have given then the assignment operator is called.

I am assuming that you have written an assignment operator. If you haven't do so now and for EVER other class you have, and while you are at it the destructor. C++ writes you an assignment operator and desctructor and copy constructor if you don't provide one, BUT they are shallow copies and if your class has any kind of complex object you definately need to write your own. The Rule is:
If in 1% doubt about needing to write the constructors/destructors/assignment operator write all 4.

In addition, I see that TheClass is derived from Parent, but you don't call its constructor in your copy consstructor. That is almost certainly unclear if not wrong.

Finally, since somethings like m_NumberOfPixelsInRegion are not constant, you are most likely going to have to write this construct for the assignment operator

TheClass::operator=(const TheClass& A)
   if (this!=&A)
        // ... etc ...
   return *this;

Edited by StuXYZ: n/a

Votes + Comments
Thanks, that was exactly the problem!

The problem must be with how the assignment operator (operator=) is implemented. Is there any documentation regarding that?

Votes + Comments
Thanks, that was exactly the problem!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.