-
Previously, the reason was that the definition of a class was actually equivalent to a declaration of a type, and no storage space was allocated, and there was no place to store the initialized data, and there would be an entity storage address and space after the class was instantiated.
However, the C++11 standard allows initializing data members at class definition, like I am now using Visual Studio 2017 to develop C++ programs, which can initialize non-static data members in the class body.
-
Because classes can't allocate memory space, they can only declare what variables they have, and initialization is done in the constructor.
-
A list of initializations must be used to initialize members in a class.
class a
int a;
public:
a ()a(10){}
-
C++11, in-class initialization is already possible.
-
I don't quite understand, why would you put a colon after public.
-
Yes, data members of classes in C++ cannot be initialized, whether private or not.
Because a class is like a template for data, a new type of data that is custom combined, not a variable. When a program declares a class, it doesn't actually claim storage space for the program, it only applies to a class object defined by that class. There's not even space, so of course you can't initialize like defining variables.
-
Static private members cannot, non-static private members can.
class a ;
-
OK. But I don't understand what you mean by this definition, which refers to when the class object is created? When a class object is created, the class's initialization function is automatically called, and you only need to write their definitions in the initialization function if you want to initialize private member variables.
-
Not to mention private, even public ownership and protection cannot be initialized, and initialization should not be carried out in the constructor.
-
In C++, the data members of a class cannot be initialized at definition time unless it is static. In C++ you write declarations when you define classes. So if you want to initialize, go to the constructor.
-
This sub-compiler is different and can be initialized at definition time in some newer compilers (e.g. vs2015).
-
No, none of the member variables of a C++ class can be initialized at definition time.
Only those that have been declared static are allowed.
-
What does a private member of class 1 have to do with initialization?
2 Initialization is generally when an object is created, and the constructor initializes it.
-
I use VS2010 and don't support writing initialization values directly, so I can use it. For the sake of generality, let's write it into the constructor.
-
Class private members can be initialized using constructors.
-
No, or what else would the constructor do.
-
The constructor can be executed in two stages: (1) the initialization phase; (2) Ordinary computation stage (i.e. it can be those assignment statements and the like.) )
Regardless of whether the member shows initialization in the constructor initialization list, members of the class type are always initialized during the initialization phase.
For example, such a class:
class a ;
When executing constructor a (int ii), it must be initialized before the assignment in the constructor body can be performed.
If CI and RI are not initialized in the initialization list, they cannot complete the initialization operation. That is, the only chance to initialize a const or reference data member is in the constructor initialization list.
So the above constructor can be written as a::a(int ii) :ci(ii), ri(ii).
One thing to add: members of built-in types are not implicitly initialized, so the above member i does not need to be initialized in the initialization phase, and of course it can be initialized, but the efficiency is the same whether it is initialized in the initialization list or assigned in the function body. Namely.
a::a(int ii) :ci(ii), ri(ii) and a::
a(int ii) :i(ii), ci(ii), ri(ii) {Same efficiency.} All correct.
-
First, you know what initialization is and what assignment is!
First of all, a Class C has 254 addresses but there is always one as a gateway route, so there can be a maximum of 253 hosts. Then these hosts are public IP addresses. It can be done by setting up a LAN and using one IP. >>>More
Runs correctly after modification.
The problem is that the main function is also a member function of the class, and the local variables defined in the member function cannot be used by other member functions. >>>More
Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More
Because for Cao Cao, as long as Xu Shu is forcibly pulled into his camp, it is already a great success, and whether Xu Shu gives advice to himself or not is not so important, because his goal has been achieved. <> >>>More
Because your temporary files are stored in C: Documents and Settings Administrator Local Settings Temp >>>More