我在制作时间轴应用程序。时间轴可以有一个TimelineItem。如果项目重复,我希望TimelineItem保存TimelineItemRepeat类型的向量,其中TimelineItem和TimelineItemRepeat中的值之间的唯一区别是开始和结束时间。使用完全相同的变量创建两个对象
因此,我希望每当我对TimelineItem实例进行编辑时, tl_item.setLocation(“Paris”),与TimelineItem相关的所有TimelineItemRepeat实例也将更新。
我试图通过创建TimelineItem实例,然后传递每个TimelineItem的变量的存储位置的构造TimelineItemRepeat实现这一目标。
目前,我声明变量并将它传递给我的两个构造函数,但是,它不起作用。我的代码:
driver.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
我要对这个错误的方式?
请显示一些代码! – OldProgrammer
对我来说,您应该创建一个TimelineItem **指针**的向量,以便重复的条目引用内存中的同一个对象。 – ApproachingDarknessFish
@OldProgrammer新增。据我所知,它的原因是TimelineItem正在创建自己的变量实例,在driver.cpp的顶部声明,所以创建一个新地址,而TimelineItemRepeat只是获取变量的地址。因此,如果我要更改driver.cpp中的变量,则TimelineItemRepeat会更新。 我只是不知道如何TimelineItem的数据域的位置传递给TimelineItemRepeat。 – Beardo