473,387 Members | 1,504 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,387 software developers and data experts.

Syntax of constructors of inherited classes calling the original constructor

I have to work on a rather big project that a bunch of people wrote and
that has no useful documentation at all, my C++ has rusted in a bit,
now I stumbled over a constructor that looks something like the below
and have no idea what to do with it :)

inheritedClass::inheritedClass ([...bunch of arguments...])
: originalConstructor ([...bunch of arguments...])
, m_cells(cells), m_srfcs(srfcs)
{
[... some code ...]
}

Now my problem is that I have no clue what the comma-separated
arguments(?) m_cells and m_srfcs do in this method definition. Is
anyone able to enlighten me what this syntax is good for?

Besides not knowing what those arguments do, I must know wether this
usage of the two class instances (m_cells and m_srfcs are instances of
2 classes) would call their constructor, because in that case I would
need to modify the argument passed to them.

I would simply test this, but the project is just too big and there are
too many changes that I have to do at once before I can do a test
compilation and check it's runtime behaviour.

Any help is appreciated :)

Regards,

Lars Uffmann

Dec 1 '05 #1
20 3198

lars.uffm...@rwth-aachen.de wrote:
I have to work on a rather big project that a bunch of people wrote and
that has no useful documentation at all, my C++ has rusted in a bit,
now I stumbled over a constructor that looks something like the below
and have no idea what to do with it :)

inheritedClass::inheritedClass ([...bunch of arguments...])
: originalConstructor ([...bunch of arguments...])
, m_cells(cells), m_srfcs(srfcs)
{
[... some code ...]
}

Now my problem is that I have no clue what the comma-separated
arguments(?) m_cells and m_srfcs do in this method definition. Is
anyone able to enlighten me what this syntax is good for?
http://www.parashift.com/c++-faq-lit....html#faq-10.6
Besides not knowing what those arguments do, I must know wether this
usage of the two class instances (m_cells and m_srfcs are instances of
2 classes) would call their constructor, because in that case I would
need to modify the argument passed to them.


Yes, that syntax calls the constructors for m_cells and m_srfcs

Gavin Deane

Dec 1 '05 #2
la**********@rwth-aachen.de wrote:
I have to work on a rather big project that a bunch of people wrote and
that has no useful documentation at all, my C++ has rusted in a bit,
I doubt that this is a good idea. You seem not to be qualified for this.
now I stumbled over a constructor that looks something like the below
and have no idea what to do with it :)

inheritedClass::inheritedClass ([...bunch of arguments...])
: originalConstructor ([...bunch of arguments...])
, m_cells(cells), m_srfcs(srfcs)
{
[... some code ...]
}

Now my problem is that I have no clue what the comma-separated
arguments(?) m_cells and m_srfcs do in this method definition. Is
anyone able to enlighten me what this syntax is good for?
m_cells and m_srfcs are members variables of inheritedClass. The above
initializes theese varibles with the values 'cells' and 'srfcs'.

This is a very basic idiom. You should refer to your Stroustrup (chapter
10.4.6) or other textbook of your choice to read about it.
Besides not knowing what those arguments do, I must know wether this
usage of the two class instances (m_cells and m_srfcs are instances of
2 classes) would call their constructor, because in that case I would
need to modify the argument passed to them.

I would simply test this, but the project is just too big and there are
too many changes that I have to do at once before I can do a test
compilation and check it's runtime behaviour.
No need to change the project. Just create a very small sandbox project
with just one file to explore the syntax and behaviour of this idiom.

Gabriel
Any help is appreciated :)

Regards,

Lars Uffmann

Dec 1 '05 #3
la**********@rwth-aachen.de wrote:

Btw, I'm just curious: At which institute do you work?

Gabriel
I have to work on a rather big project that a bunch of people wrote and
that has no useful documentation at all, my C++ has rusted in a bit,
now I stumbled over a constructor that looks something like the below
and have no idea what to do with it :)

inheritedClass::inheritedClass ([...bunch of arguments...])
: originalConstructor ([...bunch of arguments...])
, m_cells(cells), m_srfcs(srfcs)
{
[... some code ...]
}

Now my problem is that I have no clue what the comma-separated
arguments(?) m_cells and m_srfcs do in this method definition. Is
anyone able to enlighten me what this syntax is good for?

