Can a pointer to an uninitialized class access a member of a class?

Updated on technology 2024-05-13
19 answers
  1. Anonymous users2024-02-10

    In fact, when calling the member function of the class, the value of this will be passed into the first parameter by default, which is mainly used to access the member variables of the class.

    If you don't have any member variables (the space allocated for the object) referenced in your implementation of a member function, you won't be wrong to call them with a null pointer because you're not using them in your implementation. For example:

    class b

    public:

    void test()

    printf("It doesn't core off when runtime");

    void test2()

    printf("An error is reported when you run the following ** because the reference to a member variable is based on the 0 address");

    a = 100;

    private:

    int a;

    int main()

    b *lpb = null;

    lpb->test();

    lpb->test2();

    return 0;

  2. Anonymous users2024-02-09

    Because those two functions are static, the object of your class is only 8 bytes, and pb->fun() replaces it for you at compile time, so it won't cause an error.

    And, even if this doesn't report an error, this one goes against the original purpose of object orientation, and it's good to know, but it's not necessary, because if fun uses member variables, the consequence will be a crash.

  3. Anonymous users2024-02-08

    Please don't get confused.

    All functions are put together when the program is compiled and connected, called the area, and do not exist regardless of whether they are static or non-static functions"When constructing an object of a class (the first time the object is constructed), a block space is allocated to the member function (non-static member function).", but when the program is loaded, all ** already exist in the memory space. Only the local variables used in the function are allocated space when the function is called.

    When creating an object, the compilation system allocates memory space only for the member data (member variables) in the object, even if the following:

    #include

    using namespace std;

    class a

    int main()

    But it will be wrong if it is as follows:

    #include

    using namespace std;

    class a

  4. Anonymous users2024-02-07

    Because if a class contains objects of its own class, there is the problem of infinite initialization.

    To construct an object of a class, you need to construct the object of its own class before calling the constructor. When constructing an object of its own class, you must first construct its own object, and then call its constructor ......

  5. Anonymous users2024-02-06

    Question 1:

    Question 2: Pointer access data is accessed with ->. If you have to. to access, you can write like this: (*p[0])data

    The correct ** should look like this:

    #include

    using namespace std;

    class a

    class b

    b::b()

    void b::f()

    int main(void)

  6. Anonymous users2024-02-05

    Here's how:

    Your original program only defines pointers, and does not instantiate pointers to classes, and you directly use pointers without instantiation to access uncreated objects, which of course cannot be run.

    p[0] is equivalent to *(p+0), which is no longer a pointer type, and in this case it represents the first position of the array of objects a.

  7. Anonymous users2024-02-04

    Yes, but indirectly. Take a look at the example below.

    #include

    using std::cout;

    using std::endl;

    class a{

    int data;

    public: a(int i):data(i){cout<<"new"cout

  8. Anonymous users2024-02-03

    To access a non-static member, a static member function must use a usage object as a parameter.

  9. Anonymous users2024-02-02

    You need to define a class, and then point the class pointer to the address of the class, which can also be initialized as null, but the null pointer can only call class member functions that do not involve class member variables.

    Example: include

    using namespace std;

    class test

    int get()

    test():a(1),b(2){}

    public:

    int a,b;

    int main()

    Note: Class pointers initialized to null can safely call class member functions that do not involve class member variables without error, but if class member variables are called in class member functions, errors will occur, since the assignment to null can be used, then naturally uninitialized class pointers also meet this kind of situation.

  10. Anonymous users2024-02-01

    It's not wrong to assign a null value to initialize. If you make a mistake with the variable used in the assignment statement that follows, check that the variable has been initialized.

  11. Anonymous users2024-01-31

    The initialization must be null, and then point to the instance of the class when it is needed.

  12. Anonymous users2024-01-30

    What type of cicmian is, it depends on whether it is modal or non-modal, and the non-modal one is new and finished. If it is a view class, getActiveView or get the document template and then find the view class.

  13. Anonymous users2024-01-29

    cicmian *icmain=null;

    Here you initialize the pointer to be empty, which means the pointer is not pointing at anything.

    If you call his members again, you're bound to make a mistake.

    The process should probably look like this.

    clcmain m;

    .icmian *icmain=null;

    icmain = &m;

    icmain->m_>hok);

  14. Anonymous users2024-01-28

    Oh, how can a null pointer be used directly, it must be pointed at an entity, such as:

    clcmain *lcmain = new clcmain;

    or clcmain tmpmain;

    clcmain *lcmain = &tmpmain;

  15. Anonymous users2024-01-27

    cicmian *icmain=null;

    icmain->m_>hok);Dead here, icmain is not instantiated, first point icmain to a specific object.

  16. Anonymous users2024-01-26

    clcmian *lcmain;

    memset(lcmain,0,sizeof(clcmian));Allocate space.

    Assign a value again.

  17. Anonymous users2024-01-25

    The first thing to do is to clarify whether your B is the user or owner of A.

    1.If you just use a, you can get the value of the pointer to a and use it without making sure it is null. When destructing, you can set the pa to null.

    2.If B is the owner of A, and the lifecycle of A needs to be managed by B, then B's initial function should initialize A, and then initialize A's pointer. It is a good practice to use a mempool to manage this memory space.

    When destructing, you need to deconstruct the address referred to by PA.

    I'm more inclined to the 2 approach.

  18. Anonymous users2024-01-24

    The first pointer who new deletes so can't be in the destruct

    Second, that's what the first point came about.

    First of all, you a a; After the scope of variable A ends, it will be destructed, and secondly, because the destructor of your class b deletes a, the destructor inside a will also be called.

    Why do developers need to see how addresses are generated?

    People give you the address, you just use it, you can judge whether the address is null

  19. Anonymous users2024-01-23

    Personally, I think.

    b should not provide delete a in the destructor

    Because who writes the new a() here in b b1(new a()), then the a here should be released by whom.

    B has no obligation to know whether A is in the heap or in the stack, and has no right to release A without permission

Related questions
13 answers2024-05-13

In fact, this is a simple question of variable type and variable function, I think it should be explained like this: first, the original purpose of variable definition is to use the variable we define to meet the needs of our program, so, above you define variable a, then later, you will use this variable to do some operations, in most cases, it is to be initialized, so, give a corresponding type of value to initialize the variable, so that some operations can be used in the future, second, the understanding of that sentence:" You want a to represent 5, so you want to assign the value of a to 5", in fact, it is an initialization process of the variable a, where a is int (integer), corresponding to an initial value of 5, which is expressed like this: >>>More

9 answers2024-05-13

The number of electrons in the valence shell of the central atom + the number of electrons provided by the accessory atom - valence state) 2 The number obtained is the number of hybrid orbitals, and then the hybridization method is selected according to this number.

15 answers2024-05-13

Hello, you can become a bank clerk through external employment, similar to security guards. >>>More

13 answers2024-05-13

In the diet of vitiligo patients, many seafood is inedible, but some seafood like shellfish, such as clams, oyster clams, etc., are edible, so patients can eat some of these foods, but do not eat too much. Suggestions: Whole grains have the effect of preventing diseases, can make people healthier, help the body remove toxins and other benefits, which contains a variety of nutritional value is also very high, for vitiligo patients who need to supplement tyrosine and minerals, whole grains are a good choice.

11 answers2024-05-13

For example, the Three Kingdoms Kill is to learn tactics while playing, I think this game is still very good, and you can also play some card games, such as the heroes, I think their game can also let you learn a lot of strategies from it. It's still fun. <>