相关疑难解决方法(0)

传递参考与传递值之间有什么区别?

有什么区别

  1. 通过引用传递的参数
  2. 一个由值传递的参数?

你能给我一些例子吗?

language-agnostic pass-by-reference pass-by-value

539
推荐指数
11
解决办法
65万
查看次数

在C++中通过引用/值传递

我想澄清价值和参考之间的差异.

我画了一张照片

在此输入图像描述

所以,为了通过价值,

使用不同的引用创建相同对象的副本,并为本地变量分配新引用,以指向新副本

如何理解单词:"如果函数修改了该值,则修改也出现在调用函数的范围内,用于传递值和引用"

谢谢!

c++ pass-by-reference pass-by-value

35
推荐指数
5
解决办法
10万
查看次数

将可修改的参数传递给c ++函数

提供,我想将一个可修改的参数传递给一个函数,我应该选择什么:通过指针传递它还是通过引用传递它?

  1. bool GetFoo(Foo&whereToPlaceResult);
  2. bool GetFoo(Foo*whereToPlaceResult);

我问这个是因为我一直认为通过引用(1)传递参数是最好的做法,但在检查了一些本地代码数据库后,我得出结论,最常见的方法是(2).此外,该男子本人(Bjarne Stroustrup)建议使用(2).(1)和(2)的[dis]优点是什么,还是仅仅是个人品味的问题?

c++ parameters pointers reference

7
推荐指数
2
解决办法
2324
查看次数

c ++改变函数的变量参数

我想将我作为参数传递的变量更改为此函数:

bool verifyStudent(string id, string name, int grade, int points, string type) {
if(!verifyId(id)){
    cerr << "Please enter 8 charactes id! format: YYMMDDCC\n";
    cin >> id;
    return false;
} else
if(!verifyName(name)){
    cerr << "Please enter name to 35 characters!\n";
    cin >> name;
    return false;
} else
if(!verifyGrade(grade)){
    cerr << "Please enter class between 8 and 12!\n";
    cin >> grade;
    return false;
} else
if(!verifyPoints(points)){
    cerr << "Please enter points between 0 and 300!\n";
    cin >> points;
    return false;
} else …
Run Code Online (Sandbox Code Playgroud)

c++ variables arguments function

2
推荐指数
1
解决办法
7272
查看次数

将char指针传递给另一个函数:空值.C++

我有以下非常简单的程序.

int modify(char * v){
  v = "123" ;

  return 0;
}

int main (int argc, char** argv){
char *test = new char[10];
  modify(test);
  std::cout << test;
  return 0;

}
Run Code Online (Sandbox Code Playgroud)

我知道我可以打印出"123",但我故意以这种方式编写它来了解指针是如何工作的.但是,不打印"123".我应该如何正确传递指针?

c++ pointers

1
推荐指数
1
解决办法
411
查看次数

重载预增量运算符未显示正确的结果

我使用友元函数重载了预增量运算符.在重载的friend函数中,变量的值显示正确.但是这个值没有显示在显示功能中,为什么呢?

#include <iostream>
using namespace std;

class Rectangle {
public:
    int breadth;

public:
    void read();
    void display();
    friend void operator ++(Rectangle r1);
};
void Rectangle::read()
{
    cout << "Enter the breadth of the Rectangle: ";
    cin >> breadth;
}
void operator++(Rectangle r1)
{
    ++r1.breadth;
    cout<<r1.breadth<<endl; //correct result
}
void Rectangle::display()
{
    cout<<breadth<<endl; // not showing pre-incremented value, why ???
}
int main()
{
    cout<<"Unary Operator using Friend Function \n";
    Rectangle r1;
    r1.read();
    ++r1;
    cout << "\n breadth of Rectangle after …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading unary-operator friend-function

0
推荐指数
1
解决办法
449
查看次数