tags:

views:

220

answers:

5

Just when I thought I had it figured out, I get an exception handling error. Problem: The problem is that the private members lose the information outside of the constructor. Here's my class definition

Code:

class ClassType
{
    private:
             char *cPointer;
             int length;

    public:
            ClassType();
            // default constr. needed when allocating  in main.
            ClassType( const ClassType* );
            char otherFunc();    
};

classtype.cpp:

"#include ClassType.h"

ClassType( const ClassType* )
{
    cPointer = ClassType->cPointer;
    length = ClassType->length;
}

ClassType::ClassType( const char *myVar )
{
    cPointer = new char[ strlen( myVar ) + 1 ]  //+1 for trailing '\0'
    strcpy( cPointer, myVar );
    length = strlen( cPointer );
}

char ClassType::otherFunc()
{
    cPointer;  // Nothing is shown when debugging..
    cPointer = "MyPointer"; // Results in  acrash
    length = 5; // Results in a crash
}

// The main function is working properly.
+2  A: 

I can't think of why it would crash where you indicate, but there are several problems with your code (some of which are compile-time problems, so we can't be sure this code accurately reflects the problem):

  • there are ownership problems - when ClassType::ClassType( const ClassType* ) is called, which instance of ClassType owns the object pointed to by cPointer?
  • there's no dtor to release the memory allocated in `ClassType::ClassType( const char *myVar )'
  • since cPointer may point to something allocated by new or might not, you'll have issues trying to determine when the thing allocated by new should be deleted.

As far as the compile time errors go:

  • the definition of ClassType( const ClassType* ) should start with ClassType::ClassType( const ClassType* )
  • the contents of ClassType::ClassType( const ClassType* ) should be using a parameter instead of the ClassType class name as the pointer
  • char ClassType::otherFunc() needs a return statement
Michael Burr
Nobody clearly pointed that "#include ClassType.h" is wrong too...
Klaim
+4  A: 
  1. This isn't valid C++ code.
  2. If you are using C++, shouldn't you use std::string for the string?
  3. Constructor based on another instance should be ClassType(const ClassType& rhs)
chrish
I agree completely. This shouldn't compile or run.
Lyndsey Ferguson
+2  A: 

Is this the real code?

ClassType( const ClassType* )
{
    cPointer = ClassType->cPointer;
    length = ClassType->length;
}

If so, it needs to be like this:

ClassType( const ClassType* rhs )
{
    cPointer = rhs->cPointer;
    length = rhs->length;
}

Also, this constructor is not the default ctor:

ClassType( const ClassType* ); // default constr. needed when allocating  in main.

A default ctor is specifically a ctor that either takes zero parameters or all the parameters have defaults specified. In other words, a default ctor is a ctor that can be called like this:

ClassType myObject;
John Dibling
A: 

Default constructors are those for which all the arguments have default values, so your constructor that takes a pointer is not a default constructor.

Your crash locations indicate the class has not been constructed properly, so you're probably getting address errors when you assign to them.

Could you post main, as that is probably key to see the problem?

justinhj
+1  A: 

I provided a pretty complete answer in your other question about this code. I believe that the main issue is that your copy constructor is massively broken. It will cause double free errors and other badness. Also since your destructor calls delete on the pointers you allocate, you can't ever assign string literals to your classes pointers.

Evan Teran