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 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.
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
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.
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).
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.
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 &'
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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);
|
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
|
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:
...
|
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...
|
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,...
|
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>...
|
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...
|
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;...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |