Home > Back-end >  C object assignment problem
C object assignment problem

Time:09-26

The passing years, save the children: between objects under the assignment should be in the same type of why the first box assignment is not an error, the second frame will be an error, how do I change the second box?

CodePudding user response:

Can you understand so
Subclass is on the basis of the parent class extension, in a sense a subclass of memory information is greater than (or at least not less than) the parent class of memory information (more information on the memory of the parent information)
So
Parent class variables=subclass, less memory information, because the parent class variables after reduced the memory space of the subclass is assigned to the parent class variables, not cause memory cross-border (already belong to a subclass of memory space, how to narrow the memory still in legal space), so no problem
Subclass=parent class variables, because the subclass of variable memory information, expand the memory space of the parent after assigned to subclass variables, can cause memory cross-border (extended to the originally do not belong to the memory of the parent), so there is a problem

CodePudding user response:

May be backwards compatible animal, that is, the upstairs said, parent and child classes, for an example of inappropriate, just like the relationship between the string and char, string=char and do not lose the details of the char, and char=the string is not possible

CodePudding user response:

reference 1st floor qybao response:
can you so understand
Subclass is on the basis of the parent class extension, in a sense a subclass of memory information is greater than (or at least not less than) the parent class of memory information (more information on the memory of the parent information)
So
Parent class variables=subclass, less memory information, because the parent class variables after reduced the memory space of the subclass is assigned to the parent class variables, not cause memory cross-border (already belong to a subclass of memory space, how to narrow the memory still in legal space), so no problem
Subclass=parent class variables, because the subclass of variable memory information, expand the memory space of the parent after assigned to subclass variables, can cause memory cross-border (extended to the originally do not belong to the memory of the parent), so there is a problem

So how do I change?

CodePudding user response:

If you are not afraid of memory cross-border problems, you can try the forcibly converting
D2=* ((Dog *) & amp; A1);

CodePudding user response:

reference 4 floor qybao response:
if you are not afraid of memory cross-border problems, you can try the forcibly converting
D2=* ((Dog *) & amp; A1);

Bosses in addition to the forced conversion: are there any other way?

CodePudding user response:

refer to 2nd floor N - N - N response:
may be backwards compatible animal, also is the upstairs said, parent and child classes, for an example of inappropriate, just like the relationship between the string and char, string=char and do not lose the details of the char, and char=string is can't

Uh-huh, I see, thank you big support,

CodePudding user response:

refer to fifth floor LINUX/response:
Quote: refer to 4th floor qybao response:
if you are not afraid of memory cross-border problems, you can try the forcibly converting
D2=* ((Dog *) & amp; A1);

Bosses in addition to the forced conversion: are there any other way?

The memory copy
Memcpy (& amp; B2, & amp; A1, sizeof (b2));

CodePudding user response:

refer to 7th floor qybao response:
Quote: LINUX/reference 5 floor response:

Quote: refer to 4th floor qybao response:
if you are not afraid of memory cross-border problems, you can try the forcibly converting
D2=* ((Dog *) & amp; A1);

Bosses in addition to the forced conversion: are there any other way?

The memory copy
Memcpy (& amp; B2, & amp; A1, sizeof (b2));

Who can explain to me the copy of memory?

CodePudding user response:

refer to 7th floor qybao response:
Quote: LINUX/reference 5 floor response:

Quote: refer to 4th floor qybao response:
if you are not afraid of memory cross-border problems, you can try the forcibly converting
D2=* ((Dog *) & amp; A1);

Bosses in addition to the forced conversion: are there any other way?

The memory copy
Memcpy (& amp; B2, & amp; A1, sizeof (b2));

Who can explain to me the copy of memory?

CodePudding user response:

refer to 7th floor qybao response:
Quote: LINUX/reference 5 floor response:

Quote: refer to 4th floor qybao response:
if you are not afraid of memory cross-border problems, you can try the forcibly converting
D2=* ((Dog *) & amp; A1);

Bosses in addition to the forced conversion: are there any other way?

The memory copy
Memcpy (& amp; B2, & amp; A1, sizeof (b2));

Who can explain to me the copy of memory?
  • Related