Home > Back-end >  C read member variables
C read member variables

Time:05-27

In c + + posts before a friend pointed out about read-only variable problem, in fact not so convenient, c + + only through function to solve this,
Such as:
Struct SProto
{
Private:
Int32 id_;
Int32 idx_;
Int32 day_;
Int32 plan_.
Int32 need_;
Int32 x_;
Int32 y_;
}
This configuration type, don't want to external can change its value, then how many functions to do? If members more?
So c + + and is worth to change places, is to increase a way, external read-only mode, it will save you write users read the function of the time,

CodePudding user response:

You can in the get function with one parameter, the parameters selected for which members,
The key value

CodePudding user response:

Don't want to write their own compiler generally has its own complement the function of the code, click on the variable, and then press ALT + ENTER keyboard will appear intelligent window, choose the filling line, or, as the upstairs said, through a function, construct a mapping table, can also be output

CodePudding user response:

The proposal is worth thinking about

CodePudding user response:

Public:
Const

Or directly to this period of memory mapped out,

CodePudding user response:

Now, the compiler function and a strong

CodePudding user response:

Can use the macro definition
# define pridata (type, name, refix) \
Privata: \
Type the name=value; \
Public: \
Type refix# # name () \
{\
return name; \
}

CodePudding user response:

reference 1st floor zjq9931 response:
you can in the get function with one parameter, the parameter selected for which members,
The key value of

So have to be a pile of the if the else
Purpose is to be more convenient, but does not currently support


CodePudding user response:

refer to 6th floor weixin_44507928 response:
can use the macro definition
# define pridata (type, name, refix) \
Privata: \
Type the name=value; \
Public: \
Type refix# # name () \
{\
return name; \
}


Macros can indeed than if the else province a little thing, but did not reach the purpose of more convenient, thank you for your reply

CodePudding user response:

Direct written const no

 class A {
Public:
Const int a;
Const int b;
M A (int, int n) : (m), b (n) {

}
};
Int main () {
A, a1} {1, 2.
A1. A=10;//error C3892 "a1" : can't give constant assignment Project31 D: \ Documents \ Tencent Files \ 393121937 \ FileRecv \ 1. CPP 21

return 0;


}

  • Related