嵌套类和构造函数调用理解 [英] Nested class and constructor calling understanding
问题描述
我目前有一个作业,我需要创建一个DATABASE类,
包含 employees指针数组和 private inner class ,
,其中定义员工关联列表
在 DB_TYPE = 0 需要时,将 DB_TYPE 定义的常数
改为
<我的类方法使用 employees指针数组,
当 DB_TYPE = 1 时,我需要我的类方法使用列表。
因此,我需要两件事:
1。了解构造函数调用 -
当我构造一个新的DATABASE对象时,例如没有参数,
将调用默认构造函数。
我从构造函数本身调用链接列表内部构造函数来构造一个Node? p>
2。根据DB_TYPE常数工作 -
我假设这不是一个麻烦,因为我可以设置我的方法使用
两个case \or with'if '关于DB_TYPE的每个值的条件。
但是如果这不是那么简单,我很高兴得到一些advise \help如何这样做。
EDIT:
我当前的代码:
class DataBase {
public:
DataBase();
private:
Employee ** empArr; / *!< Employees指针数组* /
unsigned int empCount;
节点头;
class Node {
public:
Node();
Node(Employee * emp,Node * next);
private:
Employee * emp; / *!< Employee Pointer * /
Node * next; / *!<下一个节点(包含下一个员工)* /
};
};
感谢,
Adiel
的定义 $ c> $ c>。
在创建时调用 node
的非默认构造函数,数据库
使用构造函数初始化列表:
head(foo,bar)
,empCount(0)
,empArr(nullptr)
{
}
您可以并且应该初始化此列表中的其他成员。如果对象需要复杂的初始化,你可以写一个函数来做;通过构造函数体初始化应该是最后的手段。 (好吧,后来的手段,这不是一个可怕的事情)。
请注意,顺序成员对象被初始化是它们出现在类定义 - 不是他们出现在构造函数init列表中的顺序。因此,如果你的头
的构造函数参数必须从 empArr
中得出,确保节点头; 在 Employee ** empArr;
之后,还必须写一个函数设置 empArr
,使您的列表看起来像
:empCount 5),empArr(allocate_emparr(5)),head(empArr [0],nullptr)
b
$ b Re。 DB_TYPE
。因为它是预处理器,你可以在适当的地方做
#if DB_TYPE
//用于处理链表的代码
#else
//处理指针数组的代码
#endif
这是非常强大的,虽然有些人认为它丑陋。使用模板是更好的,特别是如果类定义可以保持不变,它只是少数的行为需要改变的函数。
示例声明(可以是类成员或自由函数):
template< int N> void do_something(void);
模板<> void do_something< 0>(void){/ * process pointers array * /}
template<> void do_something< 1>(void){/ * process linked list * /}
p>
void other_func(void)
{
do_something< DB_TYPE>();
}
I'm currently having an assignment, in which i need to create a DATABASE class,
which contains a employees pointers array, and private inner class,
that defines employees linked list
the goal to accomplish here, according to the assignment, is working acording
to a DB_TYPE defined constant.
when DB_TYPE = 0 i need my class methods to work with the employees pointers array,
when DB_TYPE = 1 i need my class methods to work with the employees linked list.
Therefore, i need two things:
1. Understanding constructor calling -
When i construct a new DATABASE object, for example with no paramaters,
the default constructor is called.
How do i call the linked list inner constructor to construct a Node, from the constructor itself?
2. Working according to the DB_TYPE constant -
I Suppose that's less of a trouble, as i can set my methods to work with
two cases\or with 'if' conditions regarding each value of DB_TYPE.
but if that's not that simple, i'll be glad to get some advise\help on how to do so.
EDIT:
My Current code:
class DataBase {
public:
DataBase();
private:
Employee ** empArr; /*!< Employees pointers array */
unsigned int empCount;
Node head;
class Node{
public:
Node();
Node(Employee * emp, Node * next);
private:
Employee * emp; /*!< Employee Pointer */
Node * next; /*!< The next node (containing the next employee) */
};
};
Thanks,
Adiel
解决方案 The Node node;
has to come after the definition of class Node
.
To invoke a non-default constructor of node
when creating a Database
you use the constructor initialization list:
Database::Database()
: head(foo, bar)
, empCount(0)
, empArr(nullptr)
{
}
You can and should initialize the other members in this list too. If the object needs complicated initialization you can write a function to do that; initializing via the constructor body should be a last resort. (well, a later resort, it's not a terrible thing).
Note that the order member objects are initialized is the order they appear in the class definition - not the order they appear in the constructor init list. So if, for example, your head
's constructor argument had to be worked out from empArr
, you would have to make sure that Node head;
came after Employee **empArr;
, and also you'd have to write a function to set up empArr
so your list would look like
: empCount(5), empArr( allocate_emparr(5) ), head(empArr[0], nullptr)
Re. DB_TYPE
. Since it is preprocessor you can do, wherever appropriate,
#if DB_TYPE
// code for processing linked list
#else
// code for processing pointers array
#endif
This is very powerful, although some people consider it ugly. It's be nicer to use a template instead, especially if the class definition can stay the same and it is only a small number of functions whose behaviour needs to change. However it is challenging to write the template version well if you have not used templates much before.
Example declarations (could be class members or free functions):
template<int N> void do_something(void);
template<> void do_something<0>(void) { /* process pointers array */ }
template<> void do_something<1>(void) { /* process linked list */ }
Usage:
void other_func(void)
{
do_something<DB_TYPE>();
}
这篇关于嵌套类和构造函数调用理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!