cpp中按引用传递和按值传递的区别是什么?

What is the different between pass by reference and pass by value in cpp?

本文关键字:区别 是什么 按值传递 按引用传递 cpp      更新时间:2023-10-16

编译器提示类中没有找到该方法。整个错误信息是找不到函数

common::base::CategoryIdCache::addNewCid(std::string, common::base::eSqlCatalog&, std::vector, std::allocator>, std::allocator, std::allocator>>>&, std::vector, std::allocator>, std::allocator>>>&)

候选人是

common::base::CategoryIdCache::addNewCid(std::string&, common::base::eSqlCatalog&, std::vector, std::allocator>, std::allocator>>>&, std::vector, std::allocator>, std::allocator>>>&)

出现这种情况有以下几个原因:

  • 你要么用不同的签名实现一个函数
  • 或者尝试将临时对象绑定到非const引用

struct X
{
    void foo(std::string& x);
};
//implementation
void X::foo(std::string x); //wrong - different signature

struct X
{
    void foo(std::string& x);
};
//
int main()
{
    X x;
    x.foo("some string");
}

按引用传递意味着将变量的地址(引用)传递给函数,而按值传递意味着复制相关变量在该时刻包含的值,然后将其传递给函数。

你必须以不同的方式声明原型或函数,而不是你传递值的方式