Class references are not defined in C ++.

Source: Internet
Author: User

Sometimes, because the class is too large, it needs to be defined later in the class;

For example:

Class Y {void f (X) ;}; class X {// some member data and functions };
// Error
Because c ++ requires that any variable must be declared before reference ., In the above definition, we can change the order of the two.

But if a loop is formed?

Class X {void f1 (Y)}; class Y {void f (X );};
// Error

This kind of rule cannot be declared by the compiler before it is called by simply changing the sequence.

So we can first declare in the above?

Class Y; class X {void f1 (Y) // error}; class Y {void f (X );};

However, in the above Code, the f1 (Y) parameter is variable Y, and the specific structure of the entire class Y must be known at this time. So it is not feasible.

So we can change the parameter Y in function f1 ()

Class Y; class X {void f1 (Y *)}; class Y {void f (X );};
// Right

Changing the preceding parameter class Y to its pointer is feasible because the compiler knows how to pass an address, which is fixed in size. You do not need to know what objects are passed, even if we do not know the size of the object type to be passed.

However, here the function f2 () can use Class X as the parameter, because Class X has been fully defined.








Related Article

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.