Initialization lists and passed parameters

J

Josh Mcfarlane

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
 
V

Victor Bazarov

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

Foo should be 1. Why is there a doubt?

V
 
A

Alipha

Josh said:
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

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

Frank Chang

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

Josh Mcfarlane

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

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

Frank Chang

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 &'
 
F

Frank Chang

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top