C ++传递参数引用和指针 [英] c++ passing arguments by reference and pointer

查看:279
本文介绍了C ++传递参数引用和指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++

class bar
{
    int i;
    char b;
    float d;
};

void foo ( bar arg );
void foo ( bar &arg );
void foo ( bar *arg );

这是一个简单的类/结构和功能的结果
我有一些QS

this is a sample class/struct and functions
i have some Qs


  • 什么是1日和传递论点'ASM',大小,速度的第二种方法之间的区别?

  • 的参数如何传递到每个案件的职能美孚(以指针的情况下,我知道指针压入堆栈)

  • 在(速度,大小,preferability),这是更好的传递参数​​,在效率方面什么时候?

  • 什么是对应每个传递参数的方式英特尔ASM'语法?

我知道什么是最有发言权的样,如果我们谈论的是旧的CPU或编译器,但什么是它不会对现代编译器和CPU重要吗?

i know what most say about "it doesn't matter on modern compilers and CPUs" but what if we're talking about Old CPUs or compilers?

在此先感谢

推荐答案

指针和参考方法应该是相当具有可比性(无论是在速度,内存使用和产生的code)。

The pointer and the reference methods should be quite comparable (both in speed, memory usage and generated code).

传递一个类直接强制编译器重复记忆,并把对象的副本在堆栈中。更糟的是,在C ++中有全部排序与此相关的肮脏位(默认的拷贝构造函数和诸如此类的东西)的。

Passing a class directly forces the compiler to duplicate memory and put a copy of the bar object on the stack. What's worse, in C++ there are all sort of nasty bits (the default copy constructor and whatnot) associated with this.

在C语言中,我总是用(可能是常数)指针。在C ++中,你应该可能会使用引用。

In C I always use (possibly const) pointers. In C++ you should likely use references.

这篇关于C ++传递参数引用和指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