In C, non const objects and non const member functions can be called const member functions and why

Updated on technology 2024-04-01
3 answers
  1. Anonymous users2024-02-07

    1) The const object calls the st member function.

    2) Non-const member functions can call const member functions, which are generally used to avoid duplication, but introduce a casting action.

    3) Details can be referred to:

    effective

    c++(3e)

    p24 supplement.

    4) The Chinese version, the one translated by Hou Jie, I turned the book to yours.

  2. Anonymous users2024-02-06

    Constant member functions cannot change the private or public data defined in the class.

    Unless you add mutable.

    The const data member can be entered by the user in the main function.

    The premise is to initialize in the constructor.

    #include

    usingnamespace

    std;class

    boxprivate

    constint

    height;

    mutable

    intwidth;

    intlength;

    public:

    box(const

    inth=10,int

    w=2,int

    l=3):height(h),width(w),length(l){}

    voiddisplay()

    const;

    voidbox::display()

    constwidth=width*2;

    cout<

    a>>b>>c;

    boxb1(a,b,c);

    Let me give you a simple example.

  3. Anonymous users2024-02-05

    To put it simply: CONST represents read-only variables in C and constants in C++.

    C is the abbreviation of constant, which means constant and unchanging, and is also translated as constant, but many people think that the values modified by const are constants, but this is actually inaccurate. Because, to be precise, a read-only variable, its value cannot be used at compile time, because the compiler does not know what it stores at compile time. Maybe this keyword should have been replaced with readonly

    In C, the variables defined by the const only give the corresponding memory address, not the immediate number as in define, so the read-only variables defined by const have only one backup during the program run (because it is a global read-only variable and stored in the static area), while the macro variables defined by define have several backups in memory.

    C++ language:

    Constant type refers to the type specified by the type modifier const that cannot be updated.

    C++ was originally introduced to replace the precompile directive, eliminating its shortcomings while inheriting its advantages. "Disadvantages: It is only the substitution of simple values and **, the lack of type detection mechanism, and the safety is not good; There are three advantages:

    1.Avoid the appearance of ambiguous numbers and clarify the semantics of the program2Convenient parameter adjustment and modification, 3

    Improve the efficiency of program execution, as there is no need to allocate space for constants

Related questions
16 answers2024-04-01

What non-enterprise questions have questions, I can introduce you to the bidding promotion. >>>More

17 answers2024-04-01

Find out why students don't complete their assignments and solve the problem. >>>More

7 answers2024-04-01

Even if it is carried out, it will be carried out in Scarborough Shoal and the Philippines itself, and the impact on our country will be minimal, and even in the southeastern coastal areas, there will be sufficient military strength to protect the safety of the people, and naval warfare, air warfare, and landing warfare are the main forms.

4 answers2024-04-01

The four-stage Taijiquan, also known as the 32-style Taijiquan, is a set of comprehensive Taijiquan based on the Yang style combined with other forms. Among them: 1, the right "press" type of the bird's tail is Wu style, 2, the progress of moving the block is the Sun style, 3, the right kick is the Chen style, 4, the left and right shuttle is the Wu style, 5, the lower potential is the Wu style, 6, the golden rooster independence is the martial style. >>>More

7 answers2024-04-01

Conventional C10, C15, C20, C25, C30 concrete mix such as the figure below. >>>More