Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Style question, structs and objects

Reply
Thread Tools

Style question, structs and objects

 
 
Inso Reiges
Guest
Posts: n/a
 
      06-06-2007
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.

 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      06-06-2007
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-lit...html#faq-10.18
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      06-06-2007
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 Wikström
 
Reply With Quote
 
Inso Reiges
Guest
Posts: n/a
 
      06-06-2007
On Jun 6, 9:04 pm, Erik Wikström <(E-Mail Removed)> 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.

 
Reply With Quote
 
Devon Null
Guest
Posts: n/a
 
      06-06-2007
Inso Reiges wrote:
> On Jun 6, 9:04 pm, Erik Wikström <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments