Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Data Types

TypeDef

Typedef in C++ is a keyword used to create aliases or alternative names for existing data types. It's a way to simplify complex type declarations and make code more readable.

  1. Basic Syntax:

    typedef existing_type new_type_name;
  2. Simple Example:

    typedef unsigned long ulong;
    ulong myNumber = 1000000UL;
  3. Array Typedef:

    typedef int IntArray[5];
    IntArray myArray = {1, 2, 3, 4, 5};
  4. Pointer Typedef:

    typedef int* IntPtr;
    IntPtr ptr = new int(10);
  5. Function Pointer Typedef:

    typedef int (*MathFunc)(int, int);
    int add(int a, int b) { return a + b; }
    MathFunc operation = add;
  6. Struct Typedef:

    typedef struct {
        int x;
        int y;
    } Point;
    
    Point p = {10, 20};
  7. Complex Types:

    typedef std::vector<std::pair<std::string, int>> NameScorePairs;
    NameScorePairs scores;
  8. Template Typedef (C++11 and later):

    template<typename T>
    using Vec = std::vector<T>;
    
    Vec<int> numbers = {1, 2, 3, 4, 5};
  9. Improving Readability:

    typedef std::unique_ptr<MyClass> MyClassPtr;
    MyClassPtr ptr = std::make_unique<MyClass>();
  10. Portability:

    typedef long long int64;  // Ensures 64-bit integer across platforms

Example:

#include <iostream>
#include <vector>
#include <string>

// Simple typedef
typedef unsigned int uint;

// Function pointer typedef
typedef int (*Operation)(int, int);

// Struct typedef
typedef struct {
    std::string name;
    int age;
} Person;

// Complex type typedef
typedef std::vector<std::pair<std::string, int>> ScoreBoard;

// Function to use with function pointer
int add(int a, int b) {
    return a + b;
}

int main() {
    uint number = 42;
    std::cout << "Number: " << number << std::endl;

    Operation op = add;
    std::cout << "10 + 20 = " << op(10, 20) << std::endl;

    Person alice = {"Alice", 30};
    std::cout << alice.name << " is " << alice.age << " years old." << std::endl;

    ScoreBoard scores = {{"Bob", 95}, {"Charlie", 88}};
    for (const auto& score : scores) {
        std::cout << score.first << " scored " << score.second << std::endl;
    }

    return 0;
}

Key points about typedef:

  • It doesn't create a new type; it just introduces a new name for an existing type.

  • It can make complex declarations more readable.

  • It's often used to create platform-independent type definitions.

  • In modern C++ (C++11 and later), the using keyword can be used similarly to typedef and is more flexible, especially with templates.

PreviousEnumNextconst in c++

Last updated 9 months ago

Run it .

🪅
here