473,809 Members | 2,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...])
: originalConstru ctor ([...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
20 3235
> 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**********@rw th-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**********@rw th-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...])
: originalConstru ctor ([...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...@hot mail.com wrote:
la**********@rw th-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.co m 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**********@rw th-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**********@rw th-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**********@rw th-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

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

Similar topics

42
5816
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
3
1772
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. Is the reason technical (compiler or CLR limitation) or logical (OOP best practices)? Any feedback would be greatly appreciated.
6
318
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 unless I create a constructor in my two other classes like this: public ClassName(string szParameter1, string szParameter2, string szParameter3) : base(szParameter1, szParameter2, szParameter3) { }
3
1434
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
2055
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 constructors... any thoughts Mike
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.