Re: Initialize static members with objects
Johannes Bauer wrote:
Hello group,
I'm changing some code from C-style to STL-C++. In the old code,
something along the lines of this is used:
struct foo {
int x, y;
};
class moo {
private:
const foo[] foos;
That's not a valid C++ declaration (static or no static).
};
and then in a cpp-file:
const foo[] moo::foos = {
That's not a valid C++ declaration.
{ 12, 14 },
{ 99, 3 },
{ 1, 2 },
{ 0 },
};
Now I'd like to change that to
class moo {
private:
const std::set<foo> foos;
};
and init that like the above C-style declaration. Is that possible? If
so, how?
I don't think you can init a set. You can, however, init something else
and make it fill the set, like so:
const std::set<foo> moo::foos;
int foo_filler(std::set<foo>& set_to_fill)
{
foo foos[] = { { 12, 14 }, { 99, 3 }, { 1, 2 }, { 0 } };
set_to_fill.insert(foos[0]);
set_to_fill.insert(foos[1]);
set_to_fill.insert(foos[2]);
set_to_fill.insert(foos[3]);
}
int dummy = foo_filler(moo::foos);
Now, your 'foo' struct does not satisfy the requirement to be
Less-Than-Comparable. It has to have operator< defined for it to be the
element of a std::set. Or you need to provide a predicate for sorting
those structs. Read your favourite C++ book on standard containers.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask