mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Followup to close #1586
This commit is contained in:
@@ -20271,17 +20271,20 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
|
|||||||
|
|
||||||
##### Example, bad
|
##### Example, bad
|
||||||
|
|
||||||
|
// Old conventional style: many problems
|
||||||
|
|
||||||
class Picture
|
class Picture
|
||||||
{
|
{
|
||||||
int mx;
|
int mx;
|
||||||
int my;
|
int my;
|
||||||
char * data;
|
char * data;
|
||||||
public:
|
public:
|
||||||
|
// main problem: constructor does not fully construct
|
||||||
Picture(int x, int y)
|
Picture(int x, int y)
|
||||||
{
|
{
|
||||||
mx = x,
|
mx = x; // also bad: assignment in constructor body rather than in member initializer
|
||||||
my = y;
|
my = y;
|
||||||
data = nullptr;
|
data = nullptr; // also bad: constant initialization in constructor rather than in member initializer
|
||||||
}
|
}
|
||||||
|
|
||||||
~Picture()
|
~Picture()
|
||||||
@@ -20289,6 +20292,7 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
|
|||||||
Cleanup();
|
Cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bad: two-phase initialization
|
||||||
bool Init()
|
bool Init()
|
||||||
{
|
{
|
||||||
// invariant checks
|
// invariant checks
|
||||||
@@ -20298,10 +20302,11 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
|
|||||||
if (data) {
|
if (data) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
data = (char*) malloc(mx*my*sizeof(int));
|
data = (char*) malloc(mx*my*sizeof(int)); // also bad: owning raw * and malloc
|
||||||
return data != nullptr;
|
return data != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// also bad: no reason to make cleanup a separate function
|
||||||
void Cleanup()
|
void Cleanup()
|
||||||
{
|
{
|
||||||
if (data) free(data);
|
if (data) free(data);
|
||||||
@@ -20320,11 +20325,11 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
|
|||||||
|
|
||||||
class Picture
|
class Picture
|
||||||
{
|
{
|
||||||
ptrdiff_t mx;
|
int mx;
|
||||||
ptrdiff_t my;
|
int my;
|
||||||
vector<char> data;
|
vector<char> data;
|
||||||
|
|
||||||
static ptrdiff_t check_size(ptrdiff_t size)
|
static int check_size(int size)
|
||||||
{
|
{
|
||||||
// invariant check
|
// invariant check
|
||||||
Expects(size > 0);
|
Expects(size > 0);
|
||||||
@@ -20333,7 +20338,7 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// even better would be a class for a 2D Size as one single parameter
|
// even better would be a class for a 2D Size as one single parameter
|
||||||
Picture(ptrdiff_t x, ptrdiff_t y)
|
Picture(int x, int y)
|
||||||
: mx(check_size(x))
|
: mx(check_size(x))
|
||||||
, my(check_size(y))
|
, my(check_size(y))
|
||||||
// now we know x and y have a valid size
|
// now we know x and y have a valid size
|
||||||
@@ -20341,6 +20346,7 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
|
|||||||
{
|
{
|
||||||
// picture is ready-to-use
|
// picture is ready-to-use
|
||||||
}
|
}
|
||||||
|
|
||||||
// compiler generated dtor does the job. (also see C.21)
|
// compiler generated dtor does the job. (also see C.21)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user