Const char * & not char * const &

Source: Internet
Author: User
Today, I saw a post posted by a student on BBS VC, saying that VC has a bug when compiling the template. The test code he posted is as follows:
// Templatebug. cpp: defines the entry point of the console application. // # Include "stdafx. H "// define an Interface Template <class datatype> Class A {public: Virtual int somefunc (const datatype & Value) = 0 ;}; // B to implement this interface, so it cannot be compiled! Template <class something1, class something2> Class B: public a <char *> {public: Virtual int somefunc (const char * & Value) {return 0 ;};}; // you can use template <class something1, class something2> Class C: public a <char *> {public: Virtual int somefunc (char * const & value) {return 0 ;};}; // The meanings of const char * and char * const are very different. However, after the template parameterization process //, the two are confused, should this be a bug? Int _ tmain (INT argc, _ tchar * argv []) {B <int, int> B; // compile this line through C <int, int> C; return 0 ;}
The comment is interesting, but it is not a bug. On the contrary, it should be said that this is in line with the c ++ specification, not the compiler obfuscation of the two, but our author obfuscated the two. Let's take a look at the const char * & P and char * const & P references. Both references an object. But the "this object" of the former is const char *, a pointer to const char, note! Although the char pointed to by the pointer cannot be changed, the value of the pointer itself can be changed, that is, it can be changed and directed to another const char object. The latter's "this object" is char *, a pointer to Char. The Pointer Points to something that can be changed, but the pointer itself cannot be changed. You cannot point it to another char object. If you think this section is not clear enough, compile the following code: void fconst (const char * & P) {* P = 'l'; // error c2166: L value specifies the constant object P ++; // OK !} Void constf (char * const & P) {* P = 'l'; // OK! P ++; // error c2166: constant object specified by L value} int main () {char STR [] = "Hello World"; char * P = & STR [2]; fconst (p); constf (p);} Now the difference between const char * & and char * const & is clear, then let's look at how the template features the somefunc parameter of the author as a const reference of datatype. The so-called const reference means that you cannot change the state of the object referenced by this reference, therefore, const datatype & and datatype const & are equivalent. That is to say, the parameter of the somefunc function of template A <datatype> is a constant reference to an object, the status of the referenced object cannot be changed within the function scope. It is clear that a <char *> is specially treated as the base class of class B and C, then the somefunc parameter of its pure virtual member function is specially converted to the const reference for char *, and somefunc has the form of int somefunc (char * const &), therefore, if it is defined as int somefunc (const char * & value), it will not match the pure virtual function and will naturally become a common virtual function. The pure virtual function is not defined as error c2259: "B <something1, something2>": the abstract class cannot be instantiated with [something1 = int, something2 = int] because of the following members: "int A <datatype >:: somefunc (const datatype &) ": Undefined pure virtual function with [datatype = char *] tempbug. CPP (5): See the Declaration of "A <datatype>: somefunc" with [datatype = char *]. The so-called bug will not be cracked. Haha.

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.