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(cons t 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! 2 5992
"ccs" <cc*@stopspammi ng.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(cons t 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
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(cons t 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 says). But in the example below the
copy
constructor of Vec is executed when initializing the Ref object. Is this
standard conform? Doesn't it result in bad performance?
#include <iostream>
|
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 I found no satisfactory answer yet.
It is about composed/aggregated classes. I am developing a scientific
code (Monte Carlo) in
which I find it natural to have classes with several levels of
aggregation.
|
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 25.) These
leaks would still be there when wrapping the result of each "new" in a
temporary smart pointer object.
Is there a simular risk when having multiple calls to new in member
initializer list of a constructor? For example:
class Foo
|
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, salary) the
compiler tells me no matching function for call to `Person::Person()'
Employee::Employee(string name, int age, string job_title, int salary)
{
Person(name, age);
|
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 class, if the
class has more than than one cosntructor, by making sure that each
constructor has a different signature. I have managed to learn that and get
| |
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;
};
|
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)
{
}
|
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 declaration in
the class, instead of the order they appear in the constructor
initializer. This would introduce interdependencies. Therefore, it
is safer to avoid such interdependence by assigning values to these
members inside the constructor...
|
by: Pallav singh |
last post by:
How can we justify that initializer list is better in performance
than assignment list in constructor of C++ ??
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |