Proper way to create static instance of a class inside of itself

chasep255

I have a pixel struct. I want to decalre static constants inside of it for colors like BLACK and WHITE. I am writing all of the in a header file which will be included in multiple cpp files.

struct Pixel
{
    typedef uint16_t quantum_t; 
    static const quantum_t MAX_QUANTUM = 0xffffL;
    static const quantum_t MIN_QUANTUM = 0;
    static const int QUANTUM_BITS = 16;

    quantum_t r, g, b;

    Pixel(quantum_t r_ = 0, quantum_t g_ = 0, quantum_t b_ = 0) :
        r(r_), g(g_), b(b_) { }

    template<typename T>
    static T clamp(T x)
    {
        return x > MAX_QUANTUM ? MAX_QUANTUM : (x < MIN_QUANTUM ? MIN_QUANTUM : x);
    }

    template<typename T>
    static Pixel clamp(T r, T g, T b)
    {
        return Pixel(clamp(r), clamp(g), clamp(b));
    }

    static const Pixel BLACK;
    static const Pixel WHITE;
};

const Pixel Pixel::BLACK(0, 0, 0);
const Pixel Pixel::WHITE(Pixel::MAX_QUANTUM, Pixel::MAX_QUANTUM, Pixel::MAX_QUANTUM);

Initially I tried to initialize the static BLACK and WHITE inside ofthe struct however I got an error from g++ about having an incomplete type. Once I declared the static variables outside of the struct the error went away and the code now compiles. This is a header only library and what I am wondering is will declaring the static variables like this cause compilation issues if it is included in multiple cpp files?

GreatAndPowerfulOz

will declaring the static variables like this cause compilation issues if it is included in multiple cpp files?

No, because the C++ standard allows this particular usage and unless they are declared extern they will not be seen or collide with other instances of the same named variable in other translation units (i.e. cpp files).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why we cannot create the Instance of the Static Class in .Net

From Dev

C++ Template class with a static const member instance of itself

From Dev

class instance variables proper usage

From Dev

Create an instance of a class in the class itself

From Dev

Cannot create an instance of the static class 'System.Tuple'

From Dev

Is there a way to detect scroll event of UITableView inside the tableView class itself?

From Dev

What is the proper way to create new instance of generic class in kotlin?

From Dev

TypeScript create new instance from class in a static context

From Dev

In Managed C++, what is the proper way to define a static singleton instance in a class?

From Dev

Static methods inside class instance - good, bad or depends?

From Dev

PHP, error set static variable for class instance inside class

From Dev

Proper way to create a set method of a Class

From Dev

How to use PowerMockito to mock new class instance inside a static method

From Dev

C# Create Global instance of a Static Class

From Dev

Is there a way to reference the instance in decorators inside a class?

From Dev

Modern way to create static or Class variable for Javascript class

From Dev

Proper way to create class variable in Data Class

From Dev

Static values set to instance work when set from static method but not from Static Method inside a class?

From Dev

Java: How to Create an instance of a subclass from with a Static method of the super class

From Dev

What is the proper way to create an object of another class in PHP?

From Dev

Cannot create an instance of the static class 'System.Tuple'

From Dev

Is there a way to detect scroll event of UITableView inside the tableView class itself?

From Dev

Instance of class inside of the class itself

From Dev

How to create an instance of a class inside a method within the class, python

From Dev

Proper way to use UIImageView inside a custom View class

From Dev

how to get list of inner class inside a class and create instance

From Dev

What's the proper way to create a static sentinel node

From Dev

Mockito is unable to create instance using @InjectMocks because of Static class in constructor

From Dev

Groovy: Proper way to create/use this class

Related Related

  1. 1

    Why we cannot create the Instance of the Static Class in .Net

  2. 2

    C++ Template class with a static const member instance of itself

  3. 3

    class instance variables proper usage

  4. 4

    Create an instance of a class in the class itself

  5. 5

    Cannot create an instance of the static class 'System.Tuple'

  6. 6

    Is there a way to detect scroll event of UITableView inside the tableView class itself?

  7. 7

    What is the proper way to create new instance of generic class in kotlin?

  8. 8

    TypeScript create new instance from class in a static context

  9. 9

    In Managed C++, what is the proper way to define a static singleton instance in a class?

  10. 10

    Static methods inside class instance - good, bad or depends?

  11. 11

    PHP, error set static variable for class instance inside class

  12. 12

    Proper way to create a set method of a Class

  13. 13

    How to use PowerMockito to mock new class instance inside a static method

  14. 14

    C# Create Global instance of a Static Class

  15. 15

    Is there a way to reference the instance in decorators inside a class?

  16. 16

    Modern way to create static or Class variable for Javascript class

  17. 17

    Proper way to create class variable in Data Class

  18. 18

    Static values set to instance work when set from static method but not from Static Method inside a class?

  19. 19

    Java: How to Create an instance of a subclass from with a Static method of the super class

  20. 20

    What is the proper way to create an object of another class in PHP?

  21. 21

    Cannot create an instance of the static class 'System.Tuple'

  22. 22

    Is there a way to detect scroll event of UITableView inside the tableView class itself?

  23. 23

    Instance of class inside of the class itself

  24. 24

    How to create an instance of a class inside a method within the class, python

  25. 25

    Proper way to use UIImageView inside a custom View class

  26. 26

    how to get list of inner class inside a class and create instance

  27. 27

    What's the proper way to create a static sentinel node

  28. 28

    Mockito is unable to create instance using @InjectMocks because of Static class in constructor

  29. 29

    Groovy: Proper way to create/use this class

HotTag

Archive