C++_ how to get string contents within a class

Source: Internet
Author: User

You encounter a problem in your project that you want to get the string contents of the class outside of the class.

The content cannot be modified by passing a string pointer directly

Possible methods:

1. Passing the address of a string array

2. Passing a string level two pointer, which is a string address

3. Pass a string reference in C + + and directly modify it by reference



The way to fail: (Intercept code: Encapsulates a function into a class,.... The code is too many, the overall test code at the end of the article. Interested classmates copy themselves)

void Getstringc (char *a) {a = "hi,kemeng~: GETSTRINGC";}

char * a2 = null;test. GETSTRINGC (A2);p rintf ("\n%s\n", A2);


You can see that the actual value of A2 has not changed.



In fact, the passing of parameters is one-time assignment:

An argument assignment to a parameter is analogous to the following code:

printf ("\ntest of Assign of Char pointer\n" "Print their address\n"); char * p2 = "Hello, kemeng~"; char * p3 = p2;printf ("P2 :%p    p3:%p\n ", p2, p3);
P2 is like the argument A2 above, p3 equals the formal parameter a.

Output:


Such a copy is equivalent to P2 and P3 point to a piece of address, this address holds the constant string "Hello, kemeng~"

But p2. The address of the P3 pointer is not the same

printf ("\ntest of Assign of Char pointer\n" "Print their address\n"); char * p2 = "Hello, kemeng~"; char * p3 = p2;printf ("P2 :%p    p3:%p\n ", p2, p3);p rintf (" Address of p2:%p, address of p3:%p\n ", &P2, &P3);


Here we have to say:

Char *p = "Hi, Kemeng";

Here the string "Hi, Kemeng" is non-modifiable


See below for reasons:

2. Is "abc" a constant? The answer is sometimes, sometimes not.

  is not a constant condition : "abc" is not the initial value of a character array , as
char str[] = "abc";
because a character array is defined, it is equivalent to defining some space to hold "abc", and because
The character array is the character that holds the characters one by one, so the compiler parses the statement into
char str[3] = {' A ', ' B ', ' C '};
also according to the above summary 1, so char str[] = "abc"; The end result is
char str[4] = {' A ', ' B ', ' C ', ' n '};
do an extension, if char str[] = "ABC"; it's written inside the function, so here
"Abc\0" is not a constant, so it should be placed on the stack.
  
  is a constant condition:when assigning "abc" to a character pointer variable, such as
char* ptr = "abc";
because the definition is a normal character pointer, and there is no space defined to hold "ABC", so the compiler has to help us
find a place to put "ABC", Obviously, the "ABC" here as a constant and put it in the program's constant area is the compiler
The most suitable choice.

Related articles Link:

http://blog.csdn.net/u010003835/article/details/48087629


Therefore, the following methods are not feasible:

void Changestringa (char *a) {a[0] = ' H '; a[1] = ' I '; a[2] = ': '; a[3] = ' + ';}

printf ("\ntest. Changestringa (hh): \ n "); char *hh =" Hello, kemeng~ ";p rintf ("%s\n ", hh), test. Changestringa (HH);//errorprintf ("%s\n", HH);





But the space referred to by the pointer P can be modified (pointing to other locations)

printf ("\ntest of change char pointer content\n"); char * P4 = "Hello, kemeng~";p rintf ("P4:%s\n", p4);p 4 = "Hi";p rintf ("P4 :%s\n ", p4);




Here are a few possible ways to do this:


1. Pass the address of the string array (two notation)

<pre name= "code" class= "CPP" >void changestringb (char *a) {strcpy (A, "Hi:");} void CHANGESTRINGC (char a[]) {strcpy (A, "Hi:");}

printf ("\ntest. CHANGESTRINGB (dd): \ n "), char dd[100] =" Hello, kemeng~ ";p rintf ("%s\n ", DD), test. CHANGESTRINGB (DD);p rintf ("%s\n", DD);p rintf ("\ntest. CHANGESTRINGC (FF): \ n "), char ff[100] =" Hello, kemeng~ ";p rintf ("%s\n ", ff), test. CHANGESTRINGC (FF);p rintf ("%s\n", FF);





2. Pass a character level two pointer, that is, the address of the character pointer (method ugly is not recommended)

void Getstringb (char **a) {*a = "hi,kemeng~: GETSTRINGB";}

char * AA = null;test. GETSTRINGB (&AA);p rintf ("\n%s\n", AA);






3. Pass a string reference in C + + and directly modify it by reference

void Getstringa (String &a) {a = "hi,kemeng~: Getstringa";}
String cstring;test. Getstringa (CString);p rintf ("\n%s\n", Cstring.c_str ());





A complete test code is also attached for your reference:

#include <iostream> #include <string>using namespace std; #pragma warning (disable:4996) class A{public:void Getstringa (String &a) {a = "hi,kemeng~: Getstringa";} void Getstringb (char **a) {*a = "hi,kemeng~: GETSTRINGB";} void Getstringd (char *a) {strcpy (A, "hi,kemeng~: Getstringd");} void Getstringe (char a[]) {strcpy (A, "hi,kemeng~: Getstringe");} void Getstringc (char *a) {a = "hi,kemeng~: GETSTRINGC";} void Printstringaddress (char *a) {printf ("%p\n", a);} void Printcharpointaddress (char *a) {printf ("%p\n", &a);} void Changestringa (char *a) {a[0] = ' H '; a[1] = ' I '; a[2] = ': '; a[3] = ' + ';} void Changestringb (char *a) {strcpy (A, "Hi:");} void CHANGESTRINGC (char a[]) {strcpy (A, "Hi:");}}; int main () {A test;string cstring;test. Getstringa (CString);p rintf ("\n%s\n", Cstring.c_str ());//char * aa = null;//test. GETSTRINGB (&AMP;AA);//printf ("\n%s\n", aa);//char * A2 = null;//test. GETSTRINGC (A2);//printf ("\n%s\n", a2);//printf ("\ntest". Printstringaddress (BB): \ n ");//char *bb =" Hello, kemeng~ ";//printf ("%p\n", BB);//test. Printstringaddress (BB);//printf ("\ntest. Printcharpointaddress (C2): \ n ");//char *c2 =" Hello, kemeng~ ";//printf ("%p\n ", &c2);//test. Printcharpointaddress (C2);//printf ("\ntest of Assign of Char pointer\n"//"Print their address\n");//char * P2 = "Hello, Ke Meng~ ";//char * p3 = p2;//printf (" p2:%p p3:%p\n ", p2, p3);//printf (" Address of p2:%p, address of p3:%p\n ", &AMP;P2, &am P;P3);//Note that "Hello, kemeng~" is stored in the string memory space//p2[1] = '%s\n ';//printf ("The", p2);//printf ("\ntest of change char pointer conten T\n ");//char * P4 =" Hello, kemeng~ ";//printf (" P4:%s\n ", p4);//p4 =" Hi ";//printf (" P4:%s\n ", p4);////printf (" \ntest ". CHANGESTRINGC (cc): \ n ");//char *cc =" Hello, kemeng~ ";//printf ("%s\n ", cc);//test. CHANGESTRINGC (cc);//error//printf ("%s\n", cc);//printf ("\ntest. CHANGESTRINGC (C3): \ n ");//char *c3 =" Hello, kemeng~ ";//printf ("%s\n ", &c3);//test. CHANGESTRINGC (C3);//error//printf ("%s\n", C3);//printf ("\ntest". CHANGESTRINGB (dd): \ n ");//char dd[100] =" Hello, kemeng~ ";//printf ("%s\n", DD);//test. CHANGESTRINGB (DD);//printf ("%s\n", DD);//printf ("\ntest. CHANGESTRINGC (FF): \ n ");//char ff[100] =" Hello, kemeng~ ";//printf ("%s\n ", ff);//test. CHANGESTRINGC (FF);//printf ("%s\n", FF);//printf ("\ntest. CHANGESTRINGC (GG): \ n ");//char gg[100] =" Hello, kemeng~ ";//printf ("%s\n ", GG);//test. Changestringa (GG);//printf ("%s\n", GG);//printf ("\ntest. Changestringa (hh): \ n ");//char *hh =" Hello, kemeng~ ";//printf ("%s\n ", hh);//test. Changestringa (HH);//error//printf ("%s\n", hh); return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C++_ how to get string contents within a class

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.