Re: If there are many member variables in a class, how can I initializethem fast in the constructor?

Discussion in 'C++' started by Ian Collins, Feb 17, 2011.

  1. Ian Collins

    Ian Collins Guest

    On 02/17/11 04:02 PM, KaiWen wrote:
    > eg:
    > class SomeThing {
    > public:
    > SomeThing();
    > private:
    > a
    > b
    > c
    > ...
    > ...
    > // a lot of variables
    > };
    >
    > If this is not a derived class(there is no vptr), can I use
    > memset in constructor?
    > SomeThing::SomeThing() {memset(this, 0, sizeof(SomeThing));}
    >
    > And if it is not a derived class, how can I initialize it fast?


    Use an initialiser list.

    --
    Ian Collins
     
    Ian Collins, Feb 17, 2011
    #1
    1. Advertising

  2. Ian Collins

    Ian Collins Guest

    Re: If there are many member variables in a class, how can I initializethem fast in the constructor?

    On 02/17/11 04:21 PM, KaiWen wrote:
    > On 2011-02-17, Ian Collins<> wrote:
    >> On 02/17/11 04:02 PM, KaiWen wrote:
    >>> eg:
    >>> class SomeThing {
    >>> public:
    >>> SomeThing();
    >>> private:
    >>> a
    >>> b
    >>> c
    >>> ...
    >>> ...
    >>> // a lot of variables
    >>> };
    >>>
    >>> If this is not a derived class(there is no vptr), can I use
    >>> memset in constructor?
    >>> SomeThing::SomeThing() {memset(this, 0, sizeof(SomeThing));}
    >>>
    >>> And if it is not a derived class, how can I initialize it fast?

    >>
    >> Use an initialiser list.
    >>

    >
    > Are you mean this:
    > SomeThing::SomeThing(Type1 aa, Type2 bb, Type3 cc, Type4 dd /* more and more */)
    > : a(aa), b(bb), c(cc), d(dd) /* more and more */
    >
    > If there are a lot of variables, write these codes are very cumbersome.


    Than your class is probably doing too much!

    --
    Ian Collins
     
    Ian Collins, Feb 17, 2011
    #2
    1. Advertising

  3. Re: If there are many member variables in a class, how can Iinitialize them fast in the constructor?

    On 17 fév, 04:21, KaiWen <> wrote:
    > On 2011-02-17, Ian Collins <> wrote:
    >
    >
    >
    > > On 02/17/11 04:02 PM, KaiWen wrote:
    > >> eg:
    > >> class SomeThing {
    > >> public:
    > >>        SomeThing();
    > >> private:
    > >>        a
    > >>        b
    > >>        c
    > >>        ...
    > >>        ...
    > >>        // a lot of variables
    > >> };

    >
    > >> If this is not a derived class(there is no vptr), can I use
    > >> memset in constructor?
    > >> SomeThing::SomeThing() {memset(this, 0, sizeof(SomeThing));}

    >
    > >> And if it is not a derived class, how can I initialize it fast?

    >
    > > Use an initialiser list.

    >
    > Are you mean this:
    > SomeThing::SomeThing(Type1 aa, Type2 bb, Type3 cc, Type4 dd /* more and more */)
    >         : a(aa), b(bb), c(cc), d(dd) /* more and more */
    >
    > If there are a lot of variables, write these codes are very cumbersome.


    Then you can use a nested class:

    class SomeThing {
    public:
    SomeThing(): data(){}
    private:
    struct Data{
    a
    b
    c
    ...
    ...
    // a lot of variables
    };
    Data data;
    };

    This way you have only one element in the initialiser list.

    --
    Michael
     
    Michael Doubez, Feb 17, 2011
    #3
  4. Re: If there are many member variables in a class, how can Iinitialize them fast in the constructor?

    On Feb 16, 10:21 pm, KaiWen <> wrote:
    > On 2011-02-17, Ian Collins <> wrote:
    > > On 02/17/11 04:02 PM, KaiWen wrote:
    > >> eg:
    > >> class SomeThing {
    > >> public:
    > >>        SomeThing();
    > >> private:
    > >>        a
    > >>        b
    > >>        c
    > >>        ...
    > >>        ...
    > >>        // a lot of variables
    > >> };

    >
    > >> If this is not a derived class(there is no vptr), can I use
    > >> memset in constructor?
    > >> SomeThing::SomeThing() {memset(this, 0, sizeof(SomeThing));}

    >
    > >> And if it is not a derived class, how can I initialize it fast?

    >
    > > Use an initialiser list.

    >
    > Are you mean this:
    > SomeThing::SomeThing(Type1 aa, Type2 bb, Type3 cc, Type4 dd /* more and more */)
    >         : a(aa), b(bb), c(cc), d(dd) /* more and more */
    >
    > If there are a lot of variables, write these codes are very cumbersome.


    Then one of the following applies:

    1) you need to learn and use a "real" code editor

    2) you need to write a code generator

    3) you need to refactor the data into data based indexed
    container (vector, map, etc)

    4) your code sucks and should be rewritten and possibly
    you need to be educated and trained again (or for the
    first time)

    KHD
     
    Keith H Duggar, Feb 17, 2011
    #4
  5. Ian Collins

    itaj sherman Guest

    Re: If there are many member variables in a class, how can Iinitialize them fast in the constructor?

    On Feb 17, 5:21 am, KaiWen <> wrote:
    > On 2011-02-17, Ian Collins <> wrote:
    >
    >


    >
    > >> And if it is not a derived class, how can I initialize it fast?

    >


    What do you mean by "fast"?
    Performance or shorter code?

    > > Use an initialiser list.

    >
    > Are you mean this:
    > SomeThing::SomeThing(Type1 aa, Type2 bb, Type3 cc, Type4 dd /* more and more */)
    > : a(aa), b(bb), c(cc), d(dd) /* more and more */
    >
    > If there are a lot of variables, write these codes are very cumbersome.
    >


    You mean your problem is with the amount of code you have to write?

    There are a million things more important than that.
    As others mentioned, how come your class has so many members? That
    suggests you may have a problem with the design.

    Finally, if you have to, you might use the following:
    1) In initializer list, any data member or bases class that you don't
    mention explicitly, must have its default constructor called
    implicitly.
    2) Change the types of all your members to ones that have a default
    constructor that does what you want.

    itaj
     
    itaj sherman, Feb 17, 2011
    #5
    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. E11
    Replies:
    1
    Views:
    4,955
    Thomas Weidenfeller
    Oct 12, 2005
  2. Siemel Naran
    Replies:
    4
    Views:
    842
    Micah Cowan
    Jan 12, 2005
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,353
  4. Replies:
    9
    Views:
    1,015
  5. Goran
    Replies:
    3
    Views:
    346
    Bo Persson
    Feb 18, 2011
Loading...

Share This Page