且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

复制构造函数的问题(有点长故事......)

更新时间:2022-12-13 22:30:27

* Jeroen:
* Jeroen:

>

基本的想法是我有一个具有数据向量的类,但是该类的对象
也可以在几个

实例之间共享该数据向量:
>
The basic idea is that I have a class which has a data vector, but
objects of that class can also share that data vector between several
instances:



使用boost :: shared_ptr实现共享。如果你想要一个

非共享副本通过一些成员函数提供。不是通过副本

构造函数。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:什么是最烦人的事情usenet和电子邮件?

Use boost::shared_ptr to implement the sharing. If you want a
non-shared copy provide that via some member function. Not via the copy
constructor.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf P. Steinbach schreef:
Alf P. Steinbach schreef:

* Jeroen:
* Jeroen:

>>
基本思想是我有一个具有数据向量的类,但该类的对象也可以共享该数据几个
实例之间的向量:
>>
The basic idea is that I have a class which has a data vector, but
objects of that class can also share that data vector between several
instances:



使用boost :: shared_ptr实现共享。如果你想要一个

非共享副本通过一些成员函数提供。不是通过副本

构造函数。


Use boost::shared_ptr to implement the sharing. If you want a
non-shared copy provide that via some member function. Not via the copy
constructor.



这可能就是问题所在。 A类所在的库使用

共享数据的概念等等。但是对于用户而言,所有这些都是不可用的,也不是可见的。用户期望类的''正常''行为

A.因此,如果库的用户使用下标运算符以便

选择一部分数据对象A并直接将其用作函数的

参数,然后调用复制构造函数,即生成非数据共享副本所需的



void user_function(A a)

{

a = 10; //参数''a''必须在这里更改,而不是原始数据!

}


void other_user_function()

{

A a;

user_function(a(1:10)); //或者我在这里使用的任何语法....

}


代码''(1:10")' '结果是A级对象与''父级'共享其

数据(这是用我原来的帖子编写的方法

''get_shared_A''在A类中,但实际上它是一个运算符),但是副本

构造函数调用''user_function''必须创建一个A,它不会在
***享其数据为了提供正确的行为。


鉴于上述情况,我在你的建议中没有看到解决方案...


Jeroen

That may just be the problem. The library in which class A resides uses
the concept of shared data and so on. But for the user all of this is
not available, nor visible. The user expects ''normal'' behaviour of class
A. So if a user of the library uses a subscript operator in order to
select a portion of the data in an object A and directly uses that as a
parameter to a function, then the copy constructor is called which is
required to make a non data-sharing copy:

void user_function(A a)
{
a = 10; // parameter ''a'' must be changed here, not the original data!
}

void other_user_function()
{
A a;
user_function(a("1:10")); // or whatever syntax I''l use here....
}

The code ''a("1:10")'' results in an object of class A which shares its
data with its ''parent'' (this was coded in my original post by method
''get_shared_A'' in class A, but its an operator in fact), but the copy
constructor called for ''user_function'' must create an A which does not
share its data in order to provide correct behaviour.

Given the above, I don''t see the solution in your suggestion...

Jeroen


* Jeroen:
* Jeroen:

Alf P. Steinbach schreef:
Alf P. Steinbach schreef:

> * Jeroen:
>* Jeroen:

>>>
基本的想法是我有一个有数据向量的类,但该类的对象也可以在s之间共享该数据向量几个
实例:
>>>
The basic idea is that I have a class which has a data vector, but
objects of that class can also share that data vector between several
instances:


使用boost :: shared_ptr实现共享。如果你想通过一些成员函数提供
非共享副本。不是通过
复制构造函数。


Use boost::shared_ptr to implement the sharing. If you want a
non-shared copy provide that via some member function. Not via the
copy constructor.



这可能就是问题所在。 A类所在的库使用

共享数据的概念等等。但是对于用户而言,所有这些都是不可用的,也不是可见的。用户期望类的''正常''行为

A.因此,如果库的用户使用下标运算符以便

选择一部分数据对象A并直接将其用作函数的

参数,然后调用复制构造函数,即生成非数据共享副本所需的



void user_function(A a)

{

a = 10; //参数''a''必须在这里更改,而不是原始数据!

}


void other_user_function()

{

A a;

user_function(a(1:10)); //或者我在这里使用的任何语法....

}


代码''(1:10")' '结果是A级对象与''父级'共享其

数据(这是用我原来的帖子编写的方法

''get_shared_A''在A类中,但实际上它是一个运算符),但是副本

构造函数调用''user_function''必须创建一个A,它不会在
***享其数据为了提供正确的行为。


鉴于上述情况,我在你的建议中没有看到解决方案......


That may just be the problem. The library in which class A resides uses
the concept of shared data and so on. But for the user all of this is
not available, nor visible. The user expects ''normal'' behaviour of class
A. So if a user of the library uses a subscript operator in order to
select a portion of the data in an object A and directly uses that as a
parameter to a function, then the copy constructor is called which is
required to make a non data-sharing copy:

void user_function(A a)
{
a = 10; // parameter ''a'' must be changed here, not the original data!
}

void other_user_function()
{
A a;
user_function(a("1:10")); // or whatever syntax I''l use here....
}

The code ''a("1:10")'' results in an object of class A which shares its
data with its ''parent'' (this was coded in my original post by method
''get_shared_A'' in class A, but its an operator in fact), but the copy
constructor called for ''user_function'' must create an A which does not
share its data in order to provide correct behaviour.

Given the above, I don''t see the solution in your suggestion...



不要对它说好话,你很困惑。你想要一个具有两个不兼容属性的副本

构造函数:制作一个非共享的

副本(仅限),并制作一个共享副本(仅限)。如果你在上面写的那个共享是一个实现细节,那么试着准确地定义

它的意图是什么;例如可能是你想要实现的是b
实现的是一些写时复制方案,在这种情况下就像我写的一样。


- -

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

答:热门发布。

问:usenet和电子邮件中最烦人的是什么?

Not to put to fine words on it, you''re confused. You want a copy
constructor that has two incompatible properties: making a non-shared
copy (only), and making a shared copy (only). If the sharing, as you
write above, is an implementation detail, then try to define exactly
what it''s meant to accomplish; e.g. it may be that what you''re trying to
achieve is some copy-on-write scheme, in which case do as I wrote.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?