C++ 在另一个类中使用类对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42987199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a class object in another class
提问by Diante
Node Class
节点类
As part of the tree implementation you should implement a Node class. Each node should contain a Customer object, pointers to left and right children and (optionally) the parent.
作为树实现的一部分,您应该实现一个 Node 类。每个节点都应该包含一个 Customer 对象、指向左右子节点和(可选)父节点的指针。
So, currently I have a customer class such that:
所以,目前我有一个客户类,这样:
class Customer {
public:
Customer(void);
Customer(string,char,int);
};
In my node class how can I create a customer object while linking the two files?
在我的节点类中,如何在链接两个文件时创建客户对象?
Do I just include the following in my node header file?
我是否只在我的节点头文件中包含以下内容?
#include "Customer.h"
class Node {
public:
//Customer class
class Customer {
public:
Customer(void);
Customer(string,char,int);
}
Node(void); //default constructor
Node(string,char,int); //Node constructor with customer details
Node* left;
Node* right;
Node* parent;
};
In the node.cpp file to pass in the values to the node:
在 node.cpp 文件中将值传递给节点:
//Constructor
Node::Node(string x, char y, int z) {
lastName = x;
firstInitial = y;
balance = z;
}
How do I pass the values of the customer object to the node construct?
如何将客户对象的值传递给节点构造?
回答by R Sahu
Do I just include the following in my node header file?
我是否只在我的节点头文件中包含以下内容?
No. You just use an object of type Customer
in Node
.
不,您只需使用Customer
in类型的对象Node
。
#include "Customer.h"
class Node {
public:
Node(void); //default constructor
Node(string,char,int); //Node constructor with customer details
Node* left;
Node* right;
Node* parent;
Customer customer;
};
Implement the constructors as:
将构造函数实现为:
Node::Node() : left(nullptr),
right(nullptr),
parent(nullptr),
customer() {}
Node::Node(string x, char y, int z) : left(nullptr),
right(nullptr),
parent(nullptr),
customer(x, y, z) {}
回答by Alexandru Banu
You simply include the header as you did and then declare a Customer object in your Node class (private / protected / public, as you wish). When you'll declare a Node object, the first thing that is constructed are the objects inside your class, and only then the class itself. So, if you would have a cout
in both constructors with the class name, when you'll be declaring a Node object, you'll see:
您只需像您一样包含标头,然后在您的 Node 类中声明一个 Customer 对象(私有/受保护/公共,如您所愿)。当您声明一个 Node 对象时,首先构造的是类中的对象,然后才是类本身。所以,如果cout
在两个构造函数中都有一个类名,当你声明一个 Node 对象时,你会看到:
Customer's constructor Node's constructor
客户的构造器 Node 的构造器
Also, if you want to specify how to construct the Customer object inside the Node constructor, you can use the list of initialization
另外,如果要在 Node 构造函数中指定如何构造 Customer 对象,可以使用初始化列表
class Node
{
public:
Customer obj;
Node(string,char,int);
}
and then define the Node constructor like this in your .cpp file:
然后在 .cpp 文件中像这样定义 Node 构造函数:
Node :: Node(string x,char y,int z) : obj(x, y, z) {
}
this is just an example. You can use static values when initializing obj
or you could also get more parameters for Node constructor and so on.
这只是一个例子。您可以在初始化时使用静态值,obj
也可以为 Node 构造函数等获取更多参数。