Pimpl成语没有动态内存分配 [英] Pimpl idiom without dynamic memory allocation
问题描述
你好!
我刚刚发现了一种使用私有实现习惯用法的方法
(pimpl),没有动态内存分配的开销。对于那些不知道这是什么的人来说,维基百科有一篇很好的文章,你可以阅读
。无论如何,我发现如果你让
实现类中的所有成员都可变,你实际上可以使用这个成语,而不需要任何
不必要。内存分配。以下是该方法的最小示例:
//在类的标题中名为Line
#include< string>
class line
{
public:
Line(const std: :string& name);
const std :: string& GetName()const;
void SetName(const std :: string& s);
private:
//私有实现习语:
//所有成员变量都隐藏在这个类中
class LineImpl;
const LineImpl& m_pimpl; //通常是非常量指针
};
//并在您的实现文件中:
#include" Line.h"
//这里我们用成员变量定义类
class Line :: LineImpl
{
public:
LineImpl(const std :: string& s):m_s(s){}
//所有方法都需要是const
const std :: string& GetName()const {return m_s; }
void SetName(const std :: string& s)const {m_s = s; }
私人:
mutable std :: string m_s; //诀窍!所有成员都是可变的
};
//创建pimpl实例而不使用新的
Line :: Line(const std :: string& s):m_pimpl(LineImpl(s)){}
//将所有成员函数转发给私有实现
const std ::串和放大器; Line :: GetName()const
{
返回m_pimpl.GetName();
}
void Line :: SetName(const std :: string& s)
{
m_pimpl.SetName(s);
}
好的专家,您有什么想法?这种方法牺牲了一些额外的速度来保证
const-correctness。值得吗?
-
Daniel
Hello!
I have just discovered a way to use the private implementation idiom
(pimpl), without the overhead of dynamic memory allocation. For those of
you who don''t know what this is, Wikipedia has a nice article you can
read. Anyway, I discovered that if you make all members in the
implementation class mutable, you can in fact use this idiom without any
"unnecessary" memory allocation. Here''s a minimal example of the method:
// In the header of your class called Line
#include <string>
class Line
{
public:
Line(const std::string& name);
const std::string& GetName() const;
void SetName(const std::string& s);
private:
// Private implementation idiom:
// all member variables are hidden in this class
class LineImpl;
const LineImpl& m_pimpl; // normally a non-const pointer
};
// and in your implementation file:
#include "Line.h"
// Here we define the class with the member variables
class Line::LineImpl
{
public:
LineImpl(const std::string& s) : m_s(s) {}
// all methods need to be const here
const std::string& GetName() const { return m_s; }
void SetName(const std::string& s) const { m_s = s; }
private:
mutable std::string m_s; // the trick! all members are mutable
};
// create the pimpl instance without using new
Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}
// forward all member functions to the private implementation
const std::string& Line::GetName() const
{
return m_pimpl.GetName();
}
void Line::SetName(const std::string& s)
{
m_pimpl.SetName(s);
}
Ok experts, what do you all think? This method sacrifies
const-correctness for some extra speed. Is it worth it?
--
Daniel
推荐答案
Daniel Lidstr?maécrit:
Daniel Lidstr?m a écrit :
你好!
我刚刚发现了一种使用私有实现习惯用法的方法
(pimpl),没有动态内存分配的开销。对于那些不知道这是什么的人来说,维基百科有一篇很好的文章,你可以阅读
。无论如何,我发现如果你让
实现类中的所有成员都可变,你实际上可以使用这个成语,而不需要任何
不必要。内存分配。以下是该方法的最小示例:
//在类的标题中名为Line
#include< string>
class line
{
public:
Line(const std: :string& name);
const std :: string& GetName()const;
void SetName(const std :: string& s);
private:
//私有实现习语:
//所有成员变量都隐藏在这个类中
class LineImpl;
const LineImpl& m_pimpl; //通常是非常量指针
};
//并在您的实现文件中:
#include" Line.h"
//这里我们用成员变量定义类
Hello!
I have just discovered a way to use the private implementation idiom
(pimpl), without the overhead of dynamic memory allocation. For those of
you who don''t know what this is, Wikipedia has a nice article you can
read. Anyway, I discovered that if you make all members in the
implementation class mutable, you can in fact use this idiom without any
"unnecessary" memory allocation. Here''s a minimal example of the method:
// In the header of your class called Line
#include <string>
class Line
{
public:
Line(const std::string& name);
const std::string& GetName() const;
void SetName(const std::string& s);
private:
// Private implementation idiom:
// all member variables are hidden in this class
class LineImpl;
const LineImpl& m_pimpl; // normally a non-const pointer
};
// and in your implementation file:
#include "Line.h"
// Here we define the class with the member variables
[snip]
[snip]
//所有方法都需要为const
// all methods need to be const here
[snip]
[snip]
mutable std :: string m_s; //诀窍!所有成员都是可变的
mutable std::string m_s; // the trick! all members are mutable
这意味着你将代码强制转换为编译。这就是全部。
Which mean you coerce the code into compilation. That''s all.
//创建pimpl实例而不使用新的
Line :: Line(const std :: string& s):m_pimpl(LineImpl(s)){}
// create the pimpl instance without using new
Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}
当超出范围时,本地会被销毁。不是吗?
Your local is destroyed when going out of scope. Doesn''t it ?
[snip]
好的专家,您有什么想法?这种方法牺牲了一些额外的速度来保证
const-correctness。这值得么?
[snip]
Ok experts, what do you all think? This method sacrifies
const-correctness for some extra speed. Is it worth it?
不是真的。
当然不值得晃来晃去参考。
Michael
Not really.
And certainly not worth a dangling reference.
Michael
文章< 47 *********************** @ news.free.fr>,
Michael DOUBEZ< mi ************ @ free.frwrote:
In article <47***********************@news.free.fr>,
Michael DOUBEZ <mi************@free.frwrote:
Daniel Lidstr?maécrit:
Daniel Lidstr?m a écrit :
//创建pimpl实例而不使用新的
Line :: Line(const std :: string& s):m_pimpl(LineImpl(s) ){}
// create the pimpl instance without using new
Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}
超出范围时,本地会被销毁。不是吗?
Your local is destroyed when going out of scope. Doesn''t it ?
不,不是。实际上可以将临时对象绑定到const
引用。不会有悬空。参考。
-
Daniel
No it isn''t. It is actually ok to bind a temporary object to a const
reference. There will be no "dangling" reference.
--
Daniel
Daniel Lidstr?maécrit:
Daniel Lidstr?m a écrit :
文章< 47 *********************** @ news.free.fr>,
Michael DOUBEZ< mi ************ @ free.frwrote:
In article <47***********************@news.free.fr>,
Michael DOUBEZ <mi************@free.frwrote:
> Daniel Lidstr?ma écrit:
>Daniel Lidstr?m a écrit :
>> //创建pimpl实例而不使用新的
Line :: Line(const std :: string& s):m_pimpl (LineImpl(s)){}
>>// create the pimpl instance without using new
Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}
当超出范围时,本地会被销毁。不是吗?
Your local is destroyed when going out of scope. Doesn''t it ?
不,不是。实际上可以将临时对象绑定到const
引用。不会有悬空。参考。
No it isn''t. It is actually ok to bind a temporary object to a const
reference. There will be no "dangling" reference.
可以绑定它,但这并不意味着临时
的生命周期延长。
示例:
const std :: string& foo()
{
返回std :: string(" bar");
}
foo()返回的值是一个悬空参考。
Michael
It is ok to bind it but that doesn''t mean the lifetime of the temporary
is extended.
Example:
const std::string& foo()
{
return std::string("bar");
}
The value returned by foo() is an dangling reference.
Michael
这篇关于Pimpl成语没有动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!