简体   繁体   English

C ++通过引用传递类中的函数

[英]C++ pass a function in a class by reference

I'm trying to pass a reference to a function in a class but am having trouble figuring out how to do it. 我正在尝试传递对类中函数的引用,但我很难弄清楚如何执行它。 So say I have a class test defined as such 所以说我有一个类定义test

#include <iostream>

class test {
public:
  test () {};
  ~test () {};

  void setA (int);
  int getA (void);

private:
  int a;

};

void test::setA (int A) { a = A; }

int test::getA (void) { return a; }

using namespace std;

int main ()
{
  test T;
  T.setA(5);

  cout << "a = " << T.getA() << endl;

  return 0;
}

That works fine but if I want to pass the values by reference 这工作正常,但如果我想通过引用传递值

#include <iostream>

class test {
public:
  test () {};
  ~test () {};

  void setA (int);
  int & getA (void);

private:
  int a;

};

void test::setA (int & A) { a = A; }

int & test::getA (void) { return a; }

using namespace std;

int main ()
{
  test T;
  T.setA(5);

  cout << "a = " << T.getA() << endl;

  return 0;
}

I cannot figure out how to configure setA to pass by reference. 我无法弄清楚如何配置setA以通过引用传递。

There are two issues with the code. 代码有两个问题。 First, the definition of setA does not match the declaration. 首先,setA的定义与声明不匹配。 You must make the declaration take in a reference as a parameter. 您必须将声明作为参数引用。

Change this: 改变这个:

void setA (int);

To this: 对此:

void setA (int&);

The second issue is that you are trying to pass an r-value (5) as a reference. 第二个问题是您尝试传递r值(5)作为参考。 You must pass in an l-value. 您必须传入l值。 You can do that by creating an int first and then passing that by reference: 你可以先创建一个int,然后通过引用传递它:

int i = 5;
T.setA(i);

Full example: 完整示例:

#include <iostream>

class test {
public:
  test () {};
  ~test () {};

  void setA (int&);
  int & getA (void);

private:
  int a;

};

void test::setA (int & A) { a = A; }

int & test::getA (void) { return a; }

using namespace std;

int main ()
{
  test T;

  int i = 5;


  T.setA(i);

  cout << "a = " << T.getA() << endl;

  return 0;
}

When you pass something by reference to a function in C++, the function does not keep the parameter in memory automatically. 当您通过引用C ++中的函数传递某些内容时,该函数不会自动将参数保留在内存中。 Thus, you have to declare it before so that it stays in memory throughout the entire function. 因此,您必须先声明它,以便它在整个函数中保留在内存中。

The 5 you tried to pass as a reference would go out of scope and get destroyed as soon as the function starts. 你试图作为参考传递的5将超出范围并在函数启动时立即销毁。 The declared i variable is instead destroyed at the end of the main function. 声明的i变量在main函数的末尾被销毁。

The reason is because in order to pass by reference, you must have an lvalue , which is a fancy way of saying something that persists beyond a single use. 原因是因为为了通过引用传递,你必须有一个左值 ,这是一种奇特的方式,可以说一些持续超出一次使用的东西。

If you created an int variable, you would be able to pass it in by reference. 如果您创建了一个int变量,则可以通过引用传入它。 In the code above, you attempted to pass in a raw integer value (5), which fails, since the compiler is expecting a reference to an int, not a raw integer value. 在上面的代码中,您尝试传入一个原始整数值(5),该值失败,因为编译器期望引用int,而不是原始整数值。

The following code would work: 以下代码可以工作:

int main ()
{
  test T;
  int myVariable = 4; // Need an actual variable to pass by reference.
  T.setA(myVariable);

  cout << "a = " << T.getA() << endl;

  return 0;
}

However, if you want your function to take raw integer values like you showed in your second example, you must have a function definition like your first example, where all the function takes is an integer. 但是,如果您希望函数采用您在第二个示例中显示的原始整数值,则必须具有类似于第一个示例的函数定义,其中所有函数都是整数。 Hope this helps! 希望这可以帮助!

Maybe you could try this: 也许你可以试试这个:

#include <iostream>

class test {
public:
    test() {};
    ~test() {};

    void setA(int&&); // requires at least C++11
    void setA(int&);
    int & getA(void);

private:
    int a;

};

void test::setA(int && A) { a = A; }
void test::setA(int&A) { a = A; }
int & test::getA(void) { return a; }

using namespace std;

int main()
{
    test T;

    int i = 5;


    T.setA(i);

    cout << "a = " << T.getA() << endl;

    T.setA(8);

    cout << "a = " << T.getA() << endl;

    return 0;
}

In the example, int& passes a l-value while int&& passes a r-value as a reference. 在示例中, int&传递l值,而int&&传递r值作为引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM