Need Clarification

I want to make sure I understand some C++ syntax. I've read definitions and looked at many examples but still don't completely understand what is going on.

Please note that I've skipped a great deal of code to simplify my question. This code will not compile or run for obvious reasons - and that is not necessary for the purposes of my question.

1
2
3
4
5
6
7
8
9
10
// file: Store.cpp
#include "Store.h"
#include "House.h"

void Store::analyze()
{
    var = House::pointer(new House(var_1, var_2, var_3));

    HouseContainer.push_back(var);
}


Where
1
2
3
4
5
6
7
8
// file: House.h

class House
{
public:
    House(const Config& cfg, CountsVec& cv, CountsMap& cm);
    typedef House* pointer;
}


And
1
2
3
4
5
6
7
8
9
10
11
// file: Store.h
#include "House.h"

class Store
{
public:
    void analyze();
    boost::ptr_vector<House> HouseContainer;
private:
    House::pointer var;
}


I don't know if what I'm doing will work (or even makes sense) since I don't fully understand what var = House::pointer(new House(var_1, var_2, var_3)); means.

My weak attempt at explaining what is happening:
var is a pointer of type House.
We are setting var equal to a new pointer of an object of type House using the constructor defined in House.h.

Am I even close to correct? If so, what does that mean? If not, what did I miss? Can someone help me here?
We are setting var equal to a new pointer of an object of type House using the constructor defined in House.h.


You are constructing a house object using operator new which returns a pointer to the object that is allocated on the heap. A pointer type is more of an integral type since it contains a memory address. The constructor in house.h is for constructing house objects but not pointers to house objects. So it looks like you are a) constructing a house object and then using the pointer returned to construct another pointer which is then assigned to the var. This is unnecessary. I believe that you can do a direct assignment which will simplify the syntax. var already is a House* so the return value of operator new can be directly assigned.

var = new House(var_1, var_2, var_3); // should be able to do a direct assignment
Ok, I think I understand a bit more. If var were defined as a std::pointer then my statement would have been necessary, right? Since I declared var as a House* I'm using an unnecessary wrapper.

Now, suppose my program is causing a core dump upon completion. Does it look like what I've got here would be the culprit? Does this code look like it's not cleaning itself up correctly? I suspect that I'm trying to delete something that has already been deleted...but I can't be sure since I'm still a little fuzzy on what I'm doing. :(
I don't see anything there that would cause a crash. ptr_vector deletes the objects automatically so the point of it is that you don't have to manually delete the objects. If you are then that is a problem. Also I have found this statement on the website for boost pointer containers: "When you do need shared semantics, this library is not what you need.". We'd have to see more code. Perhaps you could try duplicating the problem in a smaller example and then post that example.

Sorry, never heard of a std::pointer. I don't know what that is. Have you read the boost documentation completely?
http://www.boost.org/doc/libs/1_44_0/libs/ptr_container/doc/ptr_container.html
Topic archived. No new replies allowed.