Mutual calling between C ++ classes

Source: Internet
Author: User

 

This problem has previously seen mutual calls between classes when looking at other people's code, but I have never figured out how to avoid errors in syntax. I tried to write a program to test it.

Two classes are created here. One is an animal and the other is a fish.

1. There is a member function PRT in the animal class (the name is obtained at will), and the function is responsible for outputting: I am an animal.
2. Then a member function P () of the fish calls the PRT of the animal to output: I am an animal.
3. In animals, T () is used to call the P function of the fish to output: I am an animal.

In the main function, we instantiate the animals and fish respectively and call T and P respectively;

Do not get dizzy.

I believe that if you only need to complete step 1, step 2, there should be no problem. The key is step 3.

1. Add a function PRT;
2. The fish removal header file contains the animal header file. Declare an animal variable. In the member functions of fish, the PRT is called using animal-class instances;
At the beginning of the third step, the header file of the animal class will include the header file of the fish.
However, this step is too hasty. At the beginning, you included the animal header file in the fish header file. Now you include the fish header file in the animal header file. Such mutual header file inclusion is certainly an error. Believe it or not

 

It is wrong to include header files, but it is not wrong to call classes. You can think about why. Classes are actually called after instantiation. For example, I instantiate a cow for an animal. I am an instance of ainemo A, B. The Ox calls a function of a to call me an animal for the ox. B. Call the ox to call me an animal. It looks like mutual calls between classes. In fact, mutual calls between instances. A and B are independent of each other in the memory.

But how can I instantiate ainemo? When instantiating a cow, we directly call the animal class to declare a variable and use the constructor to instantiate it. However, we cannot instantiate the fish directly. Because there is no fish header file. What should we do?
1. The animal header file cannot contain the fish header file, but can be included in the. cpp file of the animal. Think about why?
So you can write: Include the fish header file in. cpp. Then instantiate the fish. Call the fish function to shout: I am an animal. Complete mutual call

2. The first method instantiates a fish is a local variable. What if I want a fish with global variables? It can only be declared in the header file.
You must declare this in the animal header file:

Class fish;

Do not write it as: class fish {}; or write its internal member data. As long as it is written as above. If you write an internal function. An error occurs during compilation, indicating that the fish is already defined. Then define a global variable. But can you define it like this? : Fish fi;
This is still wrong. Because this fish is not the real one that can be instantiated at the moment. It has no constructor. So here we use pointers. You can write like this: fish * fi. It is not instantiated for the time being. It will be instantiated when the. cpp header file contains the fish. For example, in the animal constructor, we are like this:
FI = new fish; then we can call each other between classes.

 

 

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.