由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - prevent assignment
相关主题
C++弱问C++疑问
两个继承问题请教一个作用域的问题
谁给新手解释一下这个c++小程序请教一个C++的编程
这个4d matrix,怎么老是运行不了?[合集] C++问题(copy constructor)
a simple question for C++ classWhy should i include .cpp instead of .h
关于 VC++ vitual, reload 和 derive的一个问题...问一个简单的C++问题
发个初级面试题C++菜问: 怎么这样也可以?
最初级的白痴C++问题C++ 初学者请教一个 iostream 的问题
相关话题的讨论汇总
话题: dog话题: prevent话题: assignment话题: d1话题: d3
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
C++ 初学者请教一个 iostream 的问题a simple question for C++ class
腆着脸在问一道关于 VC++ vitual, reload 和 derive的一个问题...
大家来做题C++。发个初级面试题
小问题最初级的白痴C++问题
C++弱问C++疑问
两个继承问题请教一个作用域的问题
谁给新手解释一下这个c++小程序请教一个C++的编程
这个4d matrix,怎么老是运行不了?[合集] C++问题(copy constructor)
相关话题的讨论汇总
话题: dog话题: prevent话题: assignment话题: d1话题: d3