const 类方法是否阻止在类外部分配变量?

Do const class methods prevent assigning variables outside of the class?

本文关键字:外部 分配 变量 类方法 是否 const      更新时间:2023-10-16

我已经解决了通过删除"const"来编译此代码的问题。但是,为什么在这种特定情况下,我似乎无法在 const 类方法中分配非类成员?它可能是类的一部分;虽然,我不明白为什么。

我得到了我的代码来编译,但我对这种情况感到困惑。

下面是类中的一些声明。

using twoDList = multimap<string,string>;
twoDList SomeMultiMap;

当我取下"康斯特"时,这将起作用。或者至少编译。不过,这里我只是分配仅在此函数中声明的迭代器。顺便说一下,使用命名空间标准。

bool object::foo(string a, string b) const
{
pair<object::twoDList::iterator,object::twoDList::iterator> wordRange;
wordRange = SomeMultiMap.equal_range(a);
object::twoDList::iterator it = wordRange.first;
//...
//...
//...
}

我希望这个函数在不删除 const 的情况下编译,但它没有编译。

编辑:这是编译错误,我在 linux 命令行上使用它。

g++ -g -DDEBUG -std=c++11 -c test1.cpp
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from test1.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _U2 = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _T1 = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _T2 = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >]’:
test1.cpp:15:15:   required from here
/usr/include/c++/4.8/bits/stl_pair.h:188:10: error: no match for ‘operator=’ (operand types are ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ and ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’)
first = std::forward<_U1>(__p.first);
^
/usr/include/c++/4.8/bits/stl_pair.h:188:10: note: candidates are:
In file included from /usr/include/c++/4.8/map:60:0,
from test1.cpp:2:
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&)
struct _Rb_tree_iterator
^
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note:   no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&’
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&)
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note:   no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&’
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from test1.cpp:1:
/usr/include/c++/4.8/bits/stl_pair.h:189:11: error: no match for ‘operator=’ (operand types are ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ and ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’)
second = std::forward<_U2>(__p.second);
^
/usr/include/c++/4.8/bits/stl_pair.h:189:11: note: candidates are:
In file included from /usr/include/c++/4.8/map:60:0,
from test1.cpp:2:
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&)
struct _Rb_tree_iterator
^
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note:   no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&’
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&)
/usr/include/c++/4.8/bits/stl_tree.h:157:12: note:   no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >’ to ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&’
makefile:10: recipe for target 'test1.o' failed
make: *** [test1.o] Error 1

编辑:更多编辑以修复范围运算符。另外,添加了我正在使用命名空间 std 的事实,如果有帮助的话。

成员函数参数列表后面的const是承诺不通过this指针更改类对象。this的类型变得const ClassType*而不仅仅是ClassType*。 (像任何指向常量的指针或对常量的引用一样,这仅意味着不能使用该指针或引用更改事物,而不是对象是永久的常量或不能通过其他方式更改。

说一个类对象被视为const意味着它的所有成员(任何标记的mutable除外)都被视为const。 由于在成员函数定义中,纯非静态成员名实际上Nthis->N相同,因此当函数声明在参数列表后具有const时,以这种方式命名的成员被视为const

所以在bool object::foo(string a, string b) const内部,作为object成员的SomeMultiMap这个名字,被当作const SomeMultiMapequal_rangemultimap对象表达式const时给出pair<const_iterator, const_iterator>,或者仅在对象表达式未const时给出pair<iterator, iterator>

所以这可能会起作用(取决于你之后需要做什么):

std::pair<twoDList::const_iterator,twoDList::const_iterator> wordRange;
wordRange = SomeMultiMap.equal_range(a);
twoDList::const_iterator it = wordRange.first;

(请注意,您通常不需要object::前缀,因为成员函数体在其类的范围内。

尽管我只会用auto来定义这些变量,但避免了完全正确获取类型的需要,并节省了键入那些长迭代器名称的时间。

auto wordRange = SomeMultiMap.equal_range(a);
auto it = wordRange.first;