简体   繁体   English

C ++ - Object作为参数中的变量

[英]C++ - Object as a variable in parameters

I am learning C++ through TheNewBostons tutorials and there is one of the programs that I can't figure out how it works. 我正在通过TheNewBostons教程学习C ++,其中一个程序我无法弄清楚它是如何工作的。 I'll show you the codde before the question. 我会在问题面前告诉你这个密码。

Main.cpp:

#include <iostream>
#include "People.h"
#include "Birthday.h"


using namespace std;

int main()
{
    Birthday birthObject(12,28,1986);

    People Ob("Vidar", birthObject);
    Ob.printInfo();
}

Birthday.h: Birthday.h:

#ifndef BIRTHDAY_H
#define BIRTHDAY_H


class Birthday
{
    public:
        Birthday(int m,int d,int y);
        void prinDate();
    private:
        int month, day, year;
};

#endif

Birthday.cpp: Birthday.cpp:

#include <iostream>
#include "Birthday.h"

using namespace std;

Birthday::Birthday(int m,int d,int y)
{
    month = m;
    day = d;
    year = y;
}

void Birthday::prinDate()
{
    cout << day << "/" << month << "-" << year << endl;
}

People.h: People.h:

#ifndef PEOPLE_H
#define PEOPLE_H

#include <string>
#include "Birthday.h"
using namespace std;

class People
{
    public:
        People(string x, Birthday bo);
        void printInfo();
    private:
        string name;
        Birthday dateOfBirth;
};

#endif

People.cpp: People.cpp:

#include "People.h"
#include "Birthday.h"

#include <iostream>
#include <string>
using namespace std;

People::People(string x, Birthday bo)
: name(x), dateOfBirth(bo)
{

}

void People::printInfo()
{
    cout << name << " was born on ";
    dateOfBirth.prinDate();
}

The thing that I can't figure out is how the objects are used as variables and parameters, and also how you can create an object without calling the constructor (in People.h). 我无法弄清楚的是对象如何用作变量和参数,以及如何在不调用构造函数的情况下创建对象(在People.h中)。

This expression (mem initializer) 这个表达式(mem初始化程序)

dateOfBirth(bo)

in the constructor definition 在构造函数定义中

People::People(string x, Birthday bo)
: name(x), dateOfBirth(bo)
{

}

means a call of the copy constructor of class Birthday to construct object dateOfBirth from object bo. 表示调用类Birthday的复制构造函数来从对象bo构造对象dateOfBirth。

For example if you would add an explicitly defined copy constructor for class Birthday the following way 例如,如果您要按以下方式为类Birthday添加显式定义的复制构造函数

class Birthday
{
    public:
        Birthday(int m,int d,int y);
        Birthday( const Birthday &rhs )
        {
            std::cout << "Birthday::Birthday( const Birthday & ) is called" << std::endl;
            month = rhs.month; day = rhs.day; year = rhs.year;
        } 
        void prinDate();
    private:
        int month, day, year;
};

then when this statement was executed 然后当这个陈述被执行时

People Ob("Vidar", birthObject);

you would get message 你会得到消息

Birthday::Birthday( const Birthday & ) is called

If you do not define explicitly the copy constructor then the compiler defines it implicitly. 如果未明确定义复制构造函数,则编译器会隐式定义它。

A reasonable question. 一个合理的问题。

What is happening here is that it is calling the copy constructor. 这里发生的是它正在调用复制构造函数。

Whenever you declare a class there are several functions which are declared for you, unless you explicitly override them. 每当你声明一个类时,都有几个为你声明的函数,除非你明确地覆盖它们。

  • Default constructor (if no other constructor is explicitly declared) 默认构造函数(如果没有显式声明其他构造函数)
  • Copy constructor if no move constructor or move assignment operator is explicitly declared. 如果没有显式声明移动构造函数或移动赋值运算符,则复制构造函数。 If a destructor is declared generation of a copy constructor is deprecated. 如果声明了析构函数,则不推荐生成复制构造函数。
  • Move constructor if no copy constructor, move assignment operator or destructor is explicitly declared. 如果没有显式声明复制构造函数,移动赋值运算符或析构函数,则移动构造函数。
  • Copy assignment operator if no move constructor or move assignment operator is explicitly declared. 如果未显式声明移动构造函数或移动赋值运算符,则复制赋值运算符。 If a destructor is declared generation of a copy assignment operator is deprecated. 如果声明析构函数,则不推荐生成复制赋值运算符。
  • Move assignment operator if no copy constructor, copy assignment operator or destructor is explicitly declared. 如果未显式声明复制构造函数,复制赋值运算符或析构函数,则移动赋值运算符。
  • Destructor 析构函数

Have a look here: http://en.wikipedia.org/wiki/Special_member_functions 看看这里: http//en.wikipedia.org/wiki/Special_member_functions

In this case the copy constructor is called. 在这种情况下,将调用复制构造函数。

Let's take this step by step: 让我们一步一步:

Birthday birthObject(12,28,1986);

here you're creating a new local variable birthObject of type of Birthday class, passing as parameters (12,289,1986). 在这里,您将创建一个类型为Birthday类的新局部变量birthObject ,作为参数传递(12,289,1986)。 That invokes the three-parameter constructor of Birthday class 这将调用Birthday类的三参数构造函数

People Ob("Vidar", birthObject);

Here you're creating a local variable Ob of type People (note common conventions have class names starting with uppercase, variables starting with lowercase) and passing as parameters ("Vidar" and birthObject). 在这里,您将创建一个类型为People的局部变量Ob (注意常见约定的类名以大写开头,变量以小写开头)并作为参数传递(“Vidar”和birthObject)。

That invokes the two-parameters constructor of People. 这将调用People的双参数构造函数。 This is defined as: 这被定义为:

People::People(string x, Birthday bo): name(x), dateOfBirth(bo) { }

Some notes here: 这里有一些注释:

  • Both parameters are passed by value. 两个参数都按值传递。 This means that the copy constructor (implicit, because you haven't defined any) will be invoked for each of them. 这意味着将为每个复制构造函数调用复制构造函数(隐式,因为您尚未定义任何函数)。 (note, as it has been noted in other answers, it is probably best to use const & instead). (注意,正如在其他答案中已经注意到的,最好使用const而不是)。
  • attributes are initialized (name, dateOfBirth) (this involves an additional copy constructor for each of them). 属性被初始化(name,dateOfBirth)(这涉及每个属性的附加复制构造函数)。

    Ob.printInfo(); Ob.printInfo();

Finally, the printInfo() method of the Ob instance is invoked... which in turn invokes the prinData() method of the Ob's attribute dateOfBirth. 最后,调用Ob实例的printInfo()方法......然后调用Ob的属性dateOfBirth的prinData()方法。 Those methods do not have parameters and are invoked on the respective instances. 这些方法没有参数,并在相应的实例上调用。

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

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