Structure in c++

Asked 2 years ago, Updated 2 years ago, 24 views

in the c++ structure Is it more common to use typeef? Or don't you use it well?

c++

2022-09-22 15:14

2 Answers

When using the structure in C language, you should put the structure in front of the name as shown below.

struct A {};
struct A a;

It is cumbersome to use structure every time, so if you do not use forward declaration, use typeef as shown below.

typedef struct {} A;
A a;

On the other hand, the C++ letter does not require the use of structures. You do not use structure, but it is the same in C++, so there is no need to name the structure with Guji typeef.

struct A {};
A a;

Moreover, names defined as typef cannot be declared forward, so there is no benefit from using typef. So it's usually used to declare structures only with structures rather than typeef, right?

However, for compatibility with C language, typeef is used. For example, a C++ code is shown below.

struct A {};
void func(A a);

In this case, a compilation error occurs because there is no structure before A for C++.

If the C++ code is written as shown below, it can be used in C and also in C++.

typedef struct {} A;
void func(A a);

If the code written in C++ must be compatible with C, typeef will be used, but the structure is usually defined only by structure.


2022-09-22 15:14

It's different for each person.

#include <iostream>

int main()
{
    typedef struct cStruct
    {
        //  //  Somthing..
        float af;
        short bs;
    }*pStruct;

    pStruct pTest = new cStruct();

    pTest->af = 5.0f;

    delete pTest;
    pTest = nullptr;

    system("pause");
    return 1;
}

I also use it at times like this.


2022-09-22 15:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.