c ++是否将指针传递给函数

时间:2021-10-04 18:52:30

what is the difference when i do function call in c++ to object method with pointer and not from this :

当我用c ++调用函数调用带有指针的对象方法时,有什么区别,而不是这个:

void Main::Init()
{
    Foo foo;
    Obj obj;
    Obj.someMethod(foo)  // signeture : someMethod(Foo f)
}

to this:

void Main::Init()
{
    Foo* foo = new foo();
    Obj obj;
    Obj.someMethod(foo)  // signeture : someMethod(Foo* f)
}

UPDATE
or this :

更新或此:

 void Main::Init()
    {
        Foo* foo  = new foo();
        Obj obj;
        Obj.someMethod(foo)  // signeture : someMethod(Foo *& f)
    }

what is better in term's of memory and compiler what is more recommended in practical i don't see any differences in both i get what i need out of Foo

什么是更好的内存和编译器什么是实际更推荐我没有看到任何差异我得到我需要的Foo

2 个解决方案

#1


2  

someMethod(Foo f)

f is an object passed by instance, i.e. f becomes a copy of the actual parameter foo passed into the function. Any modifications done to f by someMethod wouldn't change foo.

f是实例传递的对象,即f成为传递给函数的实际参数foo的副本。 someMethod对f进行的任何修改都不会改变foo。

Typically, it would be more efficient to pass f by a const reference: someMethod(const Foo& f). Thus, copying would be avoided and a call of a non-const method of f in someMethod would cause a compiler error.

通常,通过const引用传递f会更有效:someMethod(const Foo&f)。因此,将避免复制,并且在someMethod中调用f的非const方法将导致编译器错误。

someMethod(Foo *f)

f is a pointer to an object. Provided that the actual parameter foo is initialized (unlike your example), any modifications made by someMethod to the object pointed by f would actually apply to the object pointed by foo.

f是指向对象的指针。假设实际参数foo已初始化(与您的示例不同),someMethod对f指向的对象所做的任何修改实际上都将应用于foo指向的对象。

someMethod(Foo *& f)

f is a reference to a pointer to an object. This is usually used to declare an output parameter of a pointer type. Example:

f是对指向对象的指针的引用。这通常用于声明指针类型的输出参数。例:

void someMethod(Foo *& f)
{
  f = new Foo();
}

As f is a reference to the actual parameter Foo* foo, the result of new Foo is assigned to foo on the function exit.

由于f是对实际参数Foo * foo的引用,因此在函数出口处将新Foo的结果赋给foo。

#2


2  

Firstly that code doesn't compile.

首先,代码不能编译。

In the second snippet, foo is a pointer, and obj is an object. You're invoking foo.someMethod(obj) when its expecting a Foo* object. Same with the first snippet as well.

在第二个片段中,foo是一个指针,obj是一个对象。当你期待一个Foo *对象时,你正在调用foo.someMethod(obj)。与第一个片段相同。

From what I see you meant:

从我看到你的意思:

obj.someMethod(foo);

Explanation:

in the first snippet, foo is an object. The object is passed as a COPY to someMethod function.(call by value). Changes made to foo in someMethod will not reflect in the foo of Main::Init().

在第一个片段中,foo是一个对象。该对象作为COPY传递给someMethod函数。(按值调用)。在someMethod中对foo所做的更改不会反映在Main :: Init()的foo中。

In the second snippet, foo is sent as a pointer to object. Its called "call by reference". The address of foo will be sent to someMethod so that it can modify the object or do something with it

在第二个片段中,foo作为指向对象的指针发送。它被称为“参考呼叫”。 foo的地址将被发送到someMethod,以便它可以修改对象或对它做一些事情

#1


2  

someMethod(Foo f)

f is an object passed by instance, i.e. f becomes a copy of the actual parameter foo passed into the function. Any modifications done to f by someMethod wouldn't change foo.

f是实例传递的对象,即f成为传递给函数的实际参数foo的副本。 someMethod对f进行的任何修改都不会改变foo。

Typically, it would be more efficient to pass f by a const reference: someMethod(const Foo& f). Thus, copying would be avoided and a call of a non-const method of f in someMethod would cause a compiler error.

通常,通过const引用传递f会更有效:someMethod(const Foo&f)。因此,将避免复制,并且在someMethod中调用f的非const方法将导致编译器错误。

someMethod(Foo *f)

f is a pointer to an object. Provided that the actual parameter foo is initialized (unlike your example), any modifications made by someMethod to the object pointed by f would actually apply to the object pointed by foo.

f是指向对象的指针。假设实际参数foo已初始化(与您的示例不同),someMethod对f指向的对象所做的任何修改实际上都将应用于foo指向的对象。

someMethod(Foo *& f)

f is a reference to a pointer to an object. This is usually used to declare an output parameter of a pointer type. Example:

f是对指向对象的指针的引用。这通常用于声明指针类型的输出参数。例:

void someMethod(Foo *& f)
{
  f = new Foo();
}

As f is a reference to the actual parameter Foo* foo, the result of new Foo is assigned to foo on the function exit.

由于f是对实际参数Foo * foo的引用,因此在函数出口处将新Foo的结果赋给foo。

#2


2  

Firstly that code doesn't compile.

首先,代码不能编译。

In the second snippet, foo is a pointer, and obj is an object. You're invoking foo.someMethod(obj) when its expecting a Foo* object. Same with the first snippet as well.

在第二个片段中,foo是一个指针,obj是一个对象。当你期待一个Foo *对象时,你正在调用foo.someMethod(obj)。与第一个片段相同。

From what I see you meant:

从我看到你的意思:

obj.someMethod(foo);

Explanation:

in the first snippet, foo is an object. The object is passed as a COPY to someMethod function.(call by value). Changes made to foo in someMethod will not reflect in the foo of Main::Init().

在第一个片段中,foo是一个对象。该对象作为COPY传递给someMethod函数。(按值调用)。在someMethod中对foo所做的更改不会反映在Main :: Init()的foo中。

In the second snippet, foo is sent as a pointer to object. Its called "call by reference". The address of foo will be sent to someMethod so that it can modify the object or do something with it

在第二个片段中,foo作为指向对象的指针发送。它被称为“参考呼叫”。 foo的地址将被发送到someMethod,以便它可以修改对象或对它做一些事情