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

Question about constructors

Suppose I have the following code fragment:

class base {
private:
int m;

public:
base(int x) { m = x; }
};

class derived : public base {
private:
otherclass o;

public:
derived(int x) : base(x) { }
};

When I create a derived, is otherclass' constructor run or not? I did
an experiment and it was. However, I'd understood that it wouldn't be.
I thought that *if* I didn't define a default constructor for base
and/or derived, the computer would write one for me which initialised
all the members, but that, in the above case where I have defined a
non-default constructor, it wouldn't start writing constructors for me
(other than a copy constructor). Can you put me straight on this
please? (By the way, the behaviour of otherclass' constructor running
is what I want.)

Thanks in advance.
Paul.

Jan 11 '07 #1
4 1322
<gw****@aol.comwrote in message
news:11**********************@p59g2000hsd.googlegr oups.com
Suppose I have the following code fragment:

class base {
private:
int m;

public:
base(int x) { m = x; }
};
The preferred way to do this is:

base(int x) : m(x){}
class derived : public base {
private:
otherclass o;

public:
derived(int x) : base(x) { }
};

When I create a derived, is otherclass' constructor run or not? I did
an experiment and it was. However, I'd understood that it wouldn't be.
I thought that *if* I didn't define a default constructor for base
and/or derived, the computer would write one for me which initialised
all the members, but that, in the above case where I have defined a
non-default constructor, it wouldn't start writing constructors for me
(other than a copy constructor). Can you put me straight on this
please? (By the way, the behaviour of otherclass' constructor running
is what I want.)
The compiler doesn't write a default constructor for you. However, for the
constructor that you have written, a failure to explicitly initialize
members of class type in the initialization list means that the default
constructor of those members is called, i.e.,

derived(int x) : base(x) { }

is read as

derived(int x) : base(x), o() { }
--
John Carson

Jan 11 '07 #2

Thank you to to both you (mlimber) and John Carson for your quick and
helpful answers.

mlimber wrote:
gw7...@aol.com wrote:
Suppose I have the following code fragment:

class base {
private:
int m;

public:
base(int x) { m = x; }
};

class derived : public base {
private:
otherclass o;

public:
derived(int x) : base(x) { }
};

When I create a derived, is otherclass' constructor run or not?

It is: non-POD types are default-constructed after base classes if they
are omitted from the initializer list. Compare this FAQ:

http://parashift.com/c++-faq-lite/ctors.html#faq-10.6
Ah, now I see it - this FAQ is saying that if you don't use the
initialiser list then the members get default constructed, and if this
is not what you want then you have wasted time doing it before
assigning the desired values. The collorary being that, if the default
constructed values are fine, it is OK not to mention them in the
initialiser list or the constructor body. In this respect, the comment
"constructors should initialize as a rule all member objects in the
initialization list" is a little misleading.

Jan 11 '07 #3
gw7...@aol.com wrote:
Suppose I have the following code fragment:

class base {
private:
int m;

public:
base(int x) { m = x; }
};

class derived : public base {
private:
otherclass o;

public:
derived(int x) : base(x) { }
};

When I create a derived, is otherclass' constructor run or not?
It is: non-POD types are default-constructed after base classes if they
are omitted from the initializer list. Compare this FAQ:

http://parashift.com/c++-faq-lite/ctors.html#faq-10.6
I did
an experiment and it was. However, I'd understood that it wouldn't be.
I thought that *if* I didn't define a default constructor for base
and/or derived, the computer would write one for me which initialised
all the members,
No members are initialized except what you initialize in the
constructor. Default constructors are called for non-POD types, but if
their constructors don't initialize their POD members, then they don't
get initialized either. Consider:

struct A
{
int i_, j_;
A() : i_(0) {} // i_ is initialized, j_ is not
};

struct B : A
{
int m_, n_;
A a_;
B() : m_(0) {} // Base class is implicitly default-constructed,
// a_ is implicitly default constructed,
// m_ is initialized, n_ is not
};

int main()
{
B b;
// ...
}
After B's constructor completes, the following is true:

* b.i_, b.m_, and b.a_.i_ are initialized to 0
* b.n_, b.j_, and b.a_.j_ are all uninitialized
but that, in the above case where I have defined a
non-default constructor, it wouldn't start writing constructors for me
(other than a copy constructor). Can you put me straight on this
please?
The compiler is not generating a default constructor for base or
derived, but it is calling the default constructor for otherclass.

Cheers! --M

Jan 11 '07 #4
gw7...@aol.com wrote:
Thank you to to both you (mlimber) and John Carson for your quick and
helpful answers.

mlimber wrote:
gw7...@aol.com wrote:
Suppose I have the following code fragment:
>
class base {
private:
int m;
>
public:
base(int x) { m = x; }
};
>
class derived : public base {
private:
otherclass o;
>
public:
derived(int x) : base(x) { }
};
>
When I create a derived, is otherclass' constructor run or not?
It is: non-POD types are default-constructed after base classes if they
are omitted from the initializer list. Compare this FAQ:

http://parashift.com/c++-faq-lite/ctors.html#faq-10.6

Ah, now I see it - this FAQ is saying that if you don't use the
initialiser list then the members get default constructed,
....*unless* the (non-static) members are POD types...
and if this
is not what you want then you have wasted time doing it before
assigning the desired values. The collorary being that, if the default
constructed values are fine, it is OK not to mention them in the
initialiser list or the constructor body. In this respect, the comment
"constructors should initialize as a rule all member objects in the
initialization list" is a little misleading.
No the rule is correct (assuming you add "or constructor body" to the
end). A constructor should either initialize a class' members
explicitly (as it must with POD types) or implicitly, which default
constructors handle.

Cheers! --M

Jan 11 '07 #5

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

Similar topics

6
by: Sean Dettrick | last post by:
Hi, I have a class which I want everything to have access to, and which I only want to construct once. I have a hierarchy of classes: class ScalarField{ vector<double> f; // constructors etc...
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...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
10
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now,...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
12
by: Oleg Subachev | last post by:
I am moving from Delphi to C# and hve encountered the problem: I have the following classes and form Load event handler: public class class1 { public string S; public class1( string aS ) {...
31
by: cctv.star | last post by:
I was wondering, why you always have to remember to call bases' constructors explicitly from the derived class constructor? Why hasn't this been enforced by the language?
5
by: Dom | last post by:
is there a difference between the following: Rectangle r = new Rectangle (10,20,30,40); Rectangle r = Rectangle.FromLTRB(10,20,30,40); Also, I guess I'm not sure why Structs are created with a...
5
by: bz | last post by:
Hi, I have a base class with several constructors and I want to create a derived class, as below class MyBase { public MyBase() {...} public MyBase(int ID) {...} public MyBase(string...
2
by: Greg | last post by:
Hi, I have a class that has 3 different constructors in it. In the base constructor I want to have it set some default data. I don't want to have the overloaded constructors do these same...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.