Why can't the C ++ value assignment operator overload functions be inherited?

Source: Internet
Author: User

This problem has plagued me for a while. First, check the source code below the idea:

Class A1
{
Public:
Int perator = (int)
{
Return 8;
}

Int operator + (int)
{
Return 9;
}
};

Class B1: Public A1
{
Public:
Int operator-(int)
{
Return 7;
}
};

Int main ()
{
B1 V;
Cout <(V + 2) <Endl; // OK, print 9
Cout <(V-2) <Endl; // OK, print 7
Cout <(V = 2) <Endl; // error, see below

Return 0;
}

Error message of the VC Compiler:

Error c2679: Binary '=': No operator defined which takes a right-hand operand of Type 'const int' (or there is no acceptable conversion)

This means that the compiler cannot find the int perator = (int A) member function to call.

Really strange? Obviously int perator = (int A) is a function that I inherited from the base class public. How can the compiler not recognize it? In this case, the first reaction was to find msdn for an explanation. Microsoft told me:

"All overloaded operators defined t assignment (operator =) are inherited by Derived classes ."

This means that, except for the value assignment operator overload function, all operator overload functions can be inherited by the derived class.

I am using this function int perator = (int A). Unfortunately, this is what Microsoft calls the "value assignment operator overload function". Of course, this function cannot be inherited!

So far, my questions have not been eliminated. Why cannot the "value assignment operator overload function" be inherited by a derived class? In C ++ semantics, it is not enough to allow this function to be inherited by a derived class. A class object instance can be assigned a value by any other class object instance! For example, a color object instance can be assigned a value by an integer, or even by a white rabbit instance. Since the value assignment operator allows overloading, it should be allowed to be inherited, just as other operators can be inherited by the derived class after being overloaded.Microsoft's explanation does not explain why the "value assignment operator overload function" cannot be inherited.

I have searched for C ++ primer and other heavyweight C ++ classics, but I have not found any answer to this question, or I have not found any details. As a result, I had to turn to myself and analyze the structure of class C ++ objects. I found the correct answer:

1
When a class object instance is created, if the user does not define the "value assignment operator overload function", the compiler will automatically generate an implicit and default "value assignment operator overload function ". Therefore, B1The actual statement should be similar to the following situation:

Class A1
{
Public:
Int perator = (int)
{
Return 8;
}

Int operator + (int)
{
Return 9;
}
};

Class B1: Public A1
{
Public:
B1 & perator = (const B1 & robj); // note that this line is added by the compiler.
Int operator-(int)
{
Return 7;
}
};

2, C ++ standard: If the declared member in the derived class has the same name as the member of the base class, the member of the base class will be overwritten, even if the data type and number of parameters of the base class members and the derived class members are completely different. Obviously, operator is the name of the value assignment operator in B1.
= The Same name as operator = in the base class A1. therefore, the value assignment operator int in A1.
Perator = (int A); is implicitly assigned by the value assignment operator B1 in B1 &
Perator = (const B1 & robj); overwrite. Int in A1
Perator = (int A); the function cannot be B1
Object Access.

3In the program, statement V = 2 is actually equivalent to v. Operator
= (2); but int perator = (int
A) It has been overwritten and cannot be accessed. The default B1 & perator in B1
= (Const B1 & robj); function and parameter 2
And cannot be called.

4To confirm the default B1 &
Perator = (const B1 & robj );
The following code verifies the existence of a function:

B1 B;
B1 V;

V = B; // OK, equivalent to calling v. Operator = (B );

5Therefore, the "value assignment operator overload function" cannot be inherited by the derived class, but is replaced by the default "value assignment operator overload function" of the derived class"Overwrite.

This is the real reason why the C ++ value assignment operator overload function cannot be inherited by the derived class!

 

Explanation of the correctness of this post

C ++ programmers must read the following classical Objective C ++:

Clause 45: Find out the functions written and called by C ++ behind the scenes

When is an empty class not an empty class? ---- When the C ++ compiler passes through it. If you do not declare the following functions, the considerate compiler will declare its own version. These functions are: A copy constructor, a value assignment operator, a destructor, and a pair of access operators. In addition, if you do not declare any constructor, it will also declare a default constructor for you. All these functions are public. In other words, if you write:

Class empty {};

This is the same as writing:

Class empty {
Public:
Empty (); // default constructor
Empty (const empty & RHs); // copy the constructor

~ Empty (); // destructor ---- whether
// For the virtual function, see the following description
Empty &
Perator = (const empty & RHs); // value assignment operator

Empty * operator & (); // address fetch Operator
Const empty * operator & () const;
};

However, Objective C ++ cannot be used as the final decision. Let's look for the answer from C ++'s "constitution...

ISO/IEC 14882 is an international standard for C ++. This standard was adopted and finalized in September 1, 1998. Of course, this standard is no longer the latest standard, but it is currently the most widely supported C ++ standard. Therefore, I have always called it the "Constitution" of C ++ ".

C ++ "constitution" chapter 12th Special
Member functions (page 1:

The default constructor, copy constructor and copy assignment operator, and destructor are special member functions. The implementation will implicitly declare these member functions for a class type
When the program does not explicitly declare them, counter T as noted in 12.1. the implementation will implicitly define then if they are used, as specified in 12.1, 12.4 and 12.8. programs shall not define implicitly-declared special member functions. programs
May explicitly refer to implicitly declared special member functions.

Translation:
Default constructor, copy constructor, copy assignment function, and destructor are called special member functions. If your program does not explicitly declare these special member functions, the compiler implements them implicitly. 12.1 exceptions with special explanations. If your program calls these special member functions, the compiler will define these special member functions. In 12.1, 12.4, 12.8, the compiler defines the methods for these functions. User Programs cannot define special member functions that are implicitly declared. The user program can explicitly call the implicitly declared special member functions.

:
According to the C ++ standard:

The Declaration (declare) represents the part of the Code in the header file. For example, the following is a declaration:

Class
{
Public:
A ();
}

Define indicates the code in the source file. For example, the following is a definition:

A: ()
{}

To sum up, we can see that my first statement is correct.

For the correctness of my second statement, see C ++ "constitution" 3.3.7 name hiding (page 28th) (since C ++ "constitution" in my hand is a scanned version, text cannot be copied directly, and there are too many texts to input .)

My 4, 4 and 5 points are common sense knowledge and can be verified directly.

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.