|
|
 |
 |
 |
 |
Style question, structs and objects
Hi, I`m new to C++ and OOP in general and i come from long-time C mindset. So i have some style questions. I have a class that needs to be initialized by over nine parameters. So i decided to put this mess into some kind of an object (here by object i don`t mean a class instance) and pass it to constructor. In C such thing would usually be done by filling out a struct. Should i use an object (class instance) in C++ or is it ok to use structs? Thanks in advance.
Inso Reiges wrote: > I have a class that needs to be initialized by over nine parameters. > So i decided to put this mess into some kind of an object (here by > object i don`t mean a class instance) and pass it to constructor.
Aren't you simply transferring the initialization "mess" from one module to another? This is one possible technique you could perhaps use if you find yourself having to set tons of variables to initialize a class: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18
On 2007-06-06 15:48, Inso Reiges wrote: > Hi, > I`m new to C++ and OOP in general and i come from long-time C mindset. > So i have some style questions. > I have a class that needs to be initialized by over nine parameters. > So i decided to put this mess into some kind of an object (here by > object i don`t mean a class instance) and pass it to constructor. In C > such thing would usually be done by filling out a struct. Should i use > an object (class instance) in C++ or is it ok to use structs?
Since the difference between a struct and a class is minimal in C++ it does nor really matter, you can easily make them both behave the same way. But since it's a matter of style take a look at the following and tell me why a struct/class would be better than passing them all normally: ParameterObject param; param.p1 = "Hello"; param.p2 = "World"; param.p3 = 3.1415; param.p4 = 123456789; Object obk(param); Personally I think this is better (since it is a little bit less to type): Object obj( "Hello", "World", 3.1215 123456789 ); Notice that there are a number of ways to format the above piece of code, and more or less all of them are ugly. -- Erik Wikstrm
On Jun 6, 9:04 pm, Erik Wikstrm <Erik-wikst@telia.com> wrote:
> On 2007-06-06 15:48, Inso Reiges wrote: > > Hi, > > I`m new to C++ and OOP in general and i come from long-time C mindset. > > So i have some style questions. > > I have a class that needs to be initialized by over nine parameters. > > So i decided to put this mess into some kind of an object (here by > > object i don`t mean a class instance) and pass it to constructor. In C > > such thing would usually be done by filling out a struct. Should i use > > an object (class instance) in C++ or is it ok to use structs? > Since the difference between a struct and a class is minimal in C++ it > does nor really matter, you can easily make them both behave the same > way. But since it's a matter of style take a look at the following and > tell me why a struct/class would be better than passing them all normally:
<snip> Thanks for the answer. The reason i want to use some kind of a construct to hold init params of the object is because i want the client to have access to this params, i.e. a get method. Thinking of it now, i think the real question i need answered is, which is the most client-oriented way to hold these init variables.
Inso Reiges wrote: > On Jun 6, 9:04 pm, Erik Wikstrm <Erik-wikst @telia.com> wrote: >> On 2007-06-06 15:48, Inso Reiges wrote: >>> Hi, >>> I`m new to C++ and OOP in general and i come from long-time C mindset. >>> So i have some style questions. >>> I have a class that needs to be initialized by over nine parameters. >>> So i decided to put this mess into some kind of an object (here by >>> object i don`t mean a class instance) and pass it to constructor. In C >>> such thing would usually be done by filling out a struct. Should i use >>> an object (class instance) in C++ or is it ok to use structs? >> Since the difference between a struct and a class is minimal in C++ it >> does nor really matter, you can easily make them both behave the same >> way. But since it's a matter of style take a look at the following and >> tell me why a struct/class would be better than passing them all normally: > <snip> > Thanks for the answer. > The reason i want to use some kind of a construct to hold init params > of the object is because i want the client to have access to this > params, i.e. a get method. > Thinking of it now, i think the real question i need answered is, > which is the most client-oriented way to hold these init variables.
My little bit of knowledge gleaned over the past few days would say an initializer list would help you. It sets defaults, but can be overridden when calling your class. Just my 2 cents. -- [there are no x's in my email] I have the right to remain silent (and should probably use it as much as possible) Anything I type can and will be used against me in a court of idiocy I have the right to be wrong (and probably am) If I can not furnish my own wrongness I'm sure someone will provide it for me.
|
 |
 |
 |
 |
|