n**d 发帖数: 9764 | 1 The code is from "Thinking C++ V1". I don't think line 15 will prevent
assignment. I defined a Dog which can be assigned (=) to another Dog. Any
comment?
1 //: C12:ReferenceCounting.cpp
2 // Reference count, copy-on-write
3 #include "../require.h"
4 #include
5 #include
6 using namespace std;
7 class Dog {
8 string nm;
9 int refcount;
10 Dog(const string& name)
11 : nm(name), refcount(1) {
12 cout << "Creating Dog: " << *this << endl;
13 }
14 // Prevent ass |
b********n 发帖数: 609 | 2 google non-copyable
【在 n**d 的大作中提到】 : The code is from "Thinking C++ V1". I don't think line 15 will prevent : assignment. I defined a Dog which can be assigned (=) to another Dog. Any : comment? : 1 //: C12:ReferenceCounting.cpp : 2 // Reference count, copy-on-write : 3 #include "../require.h" : 4 #include : 5 #include : 6 using namespace std; : 7 class Dog {
|
n**d 发帖数: 9764 | 3 This is my testing code. You can see d3 = d1 works.
#include
#include
using namespace std;
class Dog {
string nm;
int refcount;
// Prevent assignment:
Dog& operator=(const Dog& rv);
public:
Dog(const string& name)
: nm(name), refcount(1) {
cout << "Creating Dog: " << *this << endl;
}
// Dogs can only be created on the heap:
static Dog* make(const string& name) {
return new Dog(name);
}
/*
Dog(const Dog& d)
【在 b********n 的大作中提到】 : google non-copyable
|
t****t 发帖数: 6806 | 4 that's not "d3 = d1"
that's "Dog d3 = d1", which is roughly equivalent to "Dog d3(d1)"
to prevent that, you need to make copy constructor private
please read carefully
【在 n**d 的大作中提到】 : This is my testing code. You can see d3 = d1 works. : #include : #include : using namespace std; : class Dog { : string nm; : int refcount; : // Prevent assignment: : Dog& operator=(const Dog& rv); : public:
|
n**d 发帖数: 9764 | 5 Why is roughly, not exactly?
【在 t****t 的大作中提到】 : that's not "d3 = d1" : that's "Dog d3 = d1", which is roughly equivalent to "Dog d3(d1)" : to prevent that, you need to make copy constructor private : please read carefully
|
t****t 发帖数: 6806 | 6 they have some subtle difference which is not frequently used. i suggest you
take it for now.
otherwise you can search my previous post for the difference...
【在 n**d 的大作中提到】 : Why is roughly, not exactly?
|
n**d 发帖数: 9764 | 7
you
Do you mean to think they are the same for now
Give me a hint how to search your previous post.
【在 t****t 的大作中提到】 : they have some subtle difference which is not frequently used. i suggest you : take it for now. : otherwise you can search my previous post for the difference...
|
k**f 发帖数: 372 | 8 Try this to see what the compiler says:
Dog d1("d1");
Dog d3("d3");
d3 = d1; // this calls the assignment operator, which
// is declared private and not implemented |
t****t 发帖数: 6806 | 9 Dog d3(d1) is called direct-initialization
Dog d3=d1 is called copy-initialization
well, now you know the name, try google it...
but again, the difference is subtle. most of times you can ignore it.
【在 n**d 的大作中提到】 : : you : Do you mean to think they are the same for now : Give me a hint how to search your previous post.
|