#include "utils/LinkedList.h"
#include "utils/RefCount.h"
#include <iostream>
#include "utils/testobject.h"
int Object::nextId = 0;
int main(void) {
::Utils::LinkedList<::Utils::RefCountedPtr<Object>> list;
list.append(new Object());
auto reference = list[0];
const auto other = list[0];
//both IDE and GCC see below line as error:
//with: error: passing ‘const Object’ as ‘this’ argument of ‘void Object::setThing(int)’ discards qualifiers [-fpermissive]
//other->setThing(3);
auto test = other;
//this is fine - IT SHOULD NOT BE
test->setThing(5);
::std::cout<<"Id: "<<other->getId()<<"\n";
}
有没有产生警告,存在用于裁判计数指针一个构造函数一个const REF计数PTR(拷贝构造) - 裁判计数是可变的。我仍然期待警告。
(正如你不能有常量构造我会假设常量声明是一个正常的拷贝,然后处理结果为const - 还是我期望的警告)
这是我的第一个步骤冒险使用自动(我通常使用自动完成),没有裸指针,我会使用标准库的,但这是更多的练习,如果有的话,我想习惯使用特征和其他类型的实用程序
预计:
自动工作,它正确得到(我鼠标在我的IDE和事实的东西作品确认编译器正在做我期望的)前两个,也就是说reference
和other
工作正常。
它没有(也没有我的IDE)得到test
正确的,那就是它丢弃const
和(正如你所看到的),我可以使用“setThing”,我不应该这样做。
附录
复制构造:
RefCountedPtr(const RefCountedPtr& from) {
refCount = from.refCount;
(*refCount)++;
data = from.data;
}
你的问题是什么?您没有SSCCE示例。 –
这听起来像你的IDE是错的,你的编译器是正确的。 – juanchopanza
@juanchopanza既没有为'auto test = other;'行产生警告,但都认识到'other'是const,但'test'不是。 –