C solves the problem of why you can t initialize values for data members in a class

Updated on technology 2024-02-28
18 answers
  1. Anonymous users2024-02-06

    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.

  2. Anonymous users2024-02-05

    Because classes can't allocate memory space, they can only declare what variables they have, and initialization is done in the constructor.

  3. Anonymous users2024-02-04

    A list of initializations must be used to initialize members in a class.

    class a

    int a;

    public:

    a ()a(10){}

  4. Anonymous users2024-02-03

    C++11, in-class initialization is already possible.

  5. Anonymous users2024-02-02

    I don't quite understand, why would you put a colon after public.

  6. Anonymous users2024-02-01

    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.

  7. Anonymous users2024-01-31

    Static private members cannot, non-static private members can.

    class a ;

  8. Anonymous users2024-01-30

    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.

  9. Anonymous users2024-01-29

    Not to mention private, even public ownership and protection cannot be initialized, and initialization should not be carried out in the constructor.

  10. Anonymous users2024-01-28

    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.

  11. Anonymous users2024-01-27

    This sub-compiler is different and can be initialized at definition time in some newer compilers (e.g. vs2015).

  12. Anonymous users2024-01-26

    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.

  13. Anonymous users2024-01-25

    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.

  14. Anonymous users2024-01-24

    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.

  15. Anonymous users2024-01-23

    Class private members can be initialized using constructors.

  16. Anonymous users2024-01-22

    No, or what else would the constructor do.

  17. Anonymous users2024-01-21

    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.

  18. Anonymous users2024-01-20

    First, you know what initialization is and what assignment is!

Related questions
10 answers2024-02-28

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

8 answers2024-02-28

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

6 answers2024-02-28

Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More

17 answers2024-02-28

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

5 answers2024-02-28

Because your temporary files are stored in C: Documents and Settings Administrator Local Settings Temp >>>More