Ask for advice, a pointer of class C.

Updated on technology 2024-05-24
5 answers
  1. Anonymous users2024-02-11

    I took a hard look at your question today.

    That is true. base *p stands for p as a pointer to the base class type. This is condition one.

    p=&obj1 represents p as the base class pointer to the subclass object. It's okay. Here's an example:

    The base class is "people".

    A subclass is a "student" derived from a "person".

    So p stands for the type "person", so point p to "student", which of course is possible, because the student inherits the type of the base class "person".

    Other words. The pointer p of the base class type points to the object obj1 of the subclass type. Together with condition one, it constitutes a necessary condition for the realization of polymorphism.

    So if you call a virtual function in the base class with p, you can implement polymorphism (provided that there is a cover function of the virtual function in the subclass), which means that the cover function in the child class object is called according to the subclass object that p points to.

  2. Anonymous users2024-02-10

    First of all, in C++, the conversion of pointers between inherited relationships is generally done with a secure transformation dynamic cast.

    Second, in an insecure conversion, the compiler won't help with checks, so there will be no errors at compile time. If the print of the subclass does not involve member functions or member variables that are not contained in the base class, there will be no error when running. However, if there is a subclass of custom elements, an error will be reported.

    Besides, it's a very strange way for you to write like this. Why does the base class call the subclass's functions? If you mean to use polymorphism, it should be.

    base* obj4 = new first_d(4);

    obj4->print();print() is a virtual function.

    This is the first d print().

  3. Anonymous users2024-02-09

    The pointer to the base class can point to the part of the derived class object that inherits from the base class, which we call polymorphism.

    If both your obj2 and obj3 prints inherit to the base class, then the call is valid.

  4. Anonymous users2024-02-08

    It can be defined like this. In the loop, the scope of the variable is in the two {} of the loop, so it is not defined repeatedly. If the definition is repeated, the compiler will report an error.

  5. Anonymous users2024-02-07

    1.Definition.

    Type: * Pointer Variable;

    The type determines the size of the memory space to which it is pointed.

    A pointer variable is also a variable that has its own memory space, which is stored on top of another variable's memory space.

    You can use typedef aliases to reduce some of the hassle of defining variables, such as typedef int* pointer;

    2.Operate.

    1) Take the address.

    int* p; int x; p = x;p points to the address of x, the type of p is int*, and the type of x is also int*

    2) Indirect access.

    For general pointer variables, the access format is: *Pointer Variables

    The pointer variable of the structure type, the access format is: (*pointer variable).Structure Members or Pointer Variables - >Structure Members

    3) Assignment. Any type of pointer can be assigned to a pointer variable of type void *, while a pointer variable of type non-void * can only accept assignments of the same type.

    4) Pointer operations.

    A pointer plus or minus an integer value: Data Type * Pointer Variable; int a;"Pointer Variables" +a; It can be understood as a change in the subscript in the array, "Pointer Variable" = Pointer Variable + (a*sizeof).

    Subtract two pointers of the same type: the result is an integer value, which corresponds to the number of elements in the storage space, which can be used to find the size of the array.

    5) The output of the pointer.

    Pointer variable of type non-char*: cout<

Related questions
9 answers2024-05-24

Scope. You static char *chh;

static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More

19 answers2024-05-24

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. >>>More

17 answers2024-05-24

The reason for this problem is that there is a problem with the create function of the linked list, and the problem is located in the red box in the figure. >>>More

20 answers2024-05-24

First, the mode is different.

Class A** is to directly deduct the subscription fee when subscribing, which is usually a "front-end charge" model. The front-end charging model is the most commonly used charging mode for current investors, and the over-the-counter charging mode that we usually subscribe to is generally the front-end charging model. >>>More

6 answers2024-05-24

c, the assignment operation requires that the lvalue is of the same type as the rvalue. >>>More