创建两个具有完全相同的变量的对象
Create two objects with exact same variables
我正在制作时间表申请。时间轴可以具有时间表。如果一个项目重复,我希望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_item
和timeline
完成后,您需要清理先前使用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;
- 你能重载对象变量名本身返回的内容吗
- 将包含C样式数组的对象初始化为成员变量(C++)
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我们再次从结构对象创建结构变量?
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- 从变量对象指向基对象
- C++ - 我可以创建编译时变量对象吗?
- 动态数组添加元素而不创建结构变量/对象
- Visual Studio C++ 编译器在局部变量对象上的奇怪行为
- 其中定义函数的静态局部变量对象
- QT良好的全局变量/对象处理
- r 值的匿名变量对象与真实对象
- QT 测试 - 变量/对象失去价值
- 访问私有变量对象
- C++访问具有相同名称的变量/对象的命名空间中的全局变量/对象
- C++-访问全局变量对象
- 表达式必须具有类类型:c++,将实例变量对象作为指针传递(点表示法参数)
- C++变量/对象和堆内存管理
- 如何在每次迭代中创建一个新变量/对象的循环
- 初始化现有变量/对象的新构造函数