c++ 有没有办法获得const对象的地址

a1o7rhls  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(114)

我正在努力实现以下目标

#include<iostream>                                                                             
#include <vector>                                                                              
                                                                                               
class var {                                                                                    
public:                                                                                        
    static std::vector<var*> variables_;                                                       
                                                                                               
    friend var operator-(const var& v) {                                                       
        // this is not compiling                                                               
        variables_.push_back(&v);                                                              
    }                                                                                          
};                                                                                             
                                                                                               
int main() { var x; }

错误

est.cpp: In function ‘var operator-(const var&)’:
test.cpp:10:32: error: no matching function for call to ‘push_back(const var*)’
   10 |         variables_.push_back(&v);
      |                                ^
In file included from /usr/include/c++/10/vector:67,
                 from test.cpp:2:
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = var*; _Alloc = std::allocator<var*>; std::vector<_Tp, _Alloc>::value_type = var*]’ (near match)
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1187:7: note:   conversion of argument 1 would be ill-formed:
test.cpp:10:30: error: invalid conversion from ‘const var*’ to ‘std::vector<var*>::value_type’ {aka ‘var*’} [-fpermissive]
   10 |         variables_.push_back(&v);
      |                              ^~
      |                              |
      |                              const var*
In file included from /usr/include/c++/10/vector:67,
                 from test.cpp:2:
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = var*; _Alloc = std::allocator<var*>; std::vector<_Tp, _Alloc>::value_type = var*]’ (near match)
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1203:7: note:   conversion of argument 1 would be ill-formed:
test.cpp:10:30: error: invalid conversion from ‘const var*’ to ‘std::vector<var*>::value_type’ {aka ‘var*’} [-fpermissive]
   10 |         variables_.push_back(&v);
      |                              ^~
      |                              |
      |                              const var*
test.cpp:11:5: warning: no return statement in function returning non-void [-Wreturn-type]
   11 |     }
      |     ^

我在这个网站上读过几篇文章,告诉我为什么这个方法行不通。其中之一是向量的元素必须是可复制赋值的。所以我明白为什么,但我不知道如何绕过它。
一定有办法的。

rhfm7lfc

rhfm7lfc1#

取一个const var&,但向量存储的是var*(没有const),要么向量需要存储指向常量的指针

static std::vector<const var*> variables_;

或者您的operator-需要采用非常数基准电压源

friend var operator-(var& v) { ... }

如果您从未计划通过variables_向量修改var,则应该执行第一件事,否则应该执行第二件事。

相关问题