Declaration and initialisation

Discussion in 'C++' started by Simon Elliott, Sep 4, 2004.

  1. Let's say I have a small class:

    class Noddy
    {
    private:
    std::string s1_;
    std::string s2_;
    public:
    Noddy();
    };

    Now let's say I want s1_ and s2_ to be initialised:

    Noddy::Noddy():s1_("string1"),s2_("string2"){}

    This is fine for a simple example, but if you have hundreds of data
    items to construct, and if they are complex objects which need several
    arguments, the whole thing starts to get a bit messy. Someone adds
    s237a to the class, and they have to remember to add
    s237a("string237a") to the list. And if they forget, the code still
    compiles.

    I'd quite like to be able to have all the data members declared and
    initialised in one place. Sort of the equivalent of:

    class Noddy
    {
    private:
    std::string s1_("string1");
    std::string s2_("string2");
    public:
    Noddy();
    };

    Any ideas?

    --
    Simon Elliott http://www.ctsn.co.uk
    Simon Elliott, Sep 4, 2004
    #1
    1. Advertising

  2. Simon Elliott

    Rolf Magnus Guest

    "Simon Elliott" <Simon at ctsn.co.uk> wrote:

    > Let's say I have a small class:
    >
    > class Noddy
    > {
    > private:
    > std::string s1_;
    > std::string s2_;
    > public:
    > Noddy();
    > };
    >
    > Now let's say I want s1_ and s2_ to be initialised:
    >
    > Noddy::Noddy():s1_("string1"),s2_("string2"){}
    >
    > This is fine for a simple example, but if you have hundreds of data
    > items to construct,


    Then your class is _way_ too big. Split it up into smaller classes.

    > and if they are complex objects which need several
    > arguments, the whole thing starts to get a bit messy. Someone adds
    > s237a to the class, and they have to remember to add
    > s237a("string237a") to the list. And if they forget, the code still
    > compiles.
    >
    > I'd quite like to be able to have all the data members declared and
    > initialised in one place. Sort of the equivalent of:
    >
    > class Noddy
    > {
    > private:
    > std::string s1_("string1");
    > std::string s2_("string2");
    > public:
    > Noddy();
    > };
    >
    > Any ideas?


    That's not possible in C++.
    Rolf Magnus, Sep 4, 2004
    #2
    1. Advertising

  3. Simon Elliott

    Phlip Guest

    Simon Elliott wrote:

    > Let's say I have a small class:
    >
    > class Noddy
    > {
    > private:
    > std::string s1_;
    > std::string s2_;
    > public:
    > Noddy();
    > };
    >
    > Now let's say I want s1_ and s2_ to be initialised:
    >
    > Noddy::Noddy():s1_("string1"),s2_("string2"){}
    >
    > This is fine for a simple example, but if you have hundreds of data
    > items to construct, and if they are complex objects which need several
    > arguments, the whole thing starts to get a bit messy.


    Why do you need hundreds of data elements. Their redundance typically
    indicates you have overlooked the possibility of a better abstraction.

    > Someone adds
    > s237a to the class, and they have to remember to add
    > s237a("string237a") to the list. And if they forget, the code still
    > compiles.


    Here's the Variable State Pattern in C++:

    typedef map<string, string> variables_t;
    variables_t variables;
    variables["s237"] = "string237";
    variables["s238"] = "string238";
    variables["s239"] = "string239";
    ....
    variables["s999"] = "string999";

    That provides more flexibility. You can store the strings in a file, iterate
    thru the map and affect each one, and generally distribute the effort of
    initializing them. No syntactic sugar will make that easy.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Sep 4, 2004
    #3
  4. Simon Elliott

    Phlip Guest

    Rolf Magnus wrote:

    > Simon Elliott wrote:


    > > I'd quite like to be able to have all the data members declared and
    > > initialised in one place. Sort of the equivalent of:
    > >
    > > class Noddy
    > > {
    > > private:
    > > std::string s1_("string1");
    > > std::string s2_("string2");
    > > public:
    > > Noddy();
    > > };
    > >
    > > Any ideas?

    >
    > That's not possible in C++.


    That's why he said "equivalent", Rolf.

    And breaking up the class would just move its thousands of members into
    hundreds of classes, distributing the problem instead of reducing it.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Sep 4, 2004
    #4
  5. On 04/09/2004, Phlip wrote:

    > Why do you need hundreds of data elements. Their redundance typically
    > indicates you have overlooked the possibility of a better abstraction.


    See below.

    > > Someone adds
    > > s237a to the class, and they have to remember to add
    > > s237a("string237a") to the list. And if they forget, the code still
    > > compiles.

    >
    > Here's the Variable State Pattern in C++:
    >
    > typedef map<string, string> variables_t;
    > variables_t variables;
    > variables["s237"] = "string237";
    > variables["s238"] = "string238";
    > variables["s239"] = "string239";
    > ...
    > variables["s999"] = "string999";
    >
    > That provides more flexibility. You can store the strings in a file,
    > iterate thru the map and affect each one, and generally distribute
    > the effort of initializing them. No syntactic sugar will make that
    > easy.


    That would solve the problem as described.

    Unfortunately in order to get a clear example it seems that I've
    oversimplified the problem.

    The class I'm designing is, in part, a parameter manager which fits in
    to a large distributed system. The large distributed system is a given
    and I have no control over it.

    The parameters are of different types, from bools to lists of complex
    objects.

    The parameter manager will be required to do a number of things:
    1/ Accept a parameter by name
    2/ Accept a parameter by number
    3/ Save a selection of its parameters to nvram
    4/ Give a list of all its parameter names
    .... etc

    The parameter manager will also be a base class for extended parameter
    managers which will handle extra parameters over and above those
    available in the base class.

    I'm not sure how I'd implement this without having a list (or series of
    lists) of the parameter names, types, numbers, values and other
    properties, all of which has to be initialised somewhere.

    --
    Simon Elliott http://www.ctsn.co.uk
    Simon Elliott, Sep 4, 2004
    #5
  6. Simon Elliott

    David Hilsee Guest

    "Simon Elliott" <Simon at ctsn.co.uk> wrote in message
    news:4139e0ee$0$6335$...
    <snip>
    > That would solve the problem as described.
    >
    > Unfortunately in order to get a clear example it seems that I've
    > oversimplified the problem.
    >
    > The class I'm designing is, in part, a parameter manager which fits in
    > to a large distributed system. The large distributed system is a given
    > and I have no control over it.
    >
    > The parameters are of different types, from bools to lists of complex
    > objects.
    >
    > The parameter manager will be required to do a number of things:
    > 1/ Accept a parameter by name
    > 2/ Accept a parameter by number
    > 3/ Save a selection of its parameters to nvram
    > 4/ Give a list of all its parameter names
    > ... etc
    >
    > The parameter manager will also be a base class for extended parameter
    > managers which will handle extra parameters over and above those
    > available in the base class.
    >
    > I'm not sure how I'd implement this without having a list (or series of
    > lists) of the parameter names, types, numbers, values and other
    > properties, all of which has to be initialised somewhere.


    Well, it does sound like at least one container is needed. The number and
    name requirement is a little odd, but you could implement that using a
    std::vector and a std::map. If you need to store multiple types, then you
    may want to use something like boost::any, so you can easily store different
    unrelated types inside a single container.

    --
    David Hilsee
    David Hilsee, Sep 4, 2004
    #6
  7. Simon Elliott

    Rich Grise Guest

    Phlip wrote:

    > Rolf Magnus wrote:
    >
    >> Simon Elliott wrote:

    >
    >> > I'd quite like to be able to have all the data members declared and
    >> > initialised in one place. Sort of the equivalent of:
    >> >
    >> > class Noddy
    >> > {
    >> > private:
    >> > std::string s1_("string1");
    >> > std::string s2_("string2");
    >> > public:
    >> > Noddy();
    >> > };
    >> >
    >> > Any ideas?

    >>
    >> That's not possible in C++.

    >
    > That's why he said "equivalent", Rolf.
    >
    > And breaking up the class would just move its thousands of members into
    > hundreds of classes, distributing the problem instead of reducing it.
    >


    What about in the constructor? I'm a C++ n00b, albeit not a programming
    noob, so if this is out of line, or merely ludicrous, please chastise me
    suitably. :)

    Cheers!
    Rich
    Rich Grise, Sep 5, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    945
  2. Alex Vinokur
    Replies:
    4
    Views:
    571
    Jonathan Turkanis
    Apr 5, 2004
  3. kelvSYC
    Replies:
    6
    Views:
    7,208
    Richard Herring
    May 17, 2005
  4. James Brodie

    Variable declaration and initialisation

    James Brodie, Jan 15, 2006, in forum: C Programming
    Replies:
    22
    Views:
    1,094
    Emmanuel Delahaye
    Jan 21, 2006
  5. Tim Clacy
    Replies:
    8
    Views:
    382
    Tim Clacy
    May 30, 2006
Loading...

Share This Page