{"id":21515699,"url":"https://github.com/goblinhack/c-plus-plus-move-constructor-example","last_synced_at":"2026-03-19T20:41:49.141Z","repository":{"id":138350335,"uuid":"263719900","full_name":"goblinhack/c-plus-plus-move-constructor-example","owner":"goblinhack","description":"c++ move and copy constructor example with a custom vector class","archived":false,"fork":false,"pushed_at":"2020-05-13T19:15:57.000Z","size":1,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-11T18:26:13.495Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goblinhack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-05-13T19:06:11.000Z","updated_at":"2023-05-04T11:49:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"ee3714af-7e65-4850-a7bf-6767c84bf02d","html_url":"https://github.com/goblinhack/c-plus-plus-move-constructor-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/goblinhack/c-plus-plus-move-constructor-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goblinhack%2Fc-plus-plus-move-constructor-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goblinhack%2Fc-plus-plus-move-constructor-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goblinhack%2Fc-plus-plus-move-constructor-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goblinhack%2Fc-plus-plus-move-constructor-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goblinhack","download_url":"https://codeload.github.com/goblinhack/c-plus-plus-move-constructor-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goblinhack%2Fc-plus-plus-move-constructor-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28947696,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T14:26:55.697Z","status":"ssl_error","status_checked_at":"2026-01-31T14:26:52.545Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T23:56:26.919Z","updated_at":"2026-01-31T16:35:38.235Z","avatar_url":"https://github.com/goblinhack.png","language":null,"readme":"Example use of the move and copy constructors with a custom class\n=================================================================\n\nExpected output:\n```\n c: [10][11]\n copy ctor called\n copy of c: [10][11]\n move ctor called\n moved c: [10][11]\n```\n\nCompile as:\n```\n  g++ -std=c++2a -O2 -Wall -pedantic foo.cpp\n```\n\nCode:\n```C++\n#include \u003ciostream\u003e\n#include \u003calgorithm\u003e\n\ntemplate\u003cclass T\u003e class MyVector {\nprivate:\n    T *data;\n    size_t maxlen;\n    size_t currlen;\npublic:\n    MyVector\u003cT\u003e () : data (nullptr), maxlen(0), currlen(0) { }\n    MyVector\u003cT\u003e (int maxlen) : data (new T [maxlen]), maxlen(maxlen), currlen(0) { }\n\n    MyVector\u003cT\u003e (const MyVector\u0026 o) {\n        std::cout \u003c\u003c \"copy ctor called\" \u003c\u003c std::endl;\n        data = new T [o.maxlen];\n        maxlen = o.maxlen;\n        currlen = o.currlen;\n        std::copy(o.data, o.data + o.maxlen, data);\n    }\n\n    MyVector\u003cT\u003e (const MyVector\u003cT\u003e\u0026\u0026 o) {\n        std::cout \u003c\u003c \"move ctor called\" \u003c\u003c std::endl;\n        data = o.data;\n        maxlen = o.maxlen;\n        currlen = o.currlen;\n    }\n\n    void push_back (const T\u0026 i) {\n        if (currlen \u003e= maxlen) {\n            maxlen *= 2;\n            auto newdata = new T [maxlen];\n            std::copy(data, data + currlen, newdata);\n            if (data) {\n                delete[] data;\n            }\n            data = newdata;\n        }\n        data[currlen++] = i;\n    }\n\n    friend std::ostream\u0026 operator\u003c\u003c(std::ostream \u0026os, const MyVector\u003cT\u003e\u0026 o) {\n        auto s = o.data;\n        auto e = o.data + o.currlen;;\n        while (s \u003c e) {\n            os \u003c\u003c \"[\" \u003c\u003c *s \u003c\u003c \"]\";\n            s++;\n        }\n        return os;\n    }\n};\n\nint main() {\n    auto c = new MyVector\u003cint\u003e(1);\n    c-\u003epush_back(10);\n    c-\u003epush_back(11);\n    std::cout \u003c\u003c \"c: \" \u003c\u003c *c \u003c\u003c std::endl;\n    auto d = *c;\n    std::cout \u003c\u003c \"copy of c: \" \u003c\u003c d \u003c\u003c std::endl;\n    auto e = std::move(*c);\n    delete c;\n    std::cout \u003c\u003c \"moved c: \" \u003c\u003c e \u003c\u003c std::endl;\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoblinhack%2Fc-plus-plus-move-constructor-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoblinhack%2Fc-plus-plus-move-constructor-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoblinhack%2Fc-plus-plus-move-constructor-example/lists"}