如何将const Class*转换为Class* ?
How would I convert a const Class* to a Class*?
我试图插入一些东西到链表,但编译器告诉我,我不能从const Student*
转换到Student*
。每个节点包含一个Student *stud
和一个Node *next
。这是我目前所写的函数:
void LinkedList::putAtTail(const Student &student){
Node *p = new Node();
p->stud = &student; //this is where I have trouble
p->next - NULL;
//then insert `p` into the Linked List
}
编译器不想编译这个,给我error: invalid conversion from ‘const Student*’ to ‘Student*’
。
我该如何解决这个问题,而不改变我的putAtTail(const Student &student)
函数的参数?
如何将const Class*转换为Class*?
选项1:
复制一份
p->stud = new Student(student);
选项2:
使用const_cast
.
p->stud = const_cast<Student*>(&student);
仅在仔细管理内存时使用此选项
相关文章:
- 无法将'Class::operator=='从"布尔"类型(类::)(类*)转换为"布尔"类型
- 'return':无法从'const class myClass'转换为'class myClass &'
- VS2017模板专用化错误无法从'Class *(__cdecl *)(Args...)'转换为'Class *(__cdecl *)(Args...)'
- "class<T>"到"class<const T>"转换运算符的惯用方法
- 通过在每个元素上调用名称(),将类的向量转换为clangtype(class :: name())的向量
- 没有从'Class *'到'Class' C++的可行转换
- 将 BOUNDED std::参数列表<class>转换为类型 std::tuple<class,class,class> tup<classObj1, classObj
- 无法将"member pointer to derived class"转换为"member pointer to base class"
- 无法将参数 1 从 'Class *' 转换为'double []'
- 如何在C ++文件中使用".class"或".jar" Java文件(无需转换)?
- 如何将以下"class reference"机制从德尔福转换为C++11?
- 在没有c风格强制转换的情况下,将DWORD_PTR强制转换为class,反之亦然
- 无法将此指针从 const Class<T> 转换为 Class<T>&
- 是否可以使作用域枚举("enum class")在上下文中可转换为布尔值?
- C++ 代码错误(非法成员初始化、'class'类型重定义、'return':无法转换)
- 将' class '指针强制转换为' float '数组:它是如何工作的?
- 如何从type (Class)转换为std:string
- 不能将int {Class}::*转换为int*
- 如何将char*转换为Class类型?
- 如何将const Class*转换为Class* ?