Application of pre-statement in C + + and trap _c language

Source: Internet
Author: User

Use of a predecessor declaration
A friend with a certain C + + development experience might encounter a scenario where two classes A and B are strongly coupled, Class A refers to the object of B, and Class B refers to the object of Class A. Okay, it's not hard, my first instinct is to get me to write this code:

Copy Code code as follows:

A.h
#include "B.h"
Class A
{

Public
A (void);
Virtual ~a (void);
};
A.cpp
#include "A.h"
A::a (void)
{
}
A::~a (void)
{
}
B.h
#include "A.h"
Class B
{
A A;
Public
B (void);
~b (void);
};
B.cpp
#include "B.h"
B::b (void)
{
}
B::~b (void)
{
}

OK, finish, compile the A.cpp, do not pass. Then compile the B.cpp, or do not pass. The compiler is dizzy, compiler to compile A.h, found that contains B.h, to compile B.h. Compile B.h When the discovery contains A.h, but A.h has been compiled (in fact, there is no compilation completed, the compiler may have done a record, A.h has been compiled, so as to avoid falling into a dead loop. Compilation errors are always stronger than the dead loop, and you will continue compiling without recompiling A.h. Later found that the definition of a, which is good, the definition of A is not compiled, so the definition of a is not found, the compilation error. The message is as follows:
1>d:/vs2010/test/test/a.h (5): Error c2146:syntax error:missing '; ' before identifier ' B '
1>d:/vs2010/test/test/a.h (5): Error c4430:missing type Specifier-int assumed. Note:c++ does not support Default-int
1>d:/vs2010/test/test/a.h (5): Error c4430:missing type Specifier-int assumed. Note:c++ does not support Default-int
then what? There is a way, C + + for us to provide a predecessor statement. What is the predecessor statement? For example, I would like to build a house (chouse), the room is not enough, I have to have a bed (CBed). But the house has not been built, the total can not buy a bed, the size of the bed I decided to buy another day. First have to cover the house, build the house when I first to leave a seat, and so the house is covered, I decided to buy what kind of bed. The predecessor statement is that when I declare a class (Chouse), I use the definition of another class (CBed), but CBed has not yet been defined, and I do not need the definition of CBed first, as long as I know that CBed is a class. Well, I'll just declare the class cbed, tell the compiler that cbed is a class (without the cbed header file):
Copy Code code as follows:

Class CBed;

The cbed in the Chouse is then used in the Cbed pointer type (because the pointer type is fixed-size, but the cbed size is determined only by knowing the cbed definition). When you want to implement the Chouse definition, you must know the definition of cbed, that is, then wrap the cbed header file on the line.

A predecessor statement is sometimes useful, for example, when two classes are dependent on each other. There are also predecessors to reduce the level of the header file contains, reduce the likelihood of error. The example mentioned above.

Copy Code code as follows:

House.h
Class CBed; When building a house: Don't buy it now, you must buy a bed
Class Chouse
{
cbed* bed; I'll leave a place for the bed.
Public
Chouse (void);
Virtual ~chouse (void);
void Gotobed ();
};
House.cpp
#include "Bed.h"
#include "House.h"//etc The house began to decorate, want to buy a bed
Chouse::chouse (void)
{
bed = new CBed (); Put the bed in the house
}
Chouse::~chouse (void)
{
}
void Chouse::gotobed ()
{
Bed->sleep ();
}
Bed.h
Class CBed
{
Public
CBed (void);
~cbed (void);
void Sleep ();
};
Bed.cpp
#include "Bed.h"
cbed::cbed (void)
{
}
cbed::~cbed (void)
{
}
void Cbed::sleep ()
{
}

traps in the predecessor declaration
Notice there's a trap here:
1, cbed* bed; You must use a pointer or a reference
Reference version:
Copy Code code as follows:

House.h
Class CBed; When building a house: Don't buy it now, you must buy a bed
Class Chouse
{
cbed& bed; I'll leave a place for the bed.
CBed bed; Compilation error
Public
Chouse (void);
Chouse (cbed& bedtmp);
Virtual ~chouse (void);
void Gotobed ();
};
House.cpp
#include "Bed.h"
#include "House.h"//etc The house began to decorate, want to buy a bed
Chouse::chouse (void)
: Bed (*new CBed ())
{
cbed* bedtmp = new CBed (); Put the bed in the house
bed = *bedtmp;
}
Chouse::chouse (cbed& bedtmp)
: Bed (bedtmp)
{
}
Chouse::~chouse (void)
{
Delete &bed;
}
void Chouse::gotobed ()
{
Bed. Sleep ();
}

2, can not use the cbed in the Chouse declaration method
An undefined type cbed is used;
The left side of the bed->sleep must point to class/struct/union/generic type
Copy Code code as follows:

Class CBed; When building a house: Don't buy it now, you must buy a bed
Class Chouse
{
cbed* bed; I'll leave a place for the bed.
CBed bed; Compilation error
Public
Chouse (void);
Virtual ~chouse (void);
void Gotobed ()
{
Bed->sleep (); I can't get any sleep if I don't have a bed.
}
};

3, call the Cbed destructor before cbed the definition
Copy Code code as follows:

House.h
Class CBed; When building a house: Don't buy it now, you must buy a bed
Class Chouse
{
cbed* bed; I'll leave a place for the bed.
CBed bed; Compilation error
Public
Chouse (void);
Virtual ~chouse (void);
void Gotobed ();
void Removebed ()
{
Delete bed; I don't need a bed, I'm going to tear it down. How can I take it apart?
}
};
House.cpp
#include "Bed.h"
#include "House.h"//etc The house began to decorate, want to buy a bed
Chouse::chouse (void)
{
bed = new CBed (); Put the bed in the house
}
Chouse::~chouse (void)
{
int i = 1;
}
void Chouse::gotobed ()
{
Bed->sleep ();
}
Bed.h
Class CBed
{
int* num;
Public
CBed (void);
~cbed (void);
void Sleep ();
};
Bed.cpp
#include "Bed.h"
cbed::cbed (void)
{
num = new int (1);
}
cbed::~cbed (void)
{
Delete num; The call was not
}
void Cbed::sleep ()
{
}
Main.cpp
#include "House.h"
int main ()
{
Chouse House;
House. Removebed ();
}

a predecessor statement resolves the interdependence of two classes
Next, give the answer to the first question:
Copy Code code as follows:

A.h
Class B;
Class A
{
b* b;
Public
A (void);
Virtual ~a (void);
};
A.cpp
#include "B.h"
#include "A.h"
A::a (void)
{
b = new B;
}
A::~a (void)
{
}
B.h
Class A;
Class B
{
A A;
Public
B (void);
~b (void);
};
B.cpp
#include "A.h"
#include "B.h"
B::b (void)
{
A = New A;
}
B::~b (void)
{
}

The application of the predecessor declaration in the Friend class method
"C + + Primer 4Edition" in the Friends of the class section, said, if the member function of another class B is declared as a friend function f in a declaration of class A, then class A must know the definition of Class B in advance, and the member function F of Class B declares that if Class A is used as a formal parameter, Then you must know the definition of Class A, so two classes are dependent on each other. To resolve this problem, you must use the class's predecessor declaration. For example:
Copy Code code as follows:

House.h
#include "Bed.h"
Class Chouse
{
friend void Cbed::sleep (chouse&);
Public
Chouse (void);
Virtual ~chouse (void);
void Gotobed ();
void Removebed ()
{
}
};
House.cpp
#include "House.h"
Chouse::chouse (void)
{
}
Chouse::~chouse (void)
{
int i = 1;
}
void Chouse::gotobed ()
{
}
Bed.h
Class Chouse;
Class CBed
{
int* num;
Public
CBed (void);
~cbed (void);
void Sleep (chouse&);
};
Bed.cpp
#include "House.h"
cbed::cbed (void)
{
num = new int (1);
}
cbed::~cbed (void)
{
Delete num;
}
void Cbed::sleep (chouse& h)
{
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.