Besides not knowing what those arguments do, I must know wether this
usage of the two class instances (m_cells and m_srfcs are instances of
2 classes) would call their constructor, because in that case I would
need to modify the argument passed to them.

I would simply test this, but the project is just too big and there are
too many changes that I have to do at once before I can do a test
compilation and check it's runtime behaviour.

Any help is appreciated :)

Regards,

Lars Uffmann

Dec 1 '05 #4

la**********@rwth-aachen.de wrote:
I have to work on a rather big project that a bunch of people wrote and
that has no useful documentation at all, my C++ has rusted in a bit,
now I stumbled over a constructor that looks something like the below
and have no idea what to do with it :)

inheritedClass::inheritedClass ([...bunch of arguments...])
: originalConstructor ([...bunch of arguments...])
, m_cells(cells), m_srfcs(srfcs)
{
[... some code ...]
}

Now my problem is that I have no clue what the comma-separated
arguments(?) m_cells and m_srfcs do in this method definition. Is
anyone able to enlighten me what this syntax is good for?

Besides not knowing what those arguments do, I must know wether this
usage of the two class instances (m_cells and m_srfcs are instances of
2 classes) would call their constructor, because in that case I would
need to modify the argument passed to them.

I would simply test this, but the project is just too big and there are
too many changes that I have to do at once before I can do a test
compilation and check it's runtime behaviour.

Any help is appreciated :)

Regards,

Lars Uffmann

the comma separated arguments m_cells and m_srcs are initialization
constructs for those variables of that class.

those constructs call the corresponding constructors depending on the
type of arguments.

Dec 1 '05 #5
> m_cells and m_srfcs are members variables of inheritedClass.
The above initializes theese varibles with the values 'cells' and 'srfcs'.
Thank you - I've had a chance to talk with my "boss" at the institute
in between and that is the information I was looking for, he said the
same :)

For a moment I was wondering why the initialization isn't done in the
inherited Constructor, but that is because then the properly
initialized member variables would NOT be available for the original
constructor to be handled. Either way, this option of initializing
member variables was completely new to me :)
No need to change the project. Just create a very small sandbox
project with just one file to explore the syntax and behaviour of
this idiom. TBH, I only started working with Linux again when I was assigned to
this project, and while I am quite comfortable with producing quality
C++ code, I am not so familiar with the gnu compiler and makefiles yet
:)
I'll have to experiment a bit before I get a test project running I
guess, just been to lazy to do that yet.
Btw, I'm just curious: At which institute do you work?

Institute of Aerodynamics - we're working on a numerical flow solver,
my job is to merge two versions that have been modified by different
people without the use of a CVS, and later to port the flow solver for
usage on a cluster system (simultaneous computing).
Regards,

Lars Uffmann

Dec 1 '05 #6
btw thank you for the parashift link, Gavin, that sort of covered all
questions I had left about the initialization list :)
Except that I am still not sure wether this calls the constructors of
m_cells and m_srfcs. Because the call m_cells(cells) just initializes
the class, and (cells) is not an argument for m_cells constructor, but
rather an object of the same type as m_cells.

Either way, my main question is answered - I do not need to modify the
code in this case at all :)

Regards,

Lars Uffmann

Dec 1 '05 #7

la**********@rwth-aachen.de wrote:
btw thank you for the parashift link, Gavin, that sort of covered all
questions I had left about the initialization list :)
Except that I am still not sure wether this calls the constructors of
m_cells and m_srfcs. Because the call m_cells(cells) just initializes
the class, and (cells) is not an argument for m_cells constructor, but
rather an object of the same type as m_cells.

Either way, my main question is answered - I do not need to modify the
code in this case at all :)

Regards,

Lars Uffmann


in such case it calls the copy constructor of the of the m_cells
objects' class.

Dec 1 '05 #8
Copy constructor is a default constructor that exists for each object
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

Regards,

Lars Uffmann

Dec 1 '05 #9
la**********@rwth-aachen.de wrote:
Copy constructor is a default constructor that exists for each object .....each class that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

Regards,

Lars Uffmann


Yup, that's right. You should be careful with this one. if your class
contains any pointers or contains classes containing pointers or
(recurse this), you are about to crash, since the data is not copied
recursively. In theese cases you should either write the copy
constructor or explicitly deactive it.
You can use this idiom to make the copy constructor inaccesible:
class Blob{
private: Blob& Blob(const Blob&);
// other stuff
};

