Style question, structs and objects

Discussion in 'C++' started by Inso Reiges, Jun 6, 2007.

  1. Inso Reiges

    Inso Reiges Guest

    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, Jun 6, 2007
    #1
    1. Advertising

  2. 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
     
    Juha Nieminen, Jun 6, 2007
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 6, 2007
    #3
  4. Inso Reiges

    Inso Reiges Guest

    On Jun 6, 9:04 pm, Erik Wikström <> 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, Jun 6, 2007
    #4
  5. Inso Reiges

    Devon Null Guest

    Inso Reiges wrote:
    > On Jun 6, 9:04 pm, Erik Wikström <> 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.
     
    Devon Null, Jun 6, 2007
    #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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    651
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    565
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    651
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,424
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    496
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page