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:

TheClass::TheClass()
{
  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?

Thanks,

David

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

class AClass
{
  public:
    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&
TheClass::operator=(const TheClass& A)
{
   if (this!=&A)
     {
        Parent::operator=(A);
        m_NumberOfPixelInRegion=A.m_NumberOfPixelsIRegon;
        // ... etc ...
     }
   return *this;
}
commented: Thanks, that was exactly the problem! +5

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

commented: Thanks, that was exactly the problem! +5

Ah of course - I implemented the assignment operator and all is well.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.