参考数据类成员访问者模式(reference data class member visitor pattern)
我有一个容器类,它有一个名为DataStore的普通旧数据类型。 此数据存储将传递给访问者。他们共享一个副本,因为访问者可以看到容器执行的任何更新。
class xyz { //Plain Old Datatype typedef struct DataStore { //add any new required data by any test here bool _detectorConnectionStatus; DataStore():_detectorConnectionStatus(false){} }DataStore; DataStore _dataStore; typedef struct visitorData { DataStore& dataStore; visitorData(DataStore data):dataStore(data){} }visitorData; //data to be sent to visitor visitorData _visitorData; }; xyz::xyz():_visitorData(_dataStore) { } class IVisitor { private: struct DataStore* dataStore; public: //get the data,no check for validity of data performed void visit(struct DataStore& dataStore){}; //process the data virtual void process() = 0; };
代码编译但是有更好/更清洁的方法吗?
I have a container class that has a plain old datatype called DataStore. This data store will be passed to visitor.They share a single copy since any updated performed by the container are to be seen by visitor.
class xyz { //Plain Old Datatype typedef struct DataStore { //add any new required data by any test here bool _detectorConnectionStatus; DataStore():_detectorConnectionStatus(false){} }DataStore; DataStore _dataStore; typedef struct visitorData { DataStore& dataStore; visitorData(DataStore data):dataStore(data){} }visitorData; //data to be sent to visitor visitorData _visitorData; }; xyz::xyz():_visitorData(_dataStore) { } class IVisitor { private: struct DataStore* dataStore; public: //get the data,no check for validity of data performed void visit(struct DataStore& dataStore){}; //process the data virtual void process() = 0; };
The code compiles but is there a better/cleaner way to do it?
原文:https://stackoverflow.com/questions/30723758
最满意答案
由于您的功能正在关闭
flux
,SEM
和depth
因此您正在使用del
s来修复返回的值,因此您遇到了问题。
flux
是一个全局可变列表,每个通过你的函数你清除flux
的内容,重新填充它,然后返回一个你之后存储为flux_n
flux
的引用。 这就是你在同一个底层list
对象中的所有flux_n
点。 这同样适用于SEM_n
。当您使用
matplotlib
绘图时,数据会被复制,因此一旦您创建了现有的图形,它们就不会受到影响。试试:
def data_reader(file_name, first_surface): flux = [] SEM = [] depth = [] with open (file_name) as inf: lines = inf.readlines() for (i, line,) in enumerate(lines): if ' surface %d' %first_surface in line: data = (lines[i+1].strip()) fields = data.split() numbers = list(map(float,fields)) flux.append(numbers[0]) SEM.append(numbers[1]) first_surface += 1 depth.append(first_surface - 101) return (flux, SEM, first_surface)
You are having issues due to the fact that your function is closing over
flux
,SEM
anddepth
so you are mucking up the returned values with thedel
s.
flux
is a global mutable list, each pass through your function your clear the contents offlux
, re-fill it, and then return a reference toflux
which you then store asflux_n
. That is all of yourflux_n
point at the same underlyinglist
object. The same applies forSEM_n
.When you plot using
matplotlib
that data gets copied so your existing graphs are not affected once you make them.Try :
def data_reader(file_name, first_surface): flux = [] SEM = [] depth = [] with open (file_name) as inf: lines = inf.readlines() for (i, line,) in enumerate(lines): if ' surface %d' %first_surface in line: data = (lines[i+1].strip()) fields = data.split() numbers = list(map(float,fields)) flux.append(numbers[0]) SEM.append(numbers[1]) first_surface += 1 depth.append(first_surface - 101) return (flux, SEM, first_surface)
相关问答
更多-
覆盖内存中的数据(Overwriting data in memory)[2022-04-30]
TL; DR; 你不应该在OCaml堆中存储任何秘密信息。 因此,您绝不能将您的秘密复制到任何OCaml堆分配值中,因此,即使是暂时的,也不能使用字节,字符串或阵列。 OCaml内存模型介绍 OCaml值统一表示为标记的机器字。 一个字的最低有效位用作标签,区分指针(标签= 0)和即时值(标签= 1)。 因此,值总是固定的大小,并且是指针或直接值。 即时值将数据存储在单词的最重要部分,即32位系统中的31位和64位系统中的63位。 指针将数据存储在块中,位于所谓的OCaml堆中 。 OCaml堆是由垃圾收集 ... -
防止Mysql覆盖数据(Prevent Mysql from overwriting data)[2021-07-08]
通过获取现有值并在PHP中加入字符串来解决问题 $id = $_GET["id"]; $username = $_SESSION['username']; $sql = "SELECT `purchased` FROM `users` WHERE `username` = '".$_SESSION['username']."'"; $data = mysql_query($sql); while($row = mysql_fetch_row($data)) { $ep = $row[0]; ... -
什么是覆盖数据?(what is overwriting the data?)[2023-07-11]
释放内存后,通常会重新分配内存。 当该内存存储在堆栈中时尤其如此。 所以,是的。 After the memory is freed it will often be reassigned. This is especially true when that memory is stored on the stack. So, yes. -
这很正常,所以你能做的就是制作这样的字典 let data = ["Fornavn": Fornavn.text!,"Efternavn": Efternavn.text!] 然后呢 self.ref.child("users").child(Personnr.text!).setValue(data) 这样做是因为它在Personnr.text父节点下存储了Fornavn和Efternavn数据 That is pretty normal so what you can do is make a di ...
-
覆盖ZipEntrys(Overwriting ZipEntrys)[2023-12-12]
如果您尝试添加重复的ZipEntry,您将获得异常。 如果要替换当前条目,则需要将其删除并重新插入。 我怀疑你获得的异常与这个异常相同。 If you try to add a duplicate ZipEntry you will get an exception. If you want to replace the current entry you need to delete it and re-insert it. I suspect the exception you get is much ... -
功能覆盖数据(Function Overwriting Data)[2022-06-04]
由于您的功能正在关闭flux , SEM和depth因此您正在使用del s来修复返回的值,因此您遇到了问题。 flux是一个全局可变列表,每个通过你的函数你清除flux的内容,重新填充它,然后返回一个你之后存储为flux_n flux的引用。 这就是你在同一个底层list对象中的所有 flux_n点。 这同样适用于SEM_n 。 当您使用matplotlib绘图时,数据会被复制,因此一旦您创建了现有的图形,它们就不会受到影响。 试试: def data_reader(file_name, first_su ... -
ArrayList覆盖本身(ArrayList overwriting itself)[2022-01-25]
这是你的问题: public class Frame{ private static Listframe = new ArrayList (); private static int size; private static int serialNum; Frame的成员不应该是静态的,因为静态成员是在类的所有实例之间共享的。 删除静态关键字。 相应地改变构造函数: public class Frame { private List ... -
在didSelectItemAtIndexPath您要将Dictionary添加到Array ,您可以使用此Dictionary来区分它来自main collectionView,而不是将isFromMain实例值设置为1 。 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { var dictSub = arr[indexPath.row] ...
-
导入JSON文件而不覆盖Firebase中的现有数据(Import JSON file without overwriting the existing data in Firebase)[2023-05-17]
您可以在Firebase数据库控制台中的任何位置导入JSON。 所以不仅在根,而且在其下的特定路径,例如/users , /users/charlinagnes等。 在某个位置导入JSON时,Firebase会在该位置执行setValue()操作 。 因此,它会使用您提供的新JSON覆盖该位置的现有数据。 没有用于执行任何类型的合并操作的UI。 但幸运的是Firebase拥有广泛的API(毕竟它是开发人员产品),允许您编写自己的合并逻辑。 使用update()方法可能有助于此类合并。 You can Imp ... -
opneing文件时的模式是错误的,应该是a+ 。 详见文档: http : //ruby-doc.org/core-2.2.4/IO.html#method-c-new 此外,您可能只想打开该文件一次而不是每行。 Your mode when opneing the file is wrong and should be a+. See details in the docs: http://ruby-doc.org/core-2.2.4/IO.html#method-c-new Also, you mi ...