Style question, structs and objects

I

Inso Reiges

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.
 
J

Juha Nieminen

Inso said:
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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
I

Inso Reiges

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.
 
D

Devon Null

Inso said:
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

classes (C++) vs. structs(C): multithreading on graph objects (e.g. DFS) 3
Style Tag Problem 1
objects and trolls 2
C++ objects and flex 3
packed structs 35
structs 29
structs 3
structs 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top