Home > Back-end >  Novice to solve simple problem c
Novice to solve simple problem c

Time:10-10

Class A
{
Public:
Int m;
Int * p=& amp; m;
};
A1 is an object of class A, if you want to through the pointer to the variable p m assignment 10, the corresponding statement is _____,

The correct answer to this question is * A1. The p=10;
I wrote the answer is: A1. * p=10;
I want to ask a question, why this pointer symbol in front of the object name? What is the meaning of this statement?

CodePudding user response:

. The priority is higher than *

CodePudding user response:

. * is an operator

It's called member access operators
A1. * p p is a pointer to member here, and you are a member of p,

 struct S{
S (int n) : mi (n) {}
Mutable int mi;
Int f (int n) {return mi + n. }
};

Struct D: public S
{
D: (int n) S (n) {}
};

Int main ()
{
Int S: : * pmi=& amp; S: : mi;
Int (S: : * pf) (int)=& amp; S: : f;

Const S S (7);
//s. * pmi=10;//error: unable to through the mutable modified
STD: : cout & lt;
D D (7);//the base class pointer can work on the derived class object
D * pd=& amp; d;
STD: : cout & lt; <(d. * pf) (7) & lt; <'
<(pd - & gt; * pf) (8) & lt; <'\ n';
}

CodePudding user response:

* A1. P=10;
Equivalent to * (A1) p), to a pointer to an int type variable p (here is to calculate A1. P value) solution reference [for int on point position space]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
A1. * p=10;
This is equivalent to directly to reference solution of member variables, one pace reachs the designated position
X. * y as a binary operator, pick up objects x y offset (declared) in advance on the data type of variable solution reference [purpose is the same as the beginning and the above]

More concrete is:

int (declaration of the variable type) the CLASSNAME: : (a statement which is relative to the case of the CLASSNAME object of this class (offset)) * (a statement which is a pointer) pp.
Such as:
& lt; 1> int Point: : * pp;
This sentence has three information:
1. * means that pp is a pointer
2. Point: : means that pp is a relative and Point a pointer on the objects of a class (specially for the Point object, under the domain of the Point class sense, other no, here also can see this is a for class level (rather than a specific object) pointer
3. Int means that pp is a pointer to int type (not to int, other)


Then the pointer assignment, we will make this pointer
& lt; 2 & gt; pp=& amp; Point: : y;//a pointer to the y coordinate point class, at this Point is not to the designated specific object pointer, citing only Point the class , and that some polymorphic forms and the two on the principle of concept is real, I think, to limit the scope of the basic practice can be adjusted according to the specific situation,

The code above, if the pp is understood as a deviation, a relative to an offset Point class, a class based on the Point of carrying type ( int ) of the migration will be more intuitive,

Then we can use the pp directly on the still and y coordinates of Point object to operate the
Such as
Point A; Point B; Point C;
Through the
& lt; 3 & gt; a. * pp=10; B. * pp=30; C. * pp=40; Such assignment,
XXXXX. * pp on our behalf to XXXXX y of this Point object, here. * as an operator, for pp offset into the position of ( & lt; 1> and & lt; 2 & gt; based on the definition and assignment statement) int solutions for reference

CodePudding user response:

The
reference reply: 3/f thousand dream life
* A1. P=10;
Equivalent to * (A1) p), to a pointer to an int type variable p (here is to calculate A1. P value) solution reference [for int on point position space]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
A1. * p=10;
This is equivalent to directly to reference solution of member variables, one pace reachs the designated position
X. * y as a binary operator, pick up objects x y offset (declared) in advance on the data type of variable solution reference [purpose is the same as the beginning and the above]

More concrete is:

int (declaration of the variable type) the CLASSNAME: : (a statement which is relative to the case of the CLASSNAME object of this class (offset)) * (a statement which is a pointer) pp.
Such as:
& lt; 1> int Point: : * pp;
This sentence has three information:
1. * means that pp is a pointer
2. Point: : means that pp is a relative and Point a pointer on the objects of a class (specially for the Point object, under the domain of the Point class sense, other no, here also can see this is a for class level (rather than a specific object) pointer
3. Int means that pp is a pointer to int type (not to int, other)


Then the pointer assignment, we will make this pointer
& lt; 2 & gt; pp=& amp; Point: : y;//a pointer to the y coordinate point class, at this Point is not to the designated specific object pointer, citing only Point the class , and that some polymorphic forms and the two on the principle of concept is real, I think, to limit the scope of the basic practice can be adjusted according to the specific situation,

The code above, if the pp is understood as a deviation, a relative to an offset Point class, a class based on the Point of carrying type ( int ) of the migration will be more intuitive,

Then we can use the pp directly on the still and y coordinates of Point object to operate the
Such as
Point A; Point B; Point C;
Through the
& lt; 3 & gt; a. * pp=10; B. * pp=30; C. * pp=40; Such assignment,
XXXXX. * pp on our behalf to XXXXX y of this Point object, here. * as an operator, for pp offset into the position of ( & lt; 1> and & lt; 2 & gt; based on the definition and assignment statement) int solutions of reference


The answer of the 1st floor pointy-haired belongs to me, not much, after seeing the response of the second floor, after further study, reply again,
  • Related