您可以为单个命令设置变量,如下所示:
MY_VARIABLE=my_value ./my_script.sh
Run Code Online (Sandbox Code Playgroud)
你可以交给另一个这样的脚本:
exec ./my_script.sh
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这样做时:
exec MY_VARIABLE=my_value ./my_script.sh
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
exec: MY_VARIABLE=my_value: not found
Run Code Online (Sandbox Code Playgroud)
这是为什么?
有没有办法做到这一点?
我有一个派生自boost :: enable_shared_from_this的基类,然后是另一个派生自基类和boost :: enable_shared_from_this的类:
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
class A : public enable_shared_from_this<A> { };
class B : public A , public enable_shared_from_this<B> {
public:
using enable_shared_from_this<B>::shared_from_this;
};
int main() {
shared_ptr<B> b = shared_ptr<B>(new B());
shared_ptr<B> b_ = b->shared_from_this();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这编译但在运行时它给出
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr
Aborted
Run Code Online (Sandbox Code Playgroud)
造成这种情况的原因是什么,有什么办法吗?
编辑:
如果我需要这样的东西怎么办:
class A : public enable_shared_from_this<A> { };
class B : public enable_shared_from_this<B> { };
class C : …Run Code Online (Sandbox Code Playgroud) c++ boost multiple-inheritance shared-ptr enable-shared-from-this
让我们说我有一个基类和派生类,以及一个带有指向基类的stl向量的函数:
class A { public: int x; };
class B : public A { };
void foo(const vector<A*> &va) {
for (vector<A*>::const_iterator it = va.begin(); it < va.end(); it++)
cout << (*it)->x << endl;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将指针列表传递给派生类?即:
vector<B*> vb;
// ... add pointers to vb ...
foo(vb);
Run Code Online (Sandbox Code Playgroud)
以上将导致以下编译器错误:
error: could not convert ‘vb’ from ‘std::vector<B*>’ to ‘std::vector<A*>’
Run Code Online (Sandbox Code Playgroud)
即使B*可转换为A*.
最后,如果有一个普通指针的解决方案,它是否也适用于boost共享指针?