{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/cpp-faq-and-notes/",
  "atlas": "https://www.pystone.net/?node=cpp-faq-and-notes#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/cpp-faq-and-notes.md",
  "context": "https://www.pystone.net/notes/cpp-faq-and-notes.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:cpp-faq-and-notes",
  "slug": "cpp-faq-and-notes",
  "title": "C++知识点与问题",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "C++知识点与问题",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "ef7792611582a4f3086846d923e8a87ac440cef78ef716bbbb351ec4819faa8f",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "C++知识点与问题",
      "anchor": "c知识点与问题",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#c%E7%9F%A5%E8%AF%86%E7%82%B9%E4%B8%8E%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "构造函数不能声明为虚函数",
      "anchor": "构造函数不能声明为虚函数",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%8D%E8%83%BD%E5%A3%B0%E6%98%8E%E4%B8%BA%E8%99%9A%E5%87%BD%E6%95%B0"
    },
    {
      "depth": 2,
      "text": "析构函数最好声明为虚函数",
      "anchor": "析构函数最好声明为虚函数",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0%E6%9C%80%E5%A5%BD%E5%A3%B0%E6%98%8E%E4%B8%BA%E8%99%9A%E5%87%BD%E6%95%B0"
    },
    {
      "depth": 2,
      "text": "函数不能返回局部变量的指针或引用的问题",
      "anchor": "函数不能返回局部变量的指针或引用的问题",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E5%87%BD%E6%95%B0%E4%B8%8D%E8%83%BD%E8%BF%94%E5%9B%9E%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F%E7%9A%84%E6%8C%87%E9%92%88%E6%88%96%E5%BC%95%E7%94%A8%E7%9A%84%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "原因",
      "anchor": "原因",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E5%8E%9F%E5%9B%A0"
    },
    {
      "depth": 2,
      "text": "解决方法",
      "anchor": "解决方法",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95"
    },
    {
      "depth": 3,
      "text": "使用全局数组。使用全局变量时，在程序结束时才释放。",
      "anchor": "使用全局数组使用全局变量时在程序结束时才释放",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E4%BD%BF%E7%94%A8%E5%85%A8%E5%B1%80%E6%95%B0%E7%BB%84%E4%BD%BF%E7%94%A8%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F%E6%97%B6%E5%9C%A8%E7%A8%8B%E5%BA%8F%E7%BB%93%E6%9D%9F%E6%97%B6%E6%89%8D%E9%87%8A%E6%94%BE"
    },
    {
      "depth": 3,
      "text": "使用new/malloc在堆上动态分配内存",
      "anchor": "使用newmalloc在堆上动态分配内存",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E4%BD%BF%E7%94%A8newmalloc%E5%9C%A8%E5%A0%86%E4%B8%8A%E5%8A%A8%E6%80%81%E5%88%86%E9%85%8D%E5%86%85%E5%AD%98"
    },
    {
      "depth": 3,
      "text": "定义为静态类型",
      "anchor": "定义为静态类型",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E5%AE%9A%E4%B9%89%E4%B8%BA%E9%9D%99%E6%80%81%E7%B1%BB%E5%9E%8B"
    },
    {
      "depth": 3,
      "text": "用String类型",
      "anchor": "用string类型",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E7%94%A8string%E7%B1%BB%E5%9E%8B"
    },
    {
      "depth": 3,
      "text": "使用字符串常量",
      "anchor": "使用字符串常量",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E4%BD%BF%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B8%B8%E9%87%8F"
    },
    {
      "depth": 3,
      "text": "在调用函数中定义指针",
      "anchor": "在调用函数中定义指针",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#%E5%9C%A8%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0%E4%B8%AD%E5%AE%9A%E4%B9%89%E6%8C%87%E9%92%88"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/cpp-faq-and-notes/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:programming-languages-and-runtimes",
      "title": "编程语言与运行时",
      "url": "https://www.pystone.net/notes/programming-languages-and-runtimes/",
      "atlas": "https://www.pystone.net/?node=programming-languages-and-runtimes#knowledge-atlas",
      "label": "编程语言与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "C++中的“C++知识点与问题”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#c"
    }
  ],
  "contentMarkdown": "# C++知识点与问题\n\n> 创建时间：2024/1/13 20:30\n\n## 构造函数不能声明为虚函数\n\n1）因为创建一个对象时需要确定对象的类型，而虚函数是在运行时确定其类型的。而在构造一个对象时，由于对象还未创建成功，编译器无法知道对象的实际类型，是类本身还是类的派生类等等\n2）虚函数的调用需要虚函数表指针，而该指针存放在对象的内存空间中；若构造函数声明为虚函数，那么由于对象还未创建，还没有内存空间，更没有虚函数表地址用来调用虚函数即构造函数了\n\n## 析构函数最好声明为虚函数\n\n首先析构函数可以为虚函数，当析构一个指向派生类的基类指针时，最好将基类的析构函数声明为虚函数，否则可以存在内存泄露的问题。\n如果析构函数不被声明成虚函数，则编译器实施静态绑定，在删除指向派生类的基类指针时，只会调用基类的析构函数而不调用派生类析构函数，这样就会造成派生类对象析构不完全。\n\n## 函数不能返回局部变量的指针或引用的问题\n\n## 原因\n\n不要返回函数体内局部变量的地址，因为函数结束时栈会回收，局部变量也随之销毁（如果局部变量为类对象，其析构函数会被自动调用），但可以返回局部变量本身。\n\n错误示例\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n## 解决方法\n\n### 使用全局数组。使用全局变量时，在程序结束时才释放。\n\n### 使用new/malloc在堆上动态分配内存\n\n> 不要忘记了，在使用完后要进行内存的释放，不然会造成内存的泄漏。分别用delete,free(),释放。使用delete时，会调用类的析构函数，而free则不会。\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n### 定义为静态类型\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n### 用String类型\n\n用string实现，是值拷贝!不存在释放内存会影响拷贝的问题。\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n### 使用字符串常量\n\n字符串常量存储在静态存储区域，所以一直都存在\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n### 在调用函数中定义指针\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n## Ref\n\nhttps://blog.csdn.net/xuyunyunaixuexi/article/details/81989201\n"
}
