forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage626_derived_constructor.cpp
More file actions
46 lines (46 loc) · 1.39 KB
/
Copy pathPage626_derived_constructor.cpp
File metadata and controls
46 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Warning: this is for illustration only and reflects bad practice. It can't compile.
#include <iostream>
#include <string>
class D;
class Base {
public:
Base() = default;
Base(const D&);
Base(const Base&) = default;
Base(Base&&) = default;
Base& operator=(const Base&) = default;
Base& operator=(Base&&) = default;
virtual ~Base() = default;
protected:
int i = 0;
};
class D : public Base {
public:
D() = default;
D(const D &d) : Base(d), s(d.s) {}
protected:
std::string s;
};
// We can use the member of D until it's defined
Base::Base(const D &d)
{
std::cout << "Base(const D&) is called" << std::endl;
i = d.i;
std::cout << s << std::endl; // error: s in not is scope
}
int main()
{
D d1;
D d2(d1); // calls Base(const D&)
Base b(d1); // calls Base(const D&)
return 0;
}
// Note: on page 626, it says:
// Although in principle, Base could have a constructor that has a parameter of
// type D, in practice, that is very unlikely.
// As the code above shows, if we handle the order of declaration and definition
// carefully, Base could have a constructor that has a parameter of type D, but
// that will lead to a circle: we use the constructor of Base to initialize an
// object of type D, but the constructor itself takes a D parameter. D should be
// built on Base, thus we shouldn't use type D's object to initialize type Base's.
// See page 619.