c++ - 理解 C++ 中指向 const 值的 const 指针

标签 c++ list pointers reference constants

我有两个问题。

首先,我对指向 const 值的 const 指针的理解有点问题。我不明白为什么 B::insert 有效,而 C::insert 导致编译器错误。我的意思是 C 中的列表不正好等于 C::insert 的参数吗?

我的第二个问题是 A const * const a 是否也可以写成 const A& a

class A
{
    //Do stuff
};

class B 
{
private:
    list<A const *> l;

public:
    void insert(A const * const a)
    {
        l.push_back(a);
    }
};

class C 
{
private:
    list<A const * const> l;

public:
    void insert(A const * const a)
    {
            l.push_back(a);
    }
};

编辑(编译错误):

g++ -Wall  -c  -O2 "sonnensystem.cpp" -std=c++11 (im Verzeichnis: C:\Users\Kenan\Desktop\OPR\cppcode\Konzepte\Kapselung\Architektur\sonnensystem01)
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32/bits/c++allocator.h:33:0,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/allocator.h:46,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/string:41,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/ios_base.h:41,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:42,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
             from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
             from sonnensystem.cpp:1:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ext/new_allocator.h: In instantiation of 'struct __gnu_cxx::new_allocator<const A* const>':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/allocator.h:92:11:   required from 'class std::allocator<const A* const>'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_list.h:315:9:   required from 'class std::__cxx11::_List_base<const A* const, std::allocator<const A* const> >'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_list.h:507:11:   required from 'class std::__cxx11::list<const A* const>'
sonnensystem.cpp:28:27:   required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ext/new_allocator.h:93:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const A* const; __gnu_cxx::new_allocator<_Tp>::const_pointer = const A* const*; __gnu_cxx::new_allocator<_Tp>::const_reference = const A* const&]' cannot be overloaded
       address(const_reference __x) const _GLIBCXX_NOEXCEPT
       ^
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ext/new_allocator.h:89:7: error: with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = const A* const; __gnu_cxx::new_allocator<_Tp>::pointer = const A* const*; __gnu_cxx::new_allocator<_Tp>::reference = const A* const&]'
       address(reference __x) const _GLIBCXX_NOEXCEPT
       ^
Kompilierung fehlgeschlagen.

最佳答案

在你的声明 A const * const 中,第一个 const 表示 A * 指针指向一个不能被改变了(一个 const 指针)。第二个 const 表示该指针的值无法更改,就像 const int 无法更改一样。由于 list(和其他标准容器)要求其成员是可分配的,因此它们不能是常量值。

对于你的第二个问题,a A const * constconst A& 是相似的,但不能互换,因为你使用它们的方式不同。

关于c++ - 理解 C++ 中指向 const 值的 const 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039478/

相关文章:

C 传递指针

c++ - 动态地将顶点发送到着色器? (没有 glGenBuffers)Opengl

c - 链表循环

c++ - wxWidgets - wxListbook 列表的宽度

c - 在 C 语言中,是否可以像在 char 数组声明中初始化字符串一样在指针声明中初始化字符串?

c - 什么情况下数组会衰减为指针?

c++ - Isapi 过滤器到 apache 模块

c++ - 需要帮助修复段错误(核心转储)

C++ STL 列表函数使用空列表进行段错误

c# - List <T> 仅当它是所有元素中的最小值时才添加值