例如:
shared_ptr<const shared_ptr<const int> > pp;
是相当令人生畏的......
const int ^ const ^ pp;
立刻让人想起原始指针等效
const int * const * pp;
我没有看到为什么这些没有为它们被模板化的类型的普通旧指针赋值操作符重载的原因.如果使智能指针的接口尽可能接近普通的旧指针,那么为什么它们不像这样对赋值运算符进行重载?
inline std::shared_ptr<type> &operator=( const type * pointer)
{
reset(a);
}
Run Code Online (Sandbox Code Playgroud)
这样你可以像使用普通指针一样使用它们,如下所示:
std::shared_ptr<int> test = new int;
Run Code Online (Sandbox Code Playgroud)
它根本不是一个问题,只是想知道为什么他们遇到了只是让一些运营商超载的麻烦.
还想知道是否有一种方法来重载全局赋值运算符来执行此操作,或者是否有任何原因我不应该这样做.
编辑:在此处为Nawaz添加关于代码格式的回答.我刚刚编写了这个测试程序,看看你说的是对的:
template<class T>
class peh
{
public:
peh() {meh = 3;}
const peh<T> & operator=(const int * peh)
{
}
};
void f( peh<int> teh)
{
}
int main()
{
int * meh = new int;
f(meh);
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个错误在这里说出来也从没有可用的转换peh<int>来int *.那么,为什么它可以接受std::shared_ptr<int>到int *?
我正在尝试使用std::unique_ptr删除器.这是我的代码:
template<class T>
struct Deleter
{
void operator()(T* p)
{
delete[] p;
}
};
void Test()
{
vector<unique_ptr<char>> v;
for(size_t i = 0; i < 5; ++i)
{
char* p = new char[10];
sprintf(p, "string %d", i);
v.push_back( unique_ptr<char, Deleter<char>>(p) ); // error is here
}
}
Run Code Online (Sandbox Code Playgroud)
错误C2664:'void std :: vector <_Ty> :: push_back(std :: unique_ptr &&)':无法将参数1从'std :: unique_ptr <_Ty,_Dx>'转换为'std :: unique_ptr <_Ty> && "
编译器:VC++ 2012.我该如何解决这个问题?我的目标是使用unique_ptr自定义删除器调用delete[]而不是默认值delete.
我正在尝试删除旧应用程序的所有删除和删除[],而是使用智能指针.在下面的代码片段中,我想删除cicle的最后一个.
std::unique_ptr<MapiFileDesc> fileDesc(new MapiFileDesc[numFiles]);
for (int i = 0; i < numFiles; ++i)
{
// Works but I've to delete[] at the end
fileDesc[i].lpszPathName = new CHAR[MAX_PATH];
// Does not work. For each iteration the previous array will be deleted
// It also happens with shared_array
boost::scoped_array<CHAR> pathName(new CHAR[MAX_PATH]);
fileDesc[i].lpszPathName = pathName.get();
}
// I want to remove the following cicle
for (int i = 0; i < numFiles; ++i)
{
delete [] fileDesc[i].lpszPathName;
fileDesc[i].lpszPathName = nullptr;
}
Run Code Online (Sandbox Code Playgroud)
您认为这种情况的最佳方法是什么:使用包装器对象来跟踪所有创建的数组并在析构函数中删除它们或使用boost :: …
以下是我正在阅读的C++书中的一些代码,展示了如何创建自己的智能指针:
template <typename T>
class SuperSmartPointer
{
public:
explicit SuperSmartPointer(T* inPtr);
virtual ~SuperSmartPointer();
SuperSmartPointer(const SuperSmartPointer<T>& src);
SuperSmartPointer<T>& operator=(const SuperSmartPointer<T>& rhs);
const T& operator*() const;
const T* operator->() const;
T& operator*();
T* operator->();
operator void*() const { return mPtr; }
protected:
T* mPtr;
static std::map<T*, int> sRefCountMap;
void finalizePointer();
void initPointer(T* inPtr);
};
template <typename T>
std::map<T*, int> SuperSmartPointer<T>::sRefCountMap;
template <typename T>
SuperSmartPointer<T>::SuperSmartPointer(T* inPtr)
{
initPointer(inPtr);
}
template <typename T> SuperSmartPointer<T>::SuperSmartPointer(const SuperSmartPointer<T>& src)
{
initPointer(src.mPtr);
}
template <typename T>
SuperSmartPointer<T>& SuperSmartPointer<T>::operator=(const …Run Code Online (Sandbox Code Playgroud) sf::RenderWindow::getPosition()0, 0当我从创建窗口的同一范围调用它时,总是返回而不是正确的位置.
以下是重现问题的示例代码:
#include <iostream>
#include <memory>
#include <SFML/Graphics.hpp>
int main() {
std::unique_ptr<sf::RenderWindow> window_;
window_ = std::unique_ptr<sf::RenderWindow>(
new sf::RenderWindow(sf::VideoMode(800, 600), "asd", sf::Style::Default)
);
window_->setFramerateLimit(30);
window_->setVerticalSyncEnabled(false);
while (window_->isOpen()) {
sf::Event evt;
while (window_->pollEvent(evt)) {
if (evt.type == sf::Event::Closed) { window_->close(); }
}
window_->clear();
window_->display();
}
const sf::Vector2i wpos = window_->getPosition();
std::cout << "window position: " << wpos.x << "/" << wpos.y <<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是:我有一个MainMenu我传递的类sf::RenderWindow *(带window_.get()).
当我打电话window_->getPosition()给MainMenu它报告正确的位置时,所以getPosition() …
我有这样的代码:
class A
{
public:
A(void);
~A(void)
{
delete b;
delete c;
delete d;
// ...
}
private:
B* b;
C* c;
D* d;
// ...
};
//A.cpp
A(void) : b(new B()), c(new C()), d(new D()) //...
{
}
Run Code Online (Sandbox Code Playgroud)
类A取得所有权在自己的对象b,c,d...什么是保持这些对象的最佳方式?我猜,std::unique_ptr<B/C/D>类型的使用将适合这种方式.例如,它允许不关心析构函数的仔细编写.
下面的代码将导致内存丢失,因为rA在构造时被初始化为无效.我该怎么办才能解决这个问题?
使用shared_ptr或希望将来的编译器版本能够捕获这个错误的代码?
#include <memory>
using namespace std;
struct A {};
void use(const A& a) {};
unique_ptr<A> foo()
{
unique_ptr<A> pa(new A());
return pa;
}
int main()
{
const A& rA = *foo(); // rA is unusable, initialized with invalid reference (invalidated by destruction of temporary unique_ptr returned from foo)
use(rA);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过在其文档的帮助下创建小程序来学习libuv.请注意,我将通过c++语言使用它,而不是c.这是我开始的 -
#include <iostream>
#include <uv.h>
int main() {
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
uv_loop_init(loop);
std::cout << "Running loop" << std::endl;
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
free(loop);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并编译它 - g++ -std=c++14 -luv main.cpp应该a.out作为输出文件,但它失败并出现错误 -
无效转换为'void*'到'uv_loop_t*{aka uv_loop_s*}
这指向malloc的使用.我通过用旧的libuv方法替换那些行来确认它 -
uv_loop_t* loop = uv_loop_new();
...
uv_loop_delete(loop);
Run Code Online (Sandbox Code Playgroud)
这不应该工作,但令人惊讶的是工作和编译在这里很好.
但我认为不是躲避这个问题,而是应该用c ++中的好方法解决这个问题.所以在这里我要求c ++中的一个很好的替代品(可能没有malloc /手动内存管理),所以我可以继续前进.
为了在C++ 14中初始化唯一指针的向量,我可以考虑以下两种方式.哪种方法更好?
#include<memory>
#include<vector>
using namespace std;
int main(){
const int N = 10000000;
{//first method
vector<unique_ptr<int>> vec(N);
for(auto it=vec.begin(); it!=vec.end();++it){
auto ptr = make_unique<int>();
*it = std::move(ptr);
}
}
{//second method
vector<unique_ptr<int>> vec;
for(int i=0; i<N; i++){
vec.push_back(make_unique<int>());
}
}
}
Run Code Online (Sandbox Code Playgroud) c++ ×10
smart-pointers ×10
unique-ptr ×5
pointers ×4
shared-ptr ×3
c++11 ×2
c++14 ×1
libuv ×1
raii ×1
sfml ×1
vector ×1
visual-c++ ×1