Skip to content

Commit

Permalink
added 7.43~7.46
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Nov 20, 2014
1 parent 5c07a01 commit c849257
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ch07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,19 @@ private:
std::string pubdate_;
};
```

## [Exercise 7.43](ex7_43.cpp)
## Exercise 7.44

illegal, cause there are ten elements, each would be default initialized. But no default initializer for the temporary object.

## Exercise 7.45

No problem. cause `C` have the default constructor.

## Exercise 7.46

- a) A class must provide at least one constructor. (**unture**, "The compiler-generated constructor is known as the synthesized default constructor.")
- b) A default constructor is a constructor with an empty parameter list. (**unture**, A default constructor is a constructor that is used if no initializer is supplied)
- c) If there are no meaningful default values for a class, the class should not provide a default constructor. (**unture**, the class should provide.)
- d) If a class does not define a default constructor, the compiler generates one that initializes each data member to the default value of its associated type. (**unture**, only if our class does not explicitly define any constructors, the compiler will implicitly define the default constructor for us.)
29 changes: 29 additions & 0 deletions ch07/ex7_43.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ex7_43.cpp
// Exercise 7.43
//
// Created by pezy on 11/20/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#include <vector>

class NoDefault {
public:
NoDefault(int i) { }
};

class C {
public:
C() : def(0) { } // define the constructor of C.
private:
NoDefault def;
};

int main()
{
C c;

std::vector<C> vec(10);
return 0;
}

0 comments on commit c849257

Please sign in to comment.