创建两个具有完全相同的变量的对象

Create two objects with exact same variables

本文关键字:变量 对象 两个 创建      更新时间:2023-10-16

我正在制作时间表申请。时间轴可以具有时间表。如果一个项目重复,我希望TimeleneItem保存类型的时间表式媒介,其中时间表和时间表中的值之间的唯一区别是开始和结束时间。

这样,我想让每当我对时间表的实例进行编辑,例如tl_item.setLocation(" Paris"(,与时间表有关的所有时间表列表实例也将更新。

我试图通过创建时间表实例,然后将每个时间表变量的内存位置传递给timelineItemrepeat的构造函数。

目前,我正在宣布变量并将其传递给我的两个构造函数,但是,它不起作用。我的代码:

驱动程序.cpp

short int type = 0;
string desc = "Lunch with Team";
string loc = "New York Office";
time_t start = time_t(0);
time_t end = time_t(600);
vector<TimelineItemRepeat> repeats;
TimelineItem tl_item(type, desc, loc, start, end);
repeats.push_back(TimelineItemRepeat(type, desc, loc, start, end, tl_item));
tl_item.setLinkedItems(repeats);
std::cout << tl_item.toString() << endl;
std::cout << tl_item.getLinkedItems()[0].toString() << endl;
tl_item.setDescription("Dinner with Team");
std::cout << tl_item.toString() << endl;
std::cout << tl_item.getLinkedItems()[0].toString() << endl;

输出

TimelineItem Description Address: 0x7fff5ebcb600
0 Lunch with Team 0 600 1
TimelineItemRepeat Description Address: 0x7fff5ebcb6a0
0 Lunch with Team 0 600
TimelineItem Description Address: 0x7fff5ebcb600
0 Dinner with Team 0 600 1
TimelineItemRepeat Description Address: 0x7fff5ebcb6a0
0 Lunch with Team 0 600

我是错误的方式吗?

我是错误的方式吗?

我会说是的。看来您正在尝试在向量中获得多个位置以引用同一对象。可以通过将 pointers的向量TimelineItem s来轻松实现。这样,我们可以拥有一个向量,称其为 timeline

如果您不知道指针是什么或它们的工作方式,请在处理更多C 之前了解它们。

假设我们希望在我们的向量中重复相同的时间表项。最基本的设置看起来像这样。

//Create a pointer to a dynamically allocated object
TimelineItem *tl_item = new TimelineItem(type, desc, loc, start, end);
vector<TimelineItem*> timeline; //vector of pointers instead of objects.
//all entries point to the same object
timeline.push_back(tl_item);
timeline.push_back(tl_item);
timeline.push_back(tl_item);

现在,您对timeline[0]进行的任何更改都会显示在timeline[1][2]中,因为它们都指向同一对象。由于这些是指针,而不是对象,因此您必须使用.插入->来访问成员,例如

tl_item->setDescription("Dinner with team"); 

具有与

相同的效果
timeline[0]->setDescription("Dinner with team");
timeline[1]->setDescription("Dinner with team");
timeline[2]->setDescription("Dinner with team");

但是,使用指针意味着我们现在需要担心内存分配。完成tl_itemtimeline完成后,您需要清理先前使用new分配的内存:

delete tl_item; //destroys the object; all pointers now point to garbage memory.

这将适用于非常简单的程序,但是我强烈建议您查看std :: shared_ptr,如果您完全关心现代C 的最佳编写。

编辑:

根据评论,您实际上需要的是两个单独的类,一个用于表示事件,一个用于存储时间轴项目。简单示例:

class Event {
    string description;
};
class TimelineItem {
    Event *event;
    timestamp time; //however you want to store this
    //whatever constructors, getters, setters you need
};
vector<TimelineItem> timeline;
Event *dinner = new Event("Dinner with team");
//Let's say we have dinner twice this week. Set these to whatever.
timestamp first_item_ts = ... ;
timestamp second_item_ts = ... ;
//Two separate items in the timeline, at different timestamps, but both refer to the same Event object using pointers!
timeline.push_back(TimelineItem(dinner, first_item_ts));
timeline.push_back(TimelineItem(dinner, second_item_ts));

现在,如果我们更改公共事件对象,则两个时间轴项目都将显示。所有以下所有效果都具有相同的效果:

timeline[0].event->setDescription("Breakfast with team")
timeline[1].event->setDescription("Breakfast with team")
event->setDescription("Breakfast with team")

我在这里遗漏了很多代码,以便清楚设置是什么。希望它的工作原理很清楚。

repeats.push_back(TimelineItemRepeat(type, desc, loc, start, end, tl_item));

上线在向量中创建全新对象(完全独立(,仅复制您的值,因此,如果您希望值更新,则应将重复声明为timeleneItem的指针的向量:

vector<TimelineItem*> repeats;

,然后,您没有添加对象,而是添加对象的地址:

TimelineItem tl_item(type, desc, loc, start, end);
repeats.push_back(&tl_item);

代码中唯一的区别是您必须在此语句中使用->而不是.

std::cout << tl_item.getLinkedItems()[0]->toString() << endl;