473,383 Members | 1,863 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.

initializer list of copy constructor

ccs
First, no compiling error for the following code...

class CStudent
{
int id;
public:
CStudent(int i) : id(i);
};

class CTeam
{
CStudent s;
public:
CTeam(int i) : s(i) {}
CTeam(const CStudent& c) : s(c.s);
};

Is the code above correct to use intializer list for copy constructor?

Is it correct to have different initializer lists for CTeam(int i) and
CTeam(const CStudent& c)?

For statement "CTeam(const CStudent& c) : s(c.s);", is the default copy
constructor of CStudent triggered?

Is it necessary to define a copy constructor for CStudent to replace its
default one?
Thanks in advance!
Jul 22 '05 #1
2 5978
"ccs" <cc*@stopspamming.com> wrote...
First, no compiling error for the following code...

class CStudent
{
int id;
public:
CStudent(int i) : id(i);
Really? No error here? What compiler?
};

class CTeam
{
CStudent s;
public:
CTeam(int i) : s(i) {}
CTeam(const CStudent& c) : s(c.s);
And here too? Really?! I somehow can't belive that.
};

Is the code above correct to use intializer list for copy constructor?
No, of course not. You cannot have an initialiser list in a declaration.
The correct way is to use an initialiser list in a definition:

CTeam(CStudent const &c) : s(c.s) {}

(notice the curly braces after the initialiser list).

Is it correct to have different initializer lists for CTeam(int i) and
CTeam(const CStudent& c)?
Sure. Whatever is dicated by your design.

For statement "CTeam(const CStudent& c) : s(c.s);", is the default copy
constructor of CStudent triggered?
The statement you're quoting here is a syntax error. If you have the
copy c-tor for CTeam as I recommended, then yes, the compiler-defined
copy c-tor for 'CStudent' will be used to construct the 's' member.

Is it necessary to define a copy constructor for CStudent to replace its
default one?


No.

Victor
Jul 22 '05 #2
ccs wrote:
First, no compiling error for the following code...

class CStudent
{
int id;
public:
CStudent(int i) : id(i);
This should actually procuce an error message. Your constructor is
missing a body.
};

class CTeam
{
CStudent s;
public:
CTeam(int i) : s(i) {}
CTeam(const CStudent& c) : s(c.s);
Same here.
};

Is the code above correct to use intializer list for copy constructor?
No. Even if you have an initalizer list, the constructor needs a body.
Is it correct to have different initializer lists for CTeam(int i) and
CTeam(const CStudent& c)?
Sure. What would they be good for if that weren't the case?
For statement "CTeam(const CStudent& c) : s(c.s);", is the default
copy constructor of CStudent triggered?
Yes.
Is it necessary to define a copy constructor for CStudent to replace
its default one?


In the above example, no.

Jul 22 '05 #3

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

Similar topics

6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
1
by: Niels Dekker - no reply address | last post by:
The book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu warns against potential memory leaks when having multiple calls to operator new within a single statement. (Item 13, page...
3
by: James | last post by:
I have a base class that has constructor Person(string name, int age) and a derived class Empolyee(string job_title, int salary) When I try to call it using new Employee(name, age, job_title,...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
1
by: Vincent RICHOMME | last post by:
Hi, Let' say I have a class with an object pointer as member something like: // classA.h class A { A(B* pB); B* m_pB; };
3
by: Ham Pastrami | last post by:
class Point { public: const int x, y; Point(int x, int y); } Point::Point(int x, int y) : x(x), y(y) { }
8
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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
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?
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.