简体   繁体   English

从具有引用返回类型的函数返回什么?

[英]What to return from a function with reference return type?

className &temp (someOtherclassName &c)

The question is what should I return I am trying to return just an object that is newly created in the class, it's not working, I also tried returning reference or pointer, and it's not working. 问题是我应该返回什么?我试图仅返回在类中新创建的对象,它不起作用,我还尝试返回引用或指针,但它不起作用。

Klub &novKlub(Clen &c){
    Klub newKlub;
    strcpy(newKlub.name, name);

    for(int i = 0; i < elements; i++){
        if(c.getLevel() == clenovi->getLevel()){
            newKlub += clenovi[i];

        }

    }
    return newKlub;
}

Here's all the code: 这是所有代码:

#include<iostream>
#include<cstring>
using namespace std;
class Clen{
private:
char name[50];
char surname[50];
int level;
public:
Clen(char n[] = "null", char s[] = "null", int l = 1){
    strcpy(name, n);
    strcpy(surname, s);
    level = l;
}

//operator++
Clen operator++(int){
    Clen temp(*this);
    ++level;
    return temp;
}

//++operator
Clen &operator ++ (){
    ++level;

    return *this;
}

int getLevel(){
    return level;
}

//operator <<
friend ostream &operator << (ostream &output, const Clen &right);

//operator==
friend bool operator == (const Clen &left, const Clen &right);

//operator!=
friend bool operator != (const Clen &left, const Clen &right);

};
ostream &operator << (ostream &output, const Clen &right){
output << right.name << " " << right.surname << ", " << right.level << endl;
return output;
}

bool operator == (const Clen &left, const Clen &right){
if(left.level == right.level)
    return true;

return false;
}

bool operator != (const Clen &left, const Clen &right){
return !(left == right);
}

class Klub{
private:
char name[100];
Clen *clenovi;
int elements;
void copy(const Klub &toCopy){
    strcpy(name, toCopy.name);
    elements = toCopy.elements;

    clenovi = new Clen[elements + 1];
    for(int i = 0; i < elements; i++){
        clenovi[i] = toCopy.clenovi[i];
    }
}

public:
//constructor
Klub(char n[] = "null", Clen c[] = NULL, int e = 0){
    strcpy(name, n);
    elements = e;

    clenovi = new Clen[elements + 1];
    for(int i = 0; i < elements; i++){
        clenovi[i] = c[i];
    }
}
//copy constructor
Klub(const Klub &toCopy){
    copy(toCopy);
}

//assignment operator
Klub &operator = (const Klub &right){
    if(this == &right)
        return *this;
    delete [] clenovi;
    copy(right);

    return *this;
}

//destructor
~Klub(){
    delete [] clenovi;
}

//operator <<
friend ostream &operator << (ostream &output, const Klub &right);


//operator +=
Klub &operator +=(Clen c){
    Clen *temp = clenovi;
    clenovi = new Clen[elements + 1];

    for(int i = 0; i < elements; i++){
        clenovi[i] = temp[i];

    }

    clenovi[elements] = c;
    ++elements;
    delete [] temp;
    return *this;

}

Klub &novKlub(Clen &c){
    Klub newKlub;
    strcpy(newKlub.name, name);

    for(int i = 0; i < elements; i++){
        if(c.getLevel() == clenovi->getLevel()){
            newKlub += clenovi[i];

        }

    }
    return *this;
}
};

ostream &operator << (ostream &output, const Klub &right){
output << right.name << endl;
for(int i = 0; i < right.elements; i++){
    output << right.clenovi[i] << endl;
}

return output;
}
int main()
{
Clen clen;
int n, stepen;
char ime[30], prezime[30];
cin >> ime >> n;
Klub k1(ime); 
for(int i = 0; i < n; i++){
    cin >> ime >> prezime >> stepen;
    Clen c(ime, prezime, stepen);
    k1 += c;
    clen = c; 
}  
Klub k2 = k1.novKlub(clen);
cout << k2;
return 0;
}

You should return your object by value, not reference. 您应该按值而不是引用返回对象。 Change your declaration to: 将声明更改为:

Klub novKlub(Clen &c)

Functions should never return a reference to a local object, as it is destroyed upon function return leaving a dangling reference. 函数从不应该返回对本地对象的引用,因为在函数返回时它会被破坏,留下一个悬挂的引用。

UPDATE: If changing the signature is not an option, you should still not return a reference to a local. 更新:如果不能更改签名,则仍不应返回对本地的引用。 In this case, presumably the intention is to create the object which is inserted into some container, and then return a reference to it within this container. 在这种情况下,大概是要创建插入到某个容器中的对象,然后在该容器中返回对其的引用。 You haven't provided enough detail about your classes to say exactly what this could be, but perhaps it shoud be inserted into clenovi or c . 您没有提供有关类的足够详细的信息来确切说明这可能是什么,但是也许应该将其插入clenovic If you provide more detail for those this can be clarified. 如果您提供更多详细信息,则可以澄清。

Given that you are creating a new object, return it by value. 假设您正在创建一个新对象,请按值返回它。

Situations that call for returning a reference are somewhat limited (see below). 要求返回参考的情况在一定程度上受到限制(请参阅下文)。 Your situation is not one of them. 您的情况不是其中之一。

The question is what should I return [if I want to return a reference] 问题是我应该返回什么[如果我想返回参考文献]

You should return an object with static lifetime, or lifetime known to the caller. 您应该返回一个具有静态生存期或调用者已知的生存期的对象。 Make sure that the caller does not need to free your object. 确保调用者不需要释放您的对象。

I am trying to return just an object that is newly created in the class, it's not working 我正在尝试只返回在类中新创建的对象,它不起作用

You should not require the caller to free objects that you return by reference, so returning a newly created object is not an option. 您不应该要求调用者释放通过引用返回的对象,因此,返回新创建的对象不是一种选择。

I also tried returning reference or pointer, and it's not working. 我也尝试返回引用或指针,但它不起作用。

You need to return an object that does not go out of scope upon return. 您需要返回一个在返回时不会超出范围的对象。 Usually you want to return 通常你想回来

  • Your object itself, ie *this , or 您的对象本身,即*this
  • One of your object's dependents, ie this->someField , or 对象的一个​​依赖项,即this->someField ,或
  • One of your function parameters (this is a very common pattern in << overriding), or 您的函数参数之一(这是<<覆盖中的一种非常常见的模式),或者
  • A static or global variable (not recommended). 静态或全局变量(不建议)。

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

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