简体   繁体   English

从类函数c ++返回对象

[英]returning object from class function c++

suppose we have a function like below 假设我们有一个像下面的函数

Car buyCar();

My question is which method we must have in Car class for this function to work ? 我的问题是要使此函数起作用,必须在Car类中使用哪种方法? Is it default constructor ? 是默认构造函数吗?

Car buyCar(); does not have the effect people think, look up "The Most Vexing Parse": https://en.wikipedia.org/wiki/Most_vexing_parse 没有人们认为的效果,请查找“最令人烦恼的解析”: https ://en.wikipedia.org/wiki/Most_vexing_parse

In the context of a function or method: 在函数或方法的上下文中:

Due to quirks in C++'s grammar, the syntax Type instance() is actually interpreted as a declaration instead of as an invocation. 由于C ++语法中的古怪之处,语法Type instance()实际上被解释为声明而不是调用。 See calling the default constructor 请参见调用默认构造函数

To call the default, parameterless, constructor of a type you need to omit the parenthesis. 要调用默认的无参数类型的构造函数,您需要省略括号。

Car buyCar; // allocates and constructs a `Car` on the stack

However, if you're allocating on the heap (with new ), then using the parenthesis does work. 但是,如果要在堆上分配(使用new ),则使用括号确实可以。

Using raw pointers: 使用原始指针:

Car* buyCar = new Car();

Using smart pointers: 使用智能指针:

unique_ptr<Car> buyCar = make_unique<Car>(); // <-- parens used because this is actually calling `make_unique` (a function) which indirectly calls the constructor

In a type definition (ie a field): 在类型定义(即字段)中:

In an initialization list, then you do use parenthesis to call the default constructor of a field - however this is largely pointless as the default (compile-generated) constructor for the type will already do this: 在初始化列表中,您确实使用括号来调用字段的默认构造函数-但这在很大程度上没有意义,因为该类型的默认(编译生成的)构造函数已经可以做到这一点:

class Inner {
public:
    Inner() {
    }
}

class Container {
private:
    Inner innerInstance;
public:
    Container() :
        innerInstance() // <-- parens used here
    { }
}

This method should return the object of the Car class. 此方法应返回Car类的对象。 Default constructor is automatically available, it needs not to be defined separately. 默认构造函数是自动可用的,不需要单独定义。 Custom constructor can also be created and initialized with required values. 还可以创建自定义构造函数,并使用所需的值对其进行初始化。 So, either you can return the default constructor or custom constructor based upon the requirements. 因此,您可以根据需要返回默认构造函数或自定义构造函数。

I have written a small example for you 我为你写了一个小例子

#include <iostream>
using namespace std;
class Car {
public :
    Car(): carnum(0) {
        cout << "default constructor " << carnum << endl;
    }

    Car (int n) : carnum (n) {
        cout << "Argument constructor " << carnum << endl; 
    }

    Car& operator = (const Car& car) {

        carnum = car.carnum;
        cout << "operator = " << carnum << endl;
        return *this;
    }

    Car (const Car& car) {
        carnum = car.carnum;
        cout << "Copy constructor " << carnum << endl;
    }

    Car buyCar() {
        return *this;
    }

    int carnum;
};
int main() {
    Car aCar, theCar(2); // default constructor for aCar and argument constructor for theCar;
    cout << endl;
    aCar = theCar.buyCar(); // copy constructor and equal operator are called
    cout << endl;
    Car bCar(aCar.buyCar()); // only copy constructor is called here
    cout << endl;
    return 0;
}

So, it depends on the context how you are using buyCar() function. 因此,这取决于上下文,您如何使用buyCar()函数。

I have assumed that buyCar() is a member function of class Car 我假设buyCar()class Car的成员函数

Example

Even if class Car do not have Constructors and Assignment Operator , the function buyCar() would still work with default constructors and assignment operators. 即使class Car没有ConstructorsAssignment Operator ,函数buyCar()仍将与默认构造函数和赋值运算符一起使用。

But if your class deals with dynamic memory, that if the class has some data members which are dynamically created/allocated then the class should have the Copy Constructor , Move Constructor , Assignment Operator and Move Assignment operator 但是,如果您的类处理动态内存,那么如果该类具有一些动态创建/分配的数据成员,则该类应具有Copy ConstructorMove ConstructorAssignment OperatorMove Assignment operator

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

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