假设我有一个相当大的类Matrix,我已经重载operator==以检查是否相等如下:
bool operator==(Matrix &a, Matrix &b);
Run Code Online (Sandbox Code Playgroud)
当然,我通过引用传递Matrix对象,因为它们太大了.
现在我有一个Matrix::inverse()返回新Matrix对象的方法.现在我想在比较中直接使用逆,如下所示:
if (a.inverse()==b) { ... }`
Run Code Online (Sandbox Code Playgroud)
问题是,这意味着inverse方法需要返回对Matrix对象的引用.两个问题:
由于我只是在这次比较中使用该引用,这是内存泄漏吗?
如果在inverse()方法中返回的对象属于boost :: shared_ptr,会发生什么?方法退出后,shared_ptr将被销毁,对象不再有效.有没有办法返回对属于shared_ptr的对象的引用?
例如:
shared_ptr<const shared_ptr<const int> > pp;
是相当令人生畏的......
const int ^ const ^ pp;
立刻让人想起原始指针等效
const int * const * pp;
我有一个基本多态类(使用虚方法)并从中派生出来.我想使用以下代码
boost::shared_ptr<base_class> ptr( new derived_class() );
Run Code Online (Sandbox Code Playgroud)
但编译器返回以下错误
cannot convert ‘fpga_northwest*’ to ‘fpga*’ in initialization
make: *** [../obj/ixecute_cmd_interface.o] Error 1
Run Code Online (Sandbox Code Playgroud)
看看周围我很想使用以下构建正常,但我有一些疑问.你认为这是正确的吗?
boost::shared_ptr<base_class> ptr_base;
boost::shared_ptr<derived_class> ptr_derived( new derived_class() );
ptr_base = boost::dynamic_pointer_cast<base_class>( ptr_derived );
Run Code Online (Sandbox Code Playgroud)
如果我使用boost::static_pointer_cast我有编译器错误; 既然我从一个衍生到一个基础的铸造应该不是更正确的一个static_cast?
谢谢你的帮助
我得到了一个抽象基类"Parent",它带有一个纯虚方法和一个实现这个方法的子类"Child"和一个成员"value".我将子类的对象实例化为shared_ptr,作为动态绑定的一种方式.我在这里使用shared_ptr而不是引用,因为我将这些对象存储在std :: vector中.
现在我想比较源代码底部定义的两个对象"someObject"和"anotherObject".因此,我在相应的Child类中覆盖了==运算符.然而,只调用shared_ptr的==运算符.我可以对后面的动态绑定对象进行比较吗?
/*
* Parent.h
*/
class Parent{
public:
virtual ~Parent(){};
virtual void someFunction() = 0;
};
/*
* Child.h
*/
class Child : public Base{
private:
short value;
public:
Child(short value);
virtual ~Child();
bool operator==(const Child &other) const;
void someFunction();
};
/*
* Child.cpp
*/
#include "Child.h"
Child::Child(short value):value(value){}
Child::~Child() {}
void Child::someFunction(){...}
bool Child::operator==(const Child &other) const {
if(this->value==other.value){
return true;
}
return false;
}
/*
* Some Method
*/
std::shared_ptr<Parent> someObject(new Child(3));
std::shared_ptr<Parent> anotherObject(new Child(4)); …Run Code Online (Sandbox Code Playgroud) 我开始学习shared_ptr和weak_ptr.理论上一切看起来都很简单 但是当我开始测试时,嗯...我有这个非常简单的程序:
#include <iostream>
#include <memory>
using namespace std;
class Second
{
public:
Second()
{
cout << "Second created" << endl;
}
~Second()
{
cout << "Second deleted" << endl;
}
};
class Main
{
public:
shared_ptr<Second> second;
Main()
{
cout << "Main created" << endl;
second = make_shared<Second>(*(new Second()));
}
~Main()
{
second.reset();
cout << "Main deleted" << endl;
}
};
void fun()
{
shared_ptr<Main> main = make_shared<Main>(*(new Main()));
}
int main()
{
cout << "Program started" << endl; …Run Code Online (Sandbox Code Playgroud) 这是我第一次在C++中使用智能指针.我有一些问题std::shared_ptr:
通过引用设置指针:
MyToy mytoy_1, mytoy_2;
set_mytoy(mytoy_1, some_data);
set_mytoy(mytoy_2, some_data);
shared_ptr<MyToy> ptr_mytoy(&mytoy_1);
Run Code Online (Sandbox Code Playgroud)重置和新任务:
ptr_mytoy.reset(&mytoy_2);
Run Code Online (Sandbox Code Playgroud)没有分配重置:
ptr_mytoy.reset();
Run Code Online (Sandbox Code Playgroud)设置NULL(?):
ptr_mytoy(nullptr);
Run Code Online (Sandbox Code Playgroud)这些例子对吗?
如何检查智能指针是否为"空"(例如,之后.reset())或是否NULL?
我正在研究shared_ptr和enable_shared_from_this的boost文档,我无法弄清楚enable_shared_from_this的实际用途.
以下是我对enable_shared_from_this的理解(示例复制自`enable_shared_from_this`的有用性是什么?).
class Y: public enable_shared_from_this<Y>
{
public:
Y(): count(3){}
shared_ptr<Y> f()
{
return shared_from_this();
}
int count;
}
int main()
{
shared_ptr<Y> p(new Y);
shared_ptr<Y> q = p->f();
cout << "\n Count from P is " << p1->count;
cout << "\n Count from q is " << q1->count;
}
Run Code Online (Sandbox Code Playgroud)
所以,现在我们有一个共享指针q,它指向p(new Y)所拥有的同一个对象,当p和q超出范围时,对象被销毁.上面的两个打印语句都将打印3作为计数.
现在,通过执行以下操作,我可以在不使用enable_shared_from_this的情况下实现相同的功能.
class Y
{
public:
Y(): count(3){}
int count;
}
int main()
{
shared_ptr<Y> p(new Y);
shared_ptr<Y> q(p);
cout << "\n Count from P is " …Run Code Online (Sandbox Code Playgroud) 我对指针很缺乏经验,而且我遇到了简单指针与a之间差异的问题std::shared_ptr.我想使用一个,shared_ptr所以当没有任何指向时,我不必非常小心地删除该对象.
我正在使用我正在使用的库/标题(easylogging ++).我不认为它是外部库的问题,但我使用指针.该库有一个函数,它返回一个指向对象的简单指针.我总是把简单的指针转换成shared_ptr麻烦发生的地方.
// Works fine---but I want a shared_ptr
Object* MyInstance(ReturnPointerToObject(...));
// Compiles fine, but crashes during deallocation of the Object (Seg fault?)
std::shared_ptr<Object> MyInstance(ReturnPointerToObject(...));
Run Code Online (Sandbox Code Playgroud)
当程序结束时释放内容时,我的程序崩溃了.
问题:
shared_ptr这样的好主意还是坏主意?我正在尝试在Raspberry Pi上编译代码w/shared_ptrs:
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
cout << *message1 <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
test.cpp: In function 'int main(int, char**)': test.cpp:4:4: error: 'shared_ptr' was not declared in this scope
shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
^ test.cpp:4:21: error: expected primary-expression before '>' token
shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
^ test.cpp:4:70: error: 'message1' was not declared in this scope
shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
Run Code Online (Sandbox Code Playgroud)
我用这个命令编译:g++ -std=c++11 …
说我喜欢上课
class A {
public: // I know, I know...
shared_ptr<string> aString;
};
Run Code Online (Sandbox Code Playgroud)
我必须要像这样的析构函数
~A() {
aString.reset();
}
Run Code Online (Sandbox Code Playgroud)
确保所有权得到妥善保释?我做了一些测试,似乎我没有 - 也就是说,当A的实例超出范围或被删除(或重置,如果我通过shared_ptr引用它)时,字符串也会被删除(我用一个更复杂的例子来证实这一点).但是,这可能是我正在使用的编译器特有的实现(clang-700.0.72).
我的问题是:总是如此,或者明确重置那些实例更好,就像删除任何其他哑指针一样?
shared-ptr ×10
c++ ×9
c++11 ×5
boost ×2
casting ×1
destructor ×1
dynamic ×1
pointers ×1
reference ×1
std ×1