--
Who is General Failure and why is he reading my hard disk?
Dec 1 '05 #10
> Copy constructor is a default constructor that exists for each object
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?


The default copy constructor is generated for you by the compiler. It
calls all the copy constructors of the members of the class. If a
member variable is from a primitive type - bitwise copy is done in the
manner you describe.

Regards,
Irina Marudina

Dec 1 '05 #11
Hey,

This is a type of initializing the members of class, in which class
contains some other objects as their members. This type of member
initiliazation called as "Member initializqation list"..

U better search "Member initializqation list in c++". in
Google and u may get some idea abt this

..
Gabriel wrote:
la**********@rwth-aachen.de wrote:
Copy constructor is a default constructor that exists for each object

....each class
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

Regards,

Lars Uffmann


Yup, that's right. You should be careful with this one. if your class
contains any pointers or contains classes containing pointers or
(recurse this), you are about to crash, since the data is not copied
recursively. In theese cases you should either write the copy
constructor or explicitly deactive it.
You can use this idiom to make the copy constructor inaccesible:
class Blob{
private: Blob& Blob(const Blob&);
// other stuff
};

--
Who is General Failure and why is he reading my hard disk?


Dec 1 '05 #12

la**********@rwth-aachen.de wrote:
Copy constructor is a default constructor that exists for each object
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

Regards,

Lars Uffmann


You need to be careful with your terminology. Saying the "copy
constructor is a default constructor..." is confusing "default
constructor" means something else.

Having said that, yes the compiler generates a copy constructer
automatically for any class unless you choose to declare one yourself.
But the compiler generated copy constructor does not necessarily simply
do a memory copy. Your example was

inheritedClass::inheritedClass ([...bunch of arguments...])
: originalConstructor ([...bunch of arguments...])
, m_cells(cells), m_srfcs(srfcs)
{
[... some code ...]
}

and we are talking about the construction of m_cells.

If m_cells and cells are of the same type, m_cells(cells) in the
initialiser list copy-constructs m_cells. What that means depends on
the type of m_cells and cells. If m_cells is a pointer as I think you
are saying, then the value of cells is indeed simply copied to m_cells.
Both pointers now have the same value so point to the same object.

But if cells and m_cells were objects of a class type you had defined,
then m_cells(cells) would not simply copy memory, it would construct
m_cells from cells using the copy-constructor for that class.

Gavin Deane

Dec 1 '05 #13

deane_ga...@hotmail.com wrote:
la**********@rwth-aachen.de wrote:
Copy constructor is a default constructor that exists for each object
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

Regards,

Lars Uffmann


You need to be careful with your terminology. Saying the "copy
constructor is a default constructor..." is confusing "default
constructor" means something else.


What's the point of trying to clear up confusion if I'm only going to
write confusing sentences myself. I missed the full stop after "is
confusing". Should have been

You need to be careful with your terminology. Saying the "copy
constructor is a default constructor..." is confusing. "default
constructor" means something else.

Apologies if it wasn't clear first time round.

Gavin Deane

Dec 1 '05 #14
bv****@gmail.com wrote:
Hey,

This is a type of initializing the members of class, in which class
contains some other objects as their members. This type of member
initiliazation called as "Member initializqation list"..

Hmmm. Í don't understand what you are saying. Would you mind to explain
again?

Please don't top-post.

Gabriel
U better search "Member initializqation list in c++". in
Google and u may get some idea abt this

.
Gabriel wrote:
la**********@rwth-aachen.de wrote:
Copy constructor is a default constructor that exists for each object

....each class
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

Regards,

Lars Uffmann

Yup, that's right. You should be careful with this one. if your class
contains any pointers or contains classes containing pointers or
(recurse this), you are about to crash, since the data is not copied
recursively. In theese cases you should either write the copy
constructor or explicitly deactive it.
You can use this idiom to make the copy constructor inaccesible:
class Blob{
private: Blob& Blob(const Blob&);
// other stuff
};

--
Who is General Failure and why is he reading my hard disk?

--
Who is General Failure and why is he reading my hard disk?
Dec 1 '05 #15
> Yup, that's right. You should be careful with this one. if your class
contains any pointers or contains classes containing pointers or
(recurse this), you are about to crash, since the data is not copied
recursively.


