본문 바로가기
College Study/C++ (ENG)

[C++] C++ 11/14/17/... New Types (ENG)

by 2den 2022. 1. 2.
728x90

 

nullptr

The old C style of NULL is a number.

// Class.h
float GetScore(const char* name);  // meant to call this
float GetScore(int id);            // actual

// Main.cpp
#define NULL 0
Class* myClass = new Class("COMP3100");
// ...
int score = myClass->GetScore(NULL);
 

So in C++ 11, you can use nullptr as a pointer.

typedef decltype(nullptr) nullptr_t;  // null pointer constant
 
int number = NULL;            // OK
int* ptr = NULL;              // OK

int anotherNumber = nullptr;  // error
int* anotherPtr = nullptr;    // OK
 

Use nullptr. Don't use NULL anymore.

 

// Main.cpp
Class* myClass = new Class("COMP3200");

const Student* student = myClass->GetStudent("Coco");
if (student != nullptr)
{
    std::cout << student->GetID() << ":" << student->GetName() << std::endl;
}
 

 

 

Fixed-width Integer

Previously, types occupy the following memory:

char 1 byte, short 2 bytes, long 4 bytes, long long 8 bytes, and int 4 bytes. But it is not standard.

 

From C++ 11, you can use fixed-width integer types.

int8_t / uint8_t

int16_t / uint16_t

int32_t / uint32_t

int64_t / uint64_t

intptr_t / uintptr_t

 

There is a link for reference below.

 

Use these types for readability.

int8_t score = student->GetScore();
 

 

 

enum class

The enum in C is not a seperate type. The compiler just converts it to int type. (We used to declare an array with count or run a for statement.) As a result, problems occurres when comparing enums.

// Main.cpp
enum eScoreType
{
    Assignment1,  // 0
    Assignment2,  // 1
    Assignment3,  // 2
    Midterm,      // 3
    Count,        // 4
};

enum eStudyType
{
    Fulltime,     // 0
    Parttime,     // 1
};

int main()
{
    eScoreType type = Midterm;
    eStudyType studyType = Fulltime;

    int num = Assignment3;  // no error! 

    if (type == Fulltime).  // neither! uh-oh.
    {
        // ...
    }

    return 0;
}
 

 

To solve these problems, enum class is made.

// Main.cpp
enum class eScoreType
{
    Assignment1,
    Assignment2,
    Assignment3,
    Midterm,
    Count,
};

enum class eStudyType
{
    Fulltime,
    Parttime,
};

int main()
{
    eScoreType score = eScoreType::Midterm;
    eStudyType studyType = eStudyType::Fulltime;

    int num = eScoreType::Assignment3;  // error 

    if (score == eStudyType::Fulltime).  // error
    {
        // ...
    }

    return 0;
}
 

 

enum class is properly supported in C++ 11. There is no implicit casting as an integer. (If you really want to, ypu can, though.) Type checking is now possible. You can also specify the amount of bytes to allocate for the enum.

// Class.h
#include <cstdint>

enum class eScoreType : unit8_t
{
    Assignment1,
    Assignment2,
    Assignment3,
    Midterm,
    Final = 0x100,  // warning
};
 

 

Use enum class except when initializing an array statically.

 

And you have to write a little bit longer code for the for statement.

// compile error
for (int i = eScoreType::Assignment1; i < eScoreType::Count; ++i) { /*...*/ }

// OK
for (int i = static_cast<int>(eScoreType::Assignment1);
        i < static_cast<int>(eScoreType::count); ++i) { /*...*/ }
 

 

 

Initialize in the Header

C++ 20 module

 

 

 


 

728x90

댓글