函数中的C ++指针 [英] C++ Pointer in Function

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

问题描述

从技术上讲,我知道以下三种方法都是有效的,但是是否有任何合乎逻辑的理由采用另一种方法呢?我的意思是,c ++中的许多东西在技术上都是有效的",但这并没有使它们变得更加愚蠢.

I know that technically all three ways below are valid, but is there any logical reason to do it one way or the other? I mean, lots of things in c++ are "technically valid" but that doesn't make them any less foolish.

int* someFunction(int* input)
{
    // code
}  

int *someFunction(int *input)
{
    // code
}

int * someFunction(int * input)
{
    // code
}

我个人认为第三种方法很烦人,但是有没有一种正确"的方法呢?我通常更倾向于使用第一个(因为第二个看起来更像是被用作解除引用运算符-并非如此)

I personally think the third one is annoying, but is there a "correct" way? I am typically more inclined to use the first one (as the second looks more like it's being used as the dereference operator - which it isn't)

推荐答案

所有内容都是等效的.选择最适合您的口味.只要确定您选择的是哪种,就可以在每种情况下都应用该选择.您的星星和花括号的位置远比每次都将它们放在同一位置要重要得多.

All are equivalent. Choose the flavor that suits you best. Just be sure whichever you chose, you apply that choice in every case. Where your stars and curly braces go is far less important than putting them in the same place every time.

我个人更喜欢int* someFunction(int* input);,但是谁在乎呢?

Personally, I prefer int* someFunction(int* input);, but who cares?

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

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