From: DeMarcus on
Hi,

I have an object:

class SomeObject
{
public:
SomeObject();

SomeObject& setValue( int i );
SomeObject& increaseRate( int percent );
... etc ...
};

This is used in the following manner

startCalculation( SomeObject().setValue( 47 ).increaseRate( 20 ) );


Now, I want to provide a small wrapper ensuring correct initialization.
My question is; shall I use derivation or a function? I.e.

struct PreparedObject : SomeObject
{
inline PreparedObject( int i, int percent ) : SomeObject()
{
(*this).setValue( i ).increaseRate( percent );
}
}

or shall I use a function:

inline SomeObject preparedObject( int i, int percent )
{
return SomeObject().setValue( i ).increaseRate( percent );
}


Later those would be used like this

startCalculation( PreparedObject( 47, 20 ) );


Both derivation and using a function work, but what are the pros and
cons? What is most elegant, efficient, safe, etc.?


Thanks,
Daniel

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Hakusa on
On Jun 15, 5:15 am, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:

> class SomeObject
> {
> public:
> SomeObject();
>
> SomeObject& setValue( int i );
> SomeObject& increaseRate( int percent );
> ... etc ...
>
> };
>
> This is used in the following manner
>
> startCalculation( SomeObject().setValue( 47 ).increaseRate( 20 ) );
>
> Now, I want to provide a small wrapper ensuring correct initialization.
> My question is; shall I use derivation or a function? I.e.

Neither, use a constructor.

class SomeObject
{
public:
SomeObject( int i, int percent );
//...
};

Your constructor should be able to ensure correct initialization on
its own.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 15 Jun., 11:15, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:
[..]
> I have an object:
>
> class SomeObject
> {
> public:
> SomeObject();
>
> SomeObject& setValue( int i );
> SomeObject& increaseRate( int percent );
> ... etc ...
> };
>
> This is used in the following manner
>
> startCalculation( SomeObject().setValue( 47 ).increaseRate( 20 ) );
>
> Now, I want to provide a small wrapper ensuring correct initialization.
> My question is; shall I use derivation or a function? I.e.
>
> struct PreparedObject : SomeObject
> {
> inline PreparedObject( int i, int percent ) : SomeObject()
> {
> (*this).setValue( i ).increaseRate( percent );
> }
> }
>
> or shall I use a function:
>
> inline SomeObject preparedObject( int i, int percent )
> {
> return SomeObject().setValue( i ).increaseRate( percent );
> }
>
> Later those would be used like this
>
> startCalculation( PreparedObject( 47, 20 ) );
>
> Both derivation and using a function work, but what are the pros and
> cons? What is most elegant, efficient, safe, etc.?

I don't see the need (and usefulness) of a derived class.
This increases the set of types without real need for
them. I wonder why you do not properly initialize
all SomeObject objects by one of the following
(independent) approaches:

a) Defining the default c'tor of SomeObject such that it does so:

class SomeObject
{
public:
SomeObject() { setValue(0); } // or other meaningful value
...
};

b) Providing a constructor that accepts an initial value:

class SomeObject
{
public:
explicit SomeObject(int value) { setValue(value); }
...
};

If you are not allowed to change the definition of
SomeObject, it seems to me, that the free-function
approach is the best you can do instead.

HTH & Greetings from Bremen,

Daniel Kr�gler



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 15 June, 10:15, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:
> Hi,
>
> I have an object:
>
> class SomeObject
> {
> public:
> SomeObject();
>
> SomeObject& setValue( int i );
> SomeObject& increaseRate( int percent );
> ... etc ...
>
> };
>
> This is used in the following manner
>
> startCalculation( SomeObject().setValue( 47 ).increaseRate( 20 ) );
>
> Now, I want to provide a small wrapper ensuring correct initialization.
> My question is; shall I use derivation or a function? I.e.
>
> struct PreparedObject : SomeObject
> {
> inline PreparedObject( int i, int percent ) : SomeObject()
> {
> (*this).setValue( i ).increaseRate( percent );
> }
>
> }
>
> or shall I use a function:
>
> inline SomeObject preparedObject( int i, int percent )
> {
> return SomeObject().setValue( i ).increaseRate( percent );
>
> }
>
> Later those would be used like this
>
> startCalculation( PreparedObject( 47, 20 ) );
>
> Both derivation and using a function work, but what are the pros and
> cons? What is most elegant, efficient, safe, etc.?

I'm not sure I'd want to do either.
Surely the only reason that you haven't already got
SomeObject::SomeObject(int value,int rate) is because you thought that
it was clearer to explicitly state what each parameter was about. You
now seem to want to reverse that design decision.




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Jun 15, 11:15 am, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote:
> Hi,
>
> I have an object:
>
> class SomeObject
> {
> public:
> SomeObject();
>
> SomeObject& setValue( int i );
> SomeObject& increaseRate( int percent );
> ... etc ...
>
> };
>
> This is used in the following manner
>
> startCalculation( SomeObject().setValue( 47 ).increaseRate( 20 ) );

I've seen code like this (even wrote some in my time), and I've come
to despise it. The major problem I see with this is that your
modifiers are misleading in form. What they buy is the ability to call
multiple methods on an object in one statement. Uh-oh, big deal.

> Now, I want to provide a small wrapper ensuring correct initialization.

Erm... That's what constructor is for?

On a more general note, it looks like you are using two-step
initialization. In my opinion, there has to be a big fat explanation
for every use. So what's your excuse? ;-)

> My question is; shall I use derivation or a function? I.e.
>
> struct PreparedObject : SomeObject
> {
> inline PreparedObject( int i, int percent ) : SomeObject()
> {
> (*this).setValue( i ).increaseRate( percent );
> }
>
> }
>
> or shall I use a function:
>
> inline SomeObject preparedObject( int i, int percent )
> {
> return SomeObject().setValue( i ).increaseRate( percent );
>
> }
>
> Later those would be used like this
>
> startCalculation( PreparedObject( 47, 20 ) );

If this is __really__ the only way you are happy with :-), I'd say
that a function is better, simply because it's less code artifacts.
(Derivation meant a class and a trivial ctor. That's incidental
complexity, not much more)

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]