{"id":53372,"date":"2025-08-12T09:59:56","date_gmt":"2025-08-12T01:59:56","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/53372.html"},"modified":"2025-08-12T09:59:56","modified_gmt":"2025-08-12T01:59:56","slug":"%e3%80%90pimpl%e7%bc%96%e7%a8%8b%e6%8a%80%e5%b7%a7%e3%80%91","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/53372.html","title":{"rendered":"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011"},"content":{"rendered":"<h3>Pimpl\u7f16\u7a0b\u6280\u5de7<\/h3>\n<\/p>\n<h4>\u6df1\u5165\u7406\u89e3 C&#043;&#043; \u7684 Pimpl \u6280\u672f<\/h4>\n<ul>\n<li>\n<ul>\n<li>Pimpl\u7f16\u7a0b\u6280\u5de7<\/li>\n<\/ul>\n<\/li>\n<li>\u4e00\u3001\u4ec0\u4e48\u662f&#096;Pimpl&#096;&#xff1f;<\/li>\n<li>\u4e8c\u3001&#096;Pimpl&#096;\u5e26\u6765\u7684\u597d\u5904?<\/li>\n<li>\u4e09\u3001&#096;example&#096;<\/li>\n<li>\u56db\u3001\u9700\u8981\u6ce8\u610f\u7684&#034;\u5751&#034;<\/li>\n<li>\u4e94\u3001\u603b\u7ed3<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e00\u3001\u4ec0\u4e48\u662fPimpl&#xff1f;<\/h2>\n<p>Pimpl&#xff0c;\u5168\u79f0\u662f Pointer to Implementation&#xff0c;\u5b57\u9762\u610f\u601d\u662f\u201c\u6307\u5411\u5b9e\u73b0\u7684\u6307\u9488\u201d\u3002 \u6838\u5fc3\u601d\u60f3\u662f&#xff1a;\u5373\u628a\u7c7b\u7684\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u5c01\u88c5\u5230\u53e6\u4e00\u4e2a\u7ed3\u6784\u4f53\u6216\u7c7b\u4e2d&#xff0c;\u7136\u540e\u5728\u539f\u59cb\u7c7b\u4e2d\u53ea\u4fdd\u5b58\u4e00\u4e2a\u6307\u5411\u8fd9\u4e2a\u5b9e\u73b0\u7684\u6307\u9488\u3002 \u4e00\u822c\u5c06\u539f\u59cb\u7c7b\u79f0\u4e3ainterface&#xff0c;\u5b9e\u73b0\u7c7b\u79f0\u4e3aimpl&#xff1b;<\/p>\n<h2>\u4e8c\u3001Pimpl\u5e26\u6765\u7684\u597d\u5904?<\/h2>\n<ul>\n<li>\u51cf\u5c11\u7f16\u8bd1\u4f9d\u8d56 \u4fee\u6539 impl \u7684\u6210\u5458\u53d8\u91cf\u6216\u65b9\u6cd5\u4e0d\u4f1a\u5f71\u54cd interface.h&#xff0c;\u56e0\u6b64\u4e0d\u4f1a\u89e6\u53d1\u5305\u542b interface.h \u7684\u6587\u4ef6\u91cd\u65b0\u7f16\u8bd1\u3002<\/li>\n<li>\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282 \u8ba9\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u5b8c\u5168\u5bf9\u5916\u90e8\u9690\u85cf&#xff0c;\u964d\u4f4e\u8026\u5408&#xff0c;\u589e\u5f3a\u5c01\u88c5\u6027\u3002<\/li>\n<li>\u4e8c\u8fdb\u5236\u517c\u5bb9 \u7531\u4e8e\u5934\u6587\u4ef6\u4e0d\u53d8&#xff0c;\u53ef\u4ee5\u5728\u4e0d\u91cd\u65b0\u7f16\u8bd1\u5ba2\u6237\u7aef\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u66f4\u65b0\u5e93\u7684\u5b9e\u73b0&#xff08;\u5c24\u5176\u5728\u5171\u4eab\u5e93\u5f00\u53d1\u4e2d\u6781\u4e3a\u91cd\u8981&#xff09;\u3002<\/li>\n<\/ul>\n<h2>\u4e09\u3001example<\/h2>\n<p>\/\/ interface.hpp<br \/>\n#ifndef INTERFACE_HPP<br \/>\n#define INTERFACE_HPP<\/p>\n<p>class Interface {<br \/>\npublic:<br \/>\n    Interface();<br \/>\n    ~Interface();<\/p>\n<p>    void doSomething();<\/p>\n<p>private:<br \/>\n    struct Impl;    \/\/ \u524d\u5411\u58f0\u660e<br \/>\n    Impl* pImpl;    \/\/ \u6307\u5411\u5b9e\u73b0\u7684\u6307\u9488<br \/>\n};<\/p>\n<p>#endif \/\/ INTERFACE_HPP<\/p>\n<p>\/\/ interface.cpp<br \/>\n#include &#034;interface.hpp&#034;<br \/>\n#include &#034;impl.hpp&#034;<\/p>\n<p>Interface::Interface()<br \/>\n    : pImpl(new Impl()) {}<\/p>\n<p>Interface::~Interface() {<br \/>\n    delete pImpl;<br \/>\n}<\/p>\n<p>void Interface::doSomething() {<br \/>\n    pImpl-&gt;doSomethingImpl();<br \/>\n}<\/p>\n<p>#ifndef IMPL_HPP<br \/>\n#define IMPL_HPP<\/p>\n<p>class Impl {<br \/>\npublic:<br \/>\n    Impl();<br \/>\n    ~Impl();<\/p>\n<p>    void doSomethingImpl();<br \/>\nprivate:<br \/>\n    int data;<br \/>\n};<\/p>\n<p>#endif \/\/ IMPL_HPP<\/p>\n<p>#include &#034;impl.hpp&#034;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>Impl::Impl() : data(42) {}<\/p>\n<p>Impl::~Impl() {}<\/p>\n<p>void Impl::doSomethingImpl() {<br \/>\n    std::cout &lt;&lt; &#034;Doing something with data &#061; &#034; &lt;&lt; data &lt;&lt; std::endl;<br \/>\n}<\/p>\n<h2>\u56db\u3001\u9700\u8981\u6ce8\u610f\u7684&#034;\u5751&#034;<\/h2>\n<ul>\n<li>\u5185\u5b58\u7ba1\u7406 \u9700\u8981\u5728\u6790\u6784\u51fd\u6570\u91cc\u91ca\u653e\u6307\u9488&#xff0c;\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002\u5efa\u8bae\u7528\u667a\u80fd\u6307\u9488&#xff08;\u5982 std::unique_ptr&#xff09;\u7ba1\u7406\u3002<\/li>\n<li>\u989d\u5916\u7684\u95f4\u63a5\u8c03\u7528\u5f00\u9500 \u8bbf\u95ee\u5b9e\u73b0\u65f6\u591a\u4e86\u4e00\u5c42\u6307\u9488\u8df3\u8f6c&#xff0c;\u6027\u80fd\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\u9700\u8981\u6743\u8861\u3002<\/li>\n<li>\u62f7\u8d1d\u4e0e\u8d4b\u503c \u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u4f1a\u5bfc\u81f4\u6d45\u62f7\u8d1d&#xff0c;\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u6df1\u62f7\u8d1d\u6216\u7981\u7528\u3002<\/li>\n<\/ul>\n<h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<p>Pimpl \u662f\u4e00\u79cd\u975e\u5e38\u5b9e\u7528\u7684 C&#043;&#043; \u7f16\u7a0b\u6280\u5de7&#xff0c;\u7279\u522b\u9002\u5408\u4e2d\u5927\u578b\u9879\u76ee&#xff0c;\u80fd\u6709\u6548\u63a7\u5236\u7f16\u8bd1\u65f6\u95f4\u3001\u63d0\u5347\u4ee3\u7801\u5c01\u88c5\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb178\u6b21\uff0c\u70b9\u8d5e4\u6b21\uff0c\u6536\u85cf3\u6b21\u3002pimpl\u7f16\u7a0b\u6280\u5de7<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190,188],"topic":[],"class_list":["post-53372","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190","tag-188"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/53372.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb178\u6b21\uff0c\u70b9\u8d5e4\u6b21\uff0c\u6536\u85cf3\u6b21\u3002pimpl\u7f16\u7a0b\u6280\u5de7\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/53372.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-12T01:59:56+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53372.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/53372.html\",\"name\":\"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-12T01:59:56+00:00\",\"dateModified\":\"2025-08-12T01:59:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/53372.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/53372.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/53372.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/53372.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb178\u6b21\uff0c\u70b9\u8d5e4\u6b21\uff0c\u6536\u85cf3\u6b21\u3002pimpl\u7f16\u7a0b\u6280\u5de7","og_url":"https:\/\/www.wsisp.com\/helps\/53372.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-12T01:59:56+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/53372.html","url":"https:\/\/www.wsisp.com\/helps\/53372.html","name":"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-12T01:59:56+00:00","dateModified":"2025-08-12T01:59:56+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/53372.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/53372.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/53372.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Pimpl\u7f16\u7a0b\u6280\u5de7\u3011"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=53372"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/53372\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=53372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=53372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=53372"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=53372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}