Gah - Didn't think about this. I >think< it doesn't necessarily pose a
problem
in this case as the newly constructed instance is supposed to use the
very same object m_cells identified by the (referential) argument
cells.

I just had a closer look at inheritedClass, and apparently m_cells is
declared as a reference:
inheritedClass : originalClass {
// some stuff
cellClass & m_cells;
// some more stuff
}

I just had a look into a documentation of declarations by reference at
http://www.everything2.com/index.pl?node=reference
and it appears to me that the copy constructor m_cells(cells) would -
in this case - initialize m_cells with the memory address of cells so
that inheritedClass can use m_cells just as the calling class used the
object cells. Is that correct?

Regards,

Lars Uffmann

Dec 1 '05 #16
la**********@rwth-aachen.de wrote:
Copy constructor is a default constructor that exists for each object
that you create? Basically doing a memory copy of (cells) and
positioning the object pointer m_cells on that memory?

It's not a "memory copy", it calls the copy constructor for all data
members of the class.

For POD, that's bitwise, but not necessarily so for non-POD.

For this reason, smart pointers and other "smart" objects that properly
manage their internals do not require any special treatment.

If you hold a raw pointer to anything, you will probably need to deal
with it in your copy constructor and assignment operator.

Ben Pope
Dec 1 '05 #17
> If m_cells is a pointer as I think you are saying, then the value of
cells is indeed simply copied to m_cells. Both pointers now have
the same value so point to the same object.


I think that would cover my most recent posts' question - the
initialization list simply makes sure that "inheritedClass" can access
"cells" via it's property "m_cells".
Seems the code is actually doing what it is supposed to be doing.

Reading undocumented, bad structured code by other people is a
nightmare.
Did I just state the obvious? *g*

Thanks everyone - this is probably not the last you'll hear from me,
but this problem is solved now at last :)

Regards,

Lars Uffmann

Dec 1 '05 #18

la**********@rwth-aachen.de wrote:
Yup, that's right. You should be careful with this one. if your class
contains any pointers or contains classes containing pointers or
(recurse this), you are about to crash, since the data is not copied
recursively.


Gah - Didn't think about this. I >think< it doesn't necessarily pose a
problem
in this case as the newly constructed instance is supposed to use the
very same object m_cells identified by the (referential) argument
cells.

I just had a closer look at inheritedClass, and apparently m_cells is
declared as a reference:
inheritedClass : originalClass {
// some stuff
cellClass & m_cells;
// some more stuff
}

I just had a look into a documentation of declarations by reference at
http://www.everything2.com/index.pl?node=reference
and it appears to me that the copy constructor m_cells(cells) would -
in this case - initialize m_cells with the memory address of cells so
that inheritedClass can use m_cells just as the calling class used the
object cells. Is that correct?


Yes. If m_cells and cells are both of type reference-to-cellClass, then
after

m_cells(cells)

in the initialiser list of the inheritedClass constructor, both m_cells
and cells are references to the same object.

But note that while references may well be implemented as pointers by
the compiler, you should not think of them as pointers. They are
different.

http://www.parashift.com/c++-faq-lit...s.html#faq-8.1

Gavin Deane

Dec 1 '05 #19

"Gabriel" <ab***@127.0.0.1> wrote
You can use this idiom to make the copy constructor inaccesible:
class Blob{
private: Blob& Blob(const Blob&);
// other stuff
};


Constructors don't have a return value. For "Blob", the copy constructor
would be:

Blob( const Blob& );

Perhaps you're confusing that with the assignment operator?

-Howard


Dec 1 '05 #20
Howard wrote:
"Gabriel" <ab***@127.0.0.1> wrote
You can use this idiom to make the copy constructor inaccesible:
class Blob{
private: Blob& Blob(const Blob&);
// other stuff
};


Constructors don't have a return value. For "Blob", the copy constructor
would be:

Blob( const Blob& );

Perhaps you're confusing that with the assignment operator?

-Howard


Yes, I was. Looked too long as I wrote it, but I just couldn't see it.
Thanx.

--
Who is General Failure and why is he reading my hard disk?
Dec 1 '05 #21

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. ...
6
by: Doug | last post by:
I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor...
3
by: Alexander Muylaert | last post by:
Hi Is their a way to keep constructors visible in inherited classes? public class X{ public x(int A){} } public class Y : X{};
21
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.