#### Archived

This topic is now archived and is closed to further replies.

# Easy problem with new (not easy for me though).

## Recommended Posts

I don''t understand at all why this won''t work. I looked in my C++ book example on new and delete, and it seems to me like I''m doing it right.
  class hPatternLine { // member variable hLine* allLines; // hLine is a predefined class // in constructor allLines = new hLine[width]; for(int i = 0; i < width; i++) allLines[i] = hLine();// bla bla initialization // in destructor ~hPatternLine() { delete[] allLines; } } 
When I run my program, it kicks me out without even starting up. The only reason I can think of this happening is that I first instantiate the object that holds the hLine pointer in a header file like this: hPatternLine hPatternLineObject; Then in an initialization function I call the constructor with arguments like this: hPatternLineObject = hPatternLine(arg1, arg2, etc); What is the compiler doing here? Is it actually creating a hPatternLine object, then calling the destructor, then recreating it with the arguments? If this is the case, would my solution be to have a member like bool createdWithArguments and only set that to true when the argument constructor is called? That way, I could test against it when I call the destructor. I''m not too sure. Thanks for any help. --Vic--

##### Share on other sites
krez    443
don;t quote me, but i believe when you use "new" it calls the constructors for all those elements in the array... also, "delete[]" calls all their destructors. so, i don;t think you need the for loop there.
i don''t know if that is your problem though.

--- krez (krezisback@aol.com)

##### Share on other sites
Well, on the for part where I make each allInes a new hLine, there are arguments there. I just left them out cuz they''re not important to what I''m asking.

--Vic--

##### Share on other sites
alexk7    138
You should not instantiate an object in an header file because if you include the file in multiple source files, you have many different objects with the same name.
hPatternLine hPatternLineObject;hPatternLineObject = hPatternLine(arg1, arg2, etc);

This code...

1. Create hPatternLineObject with the default constructor

2. Create a temporary object with the 3 args constructor

3. Use the operator= to copy the temporary into hPatternLineObject

4. Destroy the temporary object

I suppose you didn''t overload the operator=, so your first hLines pointer is lost and replace by the new one. This means that the first batch of hLines is never destroyed... or maybe you do something like allLines = new hLine[0] in the case of the default constructor ?...
By the way, if you want to delay the construction, use another pointer (and write hPatternLineObject = new hPatternLine(arg1, arg2, etc)) or simply create the object just when you need it.

##### Share on other sites
alexk7    138
If you really want this kind of "create and after initialize", use a "Init" or "Create" function instead of using "= constructor(blah blah...)" because it creates a lot of temporary objects and creates many difficult to find bugs.