0

As posted below. How would i initialize the alphabet using alphabet's own member function for my static member variable? I need to do the initialization from within the "Text.cpp" implementation file.

Text.h

class Text {
private:

    struct Font {
        enum Enum {
            Arial,
            Menlo,
            Times
        };
    };

    static Alphabet alphabet[3]; // library of letters

}; // class

I need something as seen below, just the correct way of accomplish the task. I also need to initialize the alphabet only once for the duration of my runtime, thus i have made alphabet static. Thank you. ^^

Text.cpp

Alphabet Text::alphabet[Text::Font::Arial].Load("./Alphabet/", "Arial", ".xml"));
Alphabet Text::alphabet[Text::Font::Menlo].Load("./Alphabet/", "Menlo", ".xml"));
Alphabet Text::alphabet[Text::Font::Times].Load("./Alphabet/", "Times", ".xml"));
2

2 Answers 2

3

Assuming that Alphabet has a parametrized constructor, you can do it this way in a single translation unit (in Text.cpp file),

Alphabet Text::alphabet[] = { ("./Alphabet/", "Arial", ".xml"), 
                              ("./Alphabet/", "Menlo", ".xml"),
                              ("./Alphabet/", "Times", ".xml") };
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly - and to avoid duplication, the constructor can call Load internally. (there are some extra )s in your answer - might want to edit them out). +1
@Arun Thank you. I will overload my alphabet constructor to make this work. ^^
1

Option 1: All the static variables declared in the class should be redefined in .cpp (implementation file, in your case Text.cpp), you shall initialize the variables in the definition.

Option 2: Add a new static method(function) in class "Text" to initialize the static members. Since your static data member in private section. Declare the method in public section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.