H
Hoyin
I am reading an notes about Pointer To Implementation (pImpl) Idiom
which is trying to decouple the interface and impl via a pointer e.g.
class Book
{
public:
void print(){m_p->print()};
private:
class BookImpl;
BookImpl* m_p;
};
class BookImpl // in a separated file
{
public:
void print();
private:
std::string m_Contents;
std::string m_Title;
};
which avoid re-compile on the client code if we want to add/remove
property for Book (we change class BookImpl's data member rather than
class Book), BUT, in most case, the obj of BookImpl will be allocated
in Heap rather than Stack. Will there be significant cost introduced
by this change (we will be accessing the data from Heap instead of
Stack)? I was told accessing data from Heap is less efficient than
from Stack (even thought we have pointers/address to those memory) but
why? How we can measures the cost introduced? thanks
Hoyin
which is trying to decouple the interface and impl via a pointer e.g.
class Book
{
public:
void print(){m_p->print()};
private:
class BookImpl;
BookImpl* m_p;
};
class BookImpl // in a separated file
{
public:
void print();
private:
std::string m_Contents;
std::string m_Title;
};
which avoid re-compile on the client code if we want to add/remove
property for Book (we change class BookImpl's data member rather than
class Book), BUT, in most case, the obj of BookImpl will be allocated
in Heap rather than Stack. Will there be significant cost introduced
by this change (we will be accessing the data from Heap instead of
Stack)? I was told accessing data from Heap is less efficient than
from Stack (even thought we have pointers/address to those memory) but
why? How we can measures the cost introduced? thanks
Hoyin