小编Mag*_*nRa的帖子

使用带有成员函数指针的std :: shared_ptr

使用C指针,我可以像这样做一些事情:

#include <iostream>
#include <memory>
class Foo {
     void bar(){
     std::cout<<"Hello from Foo::bar \n";
        }
   } 

void main(){
    Foo foo; 
    Foo* foo_ptr=&foo;
    std::shrared_ptr<Foo> foo_sptr(&foo);
    void (Foo::*bar_ptr)()=&Foo::bar;
    (foo.*bar_ptr)();
    (foo_ptr->*bar_ptr)();
    //(foo_sptr->*bar_ptr)(); // does not compile for me
Run Code Online (Sandbox Code Playgroud)

如果我想使用smart_ptr而不是C指针,我会收到编译错误:

error: no operator "->*" matches these operands
        operand types are: std::shared_ptr<Foo> ->* void (Foo::*)()
(foo_sptr->*bar_ptr)();
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个工作没有std :: shared_ptr :: get()?

c++ pointers

4
推荐指数
1
解决办法
2384
查看次数

C++ std:vector <T*const>

我必须继续一个程序.我之前的程序员经常使用这个结构:

std:vector< T* const>
Run Code Online (Sandbox Code Playgroud)

他在Visual Studio C++ 2010中编写了ist,并且能够编译它.我正在使用g ++,它会引发一些编译错误.

    g++ -g -Wall -c -std=c++11 -pedantic -I/usr/include/SuperLU/ src/Cell.cpp -o obj/Cell.o
In file included from src/Cell.cpp:13:0:
src/Cell.h:81:2: warning: extra ';' [-pedantic]
In file included from /usr/include/x86_64-linux-gnu/c++/4.7/./bits/c++allocator.h:34:0,
                 from /usr/include/c++/4.7/bits/allocator.h:48,
                 from /usr/include/c++/4.7/string:43,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from src/Cell.cpp:2:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of 'struct __gnu_cxx::new_allocator<BattPackage::Leg* const>':
/usr/include/c++/4.7/bits/allocator.h:89:11:   required from 'class std::allocator<BattPackage::Leg* const>'
/usr/include/c++/4.7/bits/alloc_traits.h:92:43:   required from 'struct std::allocator_traits<std::allocator<BattPackage::Leg* const> >'
/usr/include/c++/4.7/ext/alloc_traits.h:110:10:   required from 'struct __gnu_cxx::__alloc_traits<std::allocator<BattPackage::Leg* const> >'
/usr/include/c++/4.7/bits/stl_vector.h:76:28:   required from …
Run Code Online (Sandbox Code Playgroud)

c++ pointers const vector

2
推荐指数
1
解决办法
1975
查看次数

标签 统计

c++ ×2

pointers ×2

const ×1

vector ×1