Connecting Tech Pros Worldwide Forums | Help | Site Map

Initialization lists and passed parameters

Josh Mcfarlane
Guest
 
Posts: n/a
#1: Aug 24 '05
If the constructor for one of the objects within a class takes a
parameter of the constructor for the class and modifies it, when the
parameter is used in the constructor will it be the original parameter,
or the modified parameter after the initialization list?

Ex:

ClassA::ClassA(unsigned int& Parameter)
{
Parameter += 1;
}

ClassB::ClassB(unsigned int& ParameterB)
:ClassA(ParameterB)
{
Foo = Parameter;
}

In this case, if someone calls initializes the object as ClassB(0),
will Foo be 0 or 1?

Thanks,
Josh McFarlane


Alipha
Guest
 
Posts: n/a
#2: Aug 24 '05

re: Initialization lists and passed parameters



Josh Mcfarlane wrote:[color=blue]
> If the constructor for one of the objects within a class takes a
> parameter of the constructor for the class and modifies it, when the
> parameter is used in the constructor will it be the original parameter,
> or the modified parameter after the initialization list?
>
> Ex:
>
> ClassA::ClassA(unsigned int& Parameter)
> {
> Parameter += 1;
> }
>
> ClassB::ClassB(unsigned int& ParameterB)
> :ClassA(ParameterB)
> {
> Foo = Parameter;
> }
>
> In this case, if someone calls initializes the object as ClassB(0),
> will Foo be 0 or 1?
>
> Thanks,
> Josh McFarlane[/color]

TRY IT. write a test case. run it. see for yourself.

Victor Bazarov
Guest
 
Posts: n/a
#3: Aug 24 '05

re: Initialization lists and passed parameters


Josh Mcfarlane wrote:[color=blue]
> If the constructor for one of the objects within a class takes a
> parameter of the constructor for the class and modifies it, when the
> parameter is used in the constructor will it be the original parameter,
> or the modified parameter after the initialization list?
>
> Ex:
>
> ClassA::ClassA(unsigned int& Parameter)
> {
> Parameter += 1;
> }
>
> ClassB::ClassB(unsigned int& ParameterB)
> :ClassA(ParameterB)
> {
> Foo = Parameter;
> }
>
> In this case, if someone calls initializes the object as ClassB(0),
> will Foo be 0 or 1?[/color]

Foo should be 1. Why is there a doubt?

V
Frank Chang
Guest
 
Posts: n/a
#4: Aug 24 '05

re: Initialization lists and passed parameters


Josh, Is there a reason why the constructors are not defined as
ClassA::ClassA(const unsigned int& Parameter)
{
//Parameter += 1; Parameter now const
}

ClassB::ClassB(const unsigned int& ParameterB)
:ClassA(ParameterB)
{
Foo = Parameter; // do you mean ParameterB?
}

Thank you.

Old Wolf
Guest
 
Posts: n/a
#5: Aug 24 '05

re: Initialization lists and passed parameters


Victor Bazarov wrote:[color=blue]
> Josh Mcfarlane wrote:[color=green]
>> If the constructor for one of the objects within a class takes a
>> parameter of the constructor for the class and modifies it, when
>> the parameter is used in the constructor will it be the original
>> parameter, or the modified parameter after the initialization list?
>>
>> Ex:
>>
>> ClassA::ClassA(unsigned int& Parameter)
>> {
>> Parameter += 1;
>> }
>>
>> ClassB::ClassB(unsigned int& ParameterB)
>> :ClassA(ParameterB)
>> {
>> Foo = Parameter;
>> }
>>
>> In this case, if someone calls initializes the object as ClassB(0),
>> will Foo be 0 or 1?[/color]
>
> Foo should be 1. Why is there a doubt?[/color]

There should be a compiler error (cannot bind a temporary to
a non-const reference).

Josh Mcfarlane
Guest
 
Posts: n/a
#6: Aug 24 '05

re: Initialization lists and passed parameters


Frank Chang wrote:[color=blue]
> Josh, Is there a reason why the constructors are not defined as
> ClassA::ClassA(const unsigned int& Parameter)
> {
> //Parameter += 1; Parameter now const
> }
>
> ClassB::ClassB(const unsigned int& ParameterB)
> :ClassA(ParameterB)
> {
> Foo = Parameter; // do you mean ParameterB?
> }
>
> Thank you.[/color]

Yes, I meant ParameterB. I may have dumbed down the example too much.
What I am doing is trying to recreate objects from a stream of data. In
the case of ClassB, it has to reconstruct ClassA first, and then classB
should begin it's construction from the new stream of data. I did this
by incrementing the buffer position (IE Buffer += SizeofBufferRead). I
just wanted to verify that ClassB would use the new Buffer start rather
than the original buffer start.

Frank Chang
Guest
 
Posts: n/a
#7: Aug 25 '05

re: Initialization lists and passed parameters


Yes, I just verified that. On MSVC 7.1, the compiler error reads :

error C2664: 'ClassB::ClassB(unsigned int &)' : cannot convert
parameter 1 from 'int' to 'unsigned int &'

Frank Chang
Guest
 
Posts: n/a
#8: Aug 25 '05

re: Initialization lists and passed parameters


Josh, Thank you for the explanation of your application. I tested your
code on MSVC7.1. The only problem I had was the compiler error OldWolf
discovered. But, that can be fixed once one read OldWolf's post.

Closed Thread