Re: vector of structs?

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Brian Muth" <bmuth@xxxxxxxx> schrieb im Newsbeitrag news:u5pIJyjaGHA.4972@xxxxxxxxxxxxxxxxxxxxxxx

"Jason S" <jmsachs@xxxxxxxxx> wrote in message
news:1146169753.282126.207280@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I've used map<T> and vector<T> and such before, where T is a simple
type, but am rather confused about the recommended way of doing it with
structures, especially ones that contain objects as a member. (Like
CComBSTR and CComPtr)

If I have

typedef struct {
short s;
MyClass obj;
CComPtr<something> p;
} X_t;

should I use vector<X_t> or vector<X_t*>? If the former, how do I
handle push_back() with a structure? If the latter, then don't I have
to allocate memory for X_t, in which case, what's the point of using
vector<>? And if I do choose to allocate memory for X_t, I can't just
use malloc/CoTaskMemAlloc(sizeof(X_t)*N), because then how will it
properly initialize the non-simple types contained in X_t like obj()
and p() which might have constructors?


If you use vector<X_t *>, then you need to add code to free the structs
before the variable goes out of scope. I tend to chose vect<X_t> for that
reason (unless the struct is VERY large).

However, you need to then add a copy constructor and assignment operator.
Brush off your C++ language book and look up those terms.

More explicitly, you need to define:

X_t::X_t(const X_t &x)

and

inside your struct, implement
void operator=(const X_t &);

Usually there is no need to implement copy-ctors and assignment operators for struct's. In most cases the compiler will create the proper ones automatically. You only need you own copy-ctors, dtors and assignment operators if your struct (or class) contains members of types, that don't have their own "smart enough" copy_ctors etc, like raw pointers or HANDLEs. In such situations, however, it is better to use members with proper copy-ctors etc, instead of implementing them for the struct:

// raw pointers
struct Pointers
{
char* firstName;
char* lastName;
Pointers(): firstName(0), lastName(0) {}
Pointers(Pointers const& x)
{
firstName = new char[strlen(x.firstName) + 1]; strcpy(firstName, x.firstName);
lastName = new char[strlen(x.lastName) + 1]; strcpy(lastName, x.lastName);
}
~Pointers() { delete[] firstName; delete[] lastName; }
Pointers& operator=(Pointers const& x)
{
Pointers t(x);
std::swap(*this, t);
return *this;
}
};

// strings
struct Strings
{
std::string firstName;
std::string lastName;
};

It should be obvious which code is easier to write. And consider what you have to do (and what you can forget to do) when you add a middleName member to those struct's...

HTH
Heinz
.



Relevant Pages

  • Re: realloc() implicit free() ?
    ... >> "The value is that of the named member of the object to which the first ... whole object the effective struct type "in the background". ... > different types that were not in a union. ... > the accesses were being made through struct pointers rather than ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... >>> memory has no effective type, because it hasn't been stored into yet. ... Writing into one struct member causes the whole struct to be accessed ... >>> pointers to structs being the same as pointers to their first ...
    (comp.lang.c)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • Re: porting problems encountered
    ... Tru64 compiles long variables to size 8 bytes while VMS and HP-UX ... platform, the size of the structure would be 12 bytes. ... when it got to the AS/400 with 128 bit pointers. ... Using the struct from before: ...
    (comp.os.vms)
  • Re: strict aliasing rules in ISO C, someone understands them ?
    ... I understand now that my interpretation of the standard was totally ... an aggregate is a struct or an array. ... struct s1 {int i; double d;}; ... Pointers are just a means of accessing the objects, ...
    (comp.lang.c)