Class Constructor Help

Hi all. I've made 2 classes, one that opens an ifstream and reads data, and the other that opens an ofstream and writes data.

The third class I am working on will control the directions of the two classes. It has been tricky to get to work, and it makes me feel like I've done something wrong. Anyway, here is the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Decompress
{
private:
	InFile *inFile;
	OutFile *outFile;
		
public:
	Decompress(char* inName, unsigned char inSize, char * outName, unsigned char outSize)
	{
		inFile = new InFile(inName, inSize);
		outFile = new OutFile(outName, outSize);
	}
	~Decompress(){
		delete [] inFile;
		delete [] outFile;	
	};
	void test(){
		char* name = inFile[0].getName();
		for (unsigned char ch =0; ch < inFile[0].getNameSize(); ch++){
			cout << *(name + ch);
		}

	}
};

This seems to have problems of a different nature. At this point I must have done something completely wrong.

The point is that it has been hard as heck to get this class to initialize with creating a class InFile and OutFile. This is the best I could do, but now my inFile is an array (as seen in the test function).

I'm so confused.... How does that deconstructer look? My program is triggering a break point, and I'm pretty sure it has to do with the ones that I have set up.
Last edited on
Well for starters, your inFile isn't an array. And you are deleting improperly.

You are using singular new, which creates one InFile object.

You are then using array delete (delete[]) which is wrong. You must only use array delete with array new... and you are not using array new.

If you don't need an array here, the easiest way to do this would be to simply not use pointers. Don't use pointers unless you have to -- they just complicate things:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Decompress
{
private:
	InFile inFile;  // <- not a pointer
	OutFile outFile; // <- not a pointer
		
public:
	Decompress(char* inName, unsigned char inSize, char * outName, unsigned char outSize)
	    : inFile(inName, inSize)  // construct the objects in your initializer list
	    , outFile(outName, outSize)
	{
		// no need to new anything
	}
	~Decompress()
	{
		// no need to delete anything
	}
	//... 



Secondly, your test function is insane. Isn't the name null terminated? Why don't you just do this?

 
cout << inFile.getName();


Why print each character individually?



Other than that I don't see anything wrong with this class.

The mismatching new->delete[] issue might have been what's causing your problem.
Last edited on
Thanks, looks like things are working correctly. Can you tell me (or perhaps give a link) about the difference between the initializer list and what happens within the brackets?

As for the file name, I had no idea I could do that with a character array. Thanks again.
It's best to use initialization lists for a couple of reasons. One is that you can't assign const values in the brackets of a constructor but you can in the initialization list.

Another is efficiency. Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Ex1
{
private:
   int x;
public:
   Ex1();
};

class Ex2
{
private:
   int x;
public:
   Ex2();
};

Ex1::Ex1():x(5) // Initialization directly occurs
{
}

Ex2::Ex2()
{
   x = 5;  // Temporary value created for assignment
}


The initialization list method allows the initialization of x to be performed directly. In Ex2, a temporary object is created to pass into the assignment operator for x, making it less efficient.

Also, if you have a constructor that has a parameter that has the same name as the field you want to assign it to, the initialization list will be able to resolve that name correctly:
1
2
3
4
5
6
7
8
9
10
class Ex3
{
private:
   int x;
public:
   Ex3(int x):x(x)  // This will work fine
   {

   }
}


In essence, that's like saying:
1
2
3
4
Ex3(int x)
{
   this->x = x;
}


Hope this helps.
Last edited on
Topic archived. No new replies allowed.