简体   繁体   English

您如何将结构作为 class 中的私有成员并为其创建参数化构造函数/设置器 function?

[英]How do you have a struct as a private member in a class and creating a parameterized constructor/setter function for it?

I haven't seen questions about this so I hope this isn't a repeat, but I know you can have a struct as a private member of the class.我还没有看到关于这个的问题,所以我希望这不是重复,但我知道你可以拥有一个结构作为 class 的私有成员。 There seems to be no eloquent way to have a parameterized constructor with a struct as private member.似乎没有 eloquent 方法可以将带有结构的参数化构造函数作为私有成员。 Then there becomes the problem of having a setter function for the struct private member.然后就出现了为结构私有成员设置设置器 function 的问题。 There seems to be no way to access the struct members through a setter function within the one class.似乎无法通过一个 class 中的设置器 function 访问结构成员。

The header file for the Book class图书 class 的 header 文件

// Book.h file
// Header file for the Book class

#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include "Date.h"


class Book {
private:
    std::string m_ISBN;
    std::string m_title;
    std::string m_author;
    Date m_date;
public:
    // constructor
    Book();
    Book(std::string, std::string, std::string, Date);
    void set_date(int, Month, int);

};

#endif

The header file for the Date struct Date 结构的 header 文件

// Date.h file
// Header file for the Date struct

#ifndef DATE_H
#define DATE_H

enum class Month {
    jan = 1, feb, mar, april, may, june, july, aug, sep, oct, nov, dec, MAXMONTH
};

struct Date {
    int m_year { 1800 };
    Month m_month { Month::jan };
    int m_day { 1 };
};

#endif

And then here's my cpp file然后这是我的 cpp 文件

// Book.cpp file

#include "Book.h"

Book::Book() 
    : m_ISBN { " " },
    m_title { " " },
    m_author { " " },
    m_date {1800, Month::jan, 1 }
    {

    }

    Book::Book(std::string ISBN, std::string title, std::string author, Date date)
        : m_ISBN { ISBN },
        m_title { title},
        m_author { author },
        m_date { date }
        {
            
        }

        void Book::set_date(int year, Month month, int day) {
            this->Date::m_year { year }; // error
            this->Date::m_month { month }; // error
            this->Date::m_day { day }; // error 
        }

The code works fine up until set_date , but even before that point.该代码在set_date之前工作正常,但甚至在此之前。 If you create a Book object with parameters, the way you do it seems unintuitive Book one {"random_ISBN", "random_title", "random_author", { 1800, Month::jan, 3} };如果您创建带有参数的 Book object,您的操作方式似乎不直观Book one {"random_ISBN", "random_title", "random_author", { 1800, Month::jan, 3} }; Having these nest curly braces seems suboptimal.拥有这些嵌套花括号似乎不是最理想的。 You could create a Date struct and then pass that into it, but that seems wrong.您可以创建一个 Date 结构,然后将其传递给它,但这似乎是错误的。

Replace代替

void Book::set_date(int year, Month month, int day) {
            this->Date::m_year { year }; // error
            this->Date::m_month { month }; // error
            this->Date::m_day { day }; // error 
        }

with

void Book::set_date(int year, Month month, int day) {
            m_date.m_year = year;
            m_date.m_month = month;
            m_date.m_day = day;
        }

暂无
暂无

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

相关问题 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 您如何在类的成员函数中调用复制构造函数? - How do you call the copy constructor within a member function of a class? 如何使用私有构造函数中的引用成员初始化类? - How can you initialize a class with a reference member from a private constructor? static function 如何访问 class 的私有成员函数(构造函数) - How static function is accessing private member function(constructor) of a class 如何将类成员函数的返回类型设置为私有结构的对象 - How to set the Return Type of a Class Member Function as the object of a Private Struct 如何在另一个类中创建一个类的参数化构造函数作为数据成员? - How to create parameterized constructor of a class in another class as a data member? 如何通过C ++中构造函数的初始化列表来值初始化类的私有结构成员? - how can I value initialize private struct member of class via initialization list of constructor in c++? 您如何在类外部的成员函数中调用成员函数? - How do you call a member function in a member function outside of a class? 当我们有设置值的设置器时,为什么要使用参数化构造函数 - when we have setter for setting value then why do we use parameterized constructor 如何区分类成员函数和数据成员的声明 - How do you distinguish the declaration of a class member function and data member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM