復習C++:値伝達と参照伝達の違い

4600 ワード

説明の詳細は、ソースコードを参照してください.https://github.com/ZZMarquis/cxxstu common.hppソースコード:
#ifndef _CXX_STU_BASIC_COMMON_HPP_
#define _CXX_STU_BASIC_COMMON_HPP_

#include 


#define PRINT_FUNC_LINE() std::cout << __FUNCTION__ << ":" << __LINE__ << std::endl

namespace cxxstu {
    class Foo
    {
    public:
        Foo();
        Foo(const std::string name);
        Foo(const Foo &f);
        Foo& operator=(const Foo &f);
        ~Foo();

        void Rename(const std::string &name);
        void PrintName();
        void Reset();

    private:
        std::string name;
    };

    Foo::Foo()
    {
        Reset();
        std::cout << "This is Foo Constructor, name:" << name.c_str() << std::endl;
    }

    Foo::Foo(const std::string name)
    {
        this->name = name;
        std::cout << "This is Foo Constructor, name:" << name.c_str() << std::endl;
    }

    Foo::Foo(const Foo & f)
    {
        this->name = f.name;
        std::cout << "This is Foo Copy Constructor, name:" << name.c_str() << std::endl;
    }

    Foo & Foo::operator=(const Foo & f)
    {
        this->name = f.name;
        std::cout << "This is Foo Assign Constructor, name:" << name.c_str() << std::endl;
        return *this;
    }

    Foo::~Foo()
    {
        std::cout << "This is Foo Destructor, name:" << name.c_str() << std::endl;
    }

    inline void Foo::Rename(const std::string &name)
    {
        this->name = name;
    }

    inline void Foo::PrintName()
    {
        std::cout << "name:" << name.c_str() << std::endl;
    }

    inline void Foo::Reset()
    {
        this->name = std::string("HelloWorld");
    }
}

#endif // !_CXX_STU_BASIC_COMMON_HPP_

parameter.cppソースコード:
#include "common.hpp"

using namespace cxxstu;

void PassRef(const Foo &f)
{
    //       ,        ,            (       ),
    //  const                         ,     。
    PRINT_FUNC_LINE();
}

void PassValue(const Foo f)
{
    //       ,            ,             ,
    //                  ,  ,             。
    PRINT_FUNC_LINE();
}

void PassValueAndModify(Foo f)
{
    //                ,  ,               ,                。
    PRINT_FUNC_LINE();
    f.PrintName();
    f.Rename(__FUNCTION__);
    std::cout << "Rename in " << __FUNCTION__ << std::endl;
}

void PassRefAndModify(Foo &f)
{
    //                     ,                             。
    PRINT_FUNC_LINE();
    f.PrintName();
    f.Rename(__FUNCTION__);
    std::cout << "Rename in " << __FUNCTION__ << std::endl;
}

void PassRefAndAssign(Foo &f)
{
    //            ,                       ?
    //                    ,         ,            1,
    //                    ,         。
    PRINT_FUNC_LINE();
    f.PrintName();
    Foo newF(__FUNCTION__);
    f = newF;
    std::cout << "new Foo and assign it to out parameter in " << __FUNCTION__ << std::endl;
}

void PassValueAndAssign(Foo f)
{
    //            ,                      ?
    //                  ,                      。
    PRINT_FUNC_LINE();
    f.PrintName();
    Foo newF(__FUNCTION__);
    f = newF;
    std::cout << "new Foo and assign it to out parameter in " << __FUNCTION__ << std::endl;
}

int main(int argc, char **argv)
{
    Foo f;

    std::cout << "=====================================" << std::endl;

    f.Reset();
    PassRef(f);
    std::cout << "=====================================" << std::endl;

    f.Reset();
    PassValue(f);
    std::cout << "=====================================" << std::endl;

    f.Reset();
    PassValueAndModify(f);
    f.PrintName();
    std::cout << "=====================================" << std::endl;

    f.Reset();
    PassRefAndModify(f);
    f.PrintName();
    std::cout << "=====================================" << std::endl;

    f.Reset();
    PassRefAndAssign(f);
    f.PrintName();
    std::cout << "=====================================" << std::endl;

    f.Reset();
    PassValueAndAssign(f);
    f.PrintName();
    std::cout << "=====================================" << std::endl;

    getchar();
    return 0;
}