Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: If there are many member variables in a class, how can I initializethem fast in the constructor? (http://www.velocityreviews.com/forums/t743693-re-if-there-are-many-member-variables-in-a-class-how-can-i-initializethem-fast-in-the-constructor.html)

Ian Collins 02-17-2011 03:06 AM

Re: If there are many member variables in a class, how can I initializethem fast in the constructor?
 
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 02-17-2011 03:38 AM

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<ian-news@hotmail.com> 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

Michael Doubez 02-17-2011 11:50 AM

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 <wenkai1...@gmail.com> wrote:
> On 2011-02-17, Ian Collins <ian-n...@hotmail.com> 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

Keith H Duggar 02-17-2011 01:08 PM

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 <wenkai1...@gmail.com> wrote:
> On 2011-02-17, Ian Collins <ian-n...@hotmail.com> 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

itaj sherman 02-17-2011 01:47 PM

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 <wenkai1...@gmail.com> wrote:
> On 2011-02-17, Ian Collins <ian-n...@hotmail.com> 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


All times are GMT. The time now is 05:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.