473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Initialization lists and passed parameters

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

Aug 24 '05 #1
7 1272

Josh Mcfarlane wrote:
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.

Aug 24 '05 #2
Josh Mcfarlane wrote:
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
Aug 24 '05 #3
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.

Aug 24 '05 #4
Victor Bazarov wrote:
Josh Mcfarlane wrote:
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?


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

Aug 24 '05 #5
Frank Chang wrote:
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.

Aug 24 '05 #6
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 &'

Aug 24 '05 #7
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.

Aug 24 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
8
by: Jef Driesen | last post by:
Hello, Suppose I have a class that looks like this: class point { protected: unsigned int m_x, m_y; // OR unsigned int m_data; public: point(); point(unsigned int x, unsigned int y);
8
by: Baloff | last post by:
Hello I am not sure why my compiler will not initialize string e1("sam"); and will initialize string e1 = "sam"; here is my code and the error. thanks alot
11
by: natkw1 | last post by:
Hi, I'm new to C so hopefully someone can give me some guidance on where I've gone wrong. I've written the following code, trying to initialize the multi-dim array but it's not working: ...
4
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and...
8
by: Ed Dror | last post by:
Hi there ASP.NET 2.0 VB & SQL Express Lest take Northwind Categories Products as example I create a table that hold these two together and I create a stored procedure like select ProductID,...
6
by: giulianodammando | last post by:
In the development of a simple numerical simulation software i need to read some initialization parameters from a file that looks like: # Global Setup species = 1; \begin{specie}<1>...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
2
by: dj3vande | last post by:
Is this code, as the complete contents of a translation unit, valid C90 that initializes the struct foo to contain copies of the values passed to bar()? -------- struct foo { int i; void *v;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.