私には2つの質問があります。C++のconst値へのconstポインタの理解
まず、const値のconstポインタの理解に少し問題があります。私はなぜB::insert
が働くのか理解しません。一方、C::insert
はコンパイラエラーに繋がります。私は、C
のリストが正確にC::insert
のパラメータと同じではないことを意味しますか?
私の2番目の質問は、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);
}
};
編集(コンパイルエラー):あなたの宣言A const * const
で
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.
コンパイラエラーを含めると便利です。私のコンパイラにはエラーはありませんでした。 – Eljay
@Eljay私の編集を参照してください。私はこのオンラインコンパイラ[リンク](http://cpp.sh/)も試しました。/ –
なぜ[C++ 11では 'vector'?](https://stackoverflow.com/questions/6954906/does-c11-allow-vectorconst-t)を許可していますか?コンパイラは 'アドレス'について不平を言う。 –