简体   繁体   English

将对象从另一个类传递给 C++ 中另一个类的构造函数?

[英]Passing object from another class to a constructor of the other in C++?

I have 2 classes - in the second one I want to pass an object of the class CAdress and via its explicit constrcutor , assign the member ar from the class CStudent the corresponding values?我有2个类-在第二个我想通过类的对象CAdress其,并通过explicit constrcutor ,分配构件ar从类CStudent相应值? I would like to ask how can I do this?我想问一下我该怎么做? Thanks in advance!提前致谢!

class CAdress {
    string street;
    string postal;
    string city;
public:
    CAdress() {
        street = "Studentska #1";
        postal = "9010";
        city = "Varna";
    };
    CAdress(string st, string pos, string ct) {
        street = st;
        postal = pos;
        city = ct;
    }
};



  class CStudent : public CPerson2 {
        string fn;
        CAdress adr;
    public:
        CStudent() {
            fn = "12131547";
        }
        CStudent(string nm, CAdress add, string egnn) {
            name = nm;
            //how to give values to the adress?
            //add = ?
            egn = egnn;

        }

    };

It is more efficient to do it like this这样做效率更高

CStudent (const string& nm, const CAdress& add, const string& egnn) :
     name (nm),
     adr (add),
     egn (egnn) {}

For reference read https://en.cppreference.com/w/cpp/language/initializer_list .参考阅读https://en.cppreference.com/w/cpp/language/initializer_list

You can create a copy constructor and copy the value directly.您可以创建一个复制构造函数并直接复制值。 https://www.geeksforgeeks.org/copy-constructor-in-cpp/ . https://www.geeksforgeeks.org/copy-constructor-in-cpp/ By default the compiler provides one, but it only does the shallow copy.默认情况下,编译器提供一个,但它只执行浅拷贝。 So its better to create one sometimes.所以最好有时创建一个。

    CStudent(string nm, CAdress add, string egnn) {
        name = nm;
        adr = add;
        egn = egnn;
    }

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

相关问题 在C ++中将对象传递给另一个类的构造函数时遇到问题 - Having trouble passing an object into the constructor of another class in C++ 将 object 作为参数传递给另一个 class C++ 的构造函数 - passing an object as a parameter to a constructor of another class C++ C ++将std :: function从另一个类传递给构造函数 - c++ passing std::function from another class to a constructor C ++构造函数并将变量传递给另一个类 - C++ constructor and passing variables to another class C++ 将一个对象从另一个类传递给另一个对象 - C++ Passing an Object to Another Object From Another Class 将另一个类的对象传递给当前类 C++ 的构造函数(不是成员初始化) - Passing object of another class into constructor of current class C++ (Not member initialization) C ++-在另一个类中实例化对象时,调用默认构造函数以外的其他构造函数 - C++ - Calling a constructor other than the default when an object is instantiated in another class C++ - 从另一个类构造函数调用类构造函数 - C++ - Calling a class constructor from another class constructor 将Vector作为参数传递给另一个类构造函数C ++ - Passing a Vector as parameter to another class constructor C++ C++:将 object 传递给 class 构造函数,它是如何存储的? - C++: Passing object to class constructor, how is it stored?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM