Class member functions can directly access the private variables of the class parameters.

Source: Internet
Author: User

I was reading "C ++ meditation" just now and had doubts about the following code:

Class vehicelsurrogate {<br/> Public: <br/> //.... <br/> vehiclesurrogate (const vehicelsurrogate &); <br/> //.... <br/> PRIVATE: <br/> vehicle * VP; <br/>}; <br/> vehicelsurrogate: vehicelsurrogate <br/> (const vehicelsurrogate & V ): <br/> VP (v. VP? V. VP-> copy (): 0 ){}

Previously, the member functions defined in this class must be used to access the class parameters. However, in the preceding example, the parameter V directly accesses its private variables. To verify whether this writing method is feasible, write a small program for verification. The procedure is as follows:

/* <Br/> * file: scope. CPP <br/> */<br/> class int {<br/> Public: <br/> int (): Value (0) {}< br/> int (INT Val): Value (VAL) {}< br/> int (const Int & X); <br/> ~ INT () {}</P> <p> Int & operator = (const Int & X); <br/> PRIVATE: <br/> int value; <br/>}; <br/> INT: int (const Int & X) <br/>{< br/> value = x. value; <br/>}< br/> Int & INT: Operator = (const Int & X) <br/>{< br/> If (this! = & X) <br/> value = x. value; <br/> return * This; <br/>}< br/> int main () <br/>{< br/> int A (1 ); <br/> int B; <br/> B = A; <br/>}< br/>

Compile, run, and Debug:

Supertool @ supertool-desktop :~ /Test $ C ++-wall-G scope. cpp-O scope <br/> supertool @ supertool-desktop :~ /Test $./scope <br/> supertool @ supertool-desktop :~ /Test $ GDB. /scope <br/> gnu gdb (GDB) 7.1-ubuntu <br/> copyright (c) 2010 Free Software Foundation, Inc. <br/> license gplv3 +: gnu gpl Version 3 or later <pttp://gnu.org/licenses/gpl.html> <br/> This is free software: You are free to change and redistribute it. <br/> there is no warranty, to the extent permitted by law. type "show copying" <br/> and "show warranty" for details. <br/> This GDB was configu Red as "x86_64-linux-gnu ". <br/> for Bug reporting instructions, please see: <br/> <pttp://www.gnu.org/software/gdb/bugs/>... <br/> reading symbols from/home/supertool/test/scope... done. <br/> (GDB) L <br/> 20int & INT: Operator = (const Int & X) <br/> 21 {<br/> 22if (this! = & X) <br/> 23 value = x. value; <br/> 24 return * This; <br/> 25} <br/> 26 <br/> 27int main () <br/> 28 {<br/> 29int A (1); <br/> (GDB) L <br/> 30int B; <br/> 31 <br/> 32B = A; <br/> 33} <br/> (GDB) B 32 <br/> breakpoint 1 at 0x400635: file Scope. CPP, line 32. <br/> (GDB) r <br/> starting program:/home/supertool/test/scope <br/> breakpoint 1, main () at scope. CPP: 32 <br/> 32B = A; <br/> (GDB) S <br/> INT: Operator = (this = 0x7fffffffe230, x =...) at scope. cpp: 22 <br/> 22if (this! = & X) <br/> (GDB) S <br/> 23 value = x. value; <br/> (GDB) print value <br/> $1 = 0 <br/> (GDB) print X. value <br/> $2 = 1 <br/> (GDB) n <br/> 24 return * This; <br/> (GDB) print this <br/> $3 = (int * const) 0x7fffffffe230 <br/> (GDB) print * $3 <br/> $4 = {value = 1} <br/> (GDB) <br/>

Through the above program verification, this writing method is feasible. At least no error is reported in my editor. Here, we may confuse the above situation with the conventions that the subclass can only access the parent class and protect members. Further research is required.

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.