【问题标题】:Passing an object to a function taking pointer as parameter将对象传递给以指针为参数的函数
【发布时间】:2010-10-06 16:01:01
【问题描述】:

当我只有一个对象但我的函数需要一个指向该类型对象的指针时,它是如何工作的。

Someoclass test1;

SomeFunction( Someclass*)
{
    //does something
};

【问题讨论】:

    标签: c++ pointers function reference


    【解决方案1】:

    你传递对象的地址。

    SomeFunction(&test1);
    

    【讨论】:

    • & 用于两件事,参考和地址?
    • @Apoc: '&' 有 3 个含义: & 按位与 运算符,&reference 类型,& 运算符的地址
    【解决方案2】:

    调用函数需要使用运算符(&)的地址。所以在你的情况下:

    Someclass test1;
    
    SomeFunction(&test1);
    

    如果您要问指针是如何工作的,there's a detailed explanation here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 2021-12-13
      • 2019-08-17
      • 2014-03-31
      • 2017-03-17
      相关资源
      最近更新 更多