3

I would like to have a static char array member initialized in terms of other static char array members - but the initialization is such that code is necessary. Is this possible?

class fred {
    static char *a;
    static char *b;
    static char c[4];
}

Now a and b will have fixed values, but I want to construct c in terms of them. EG:

fred::a = "1234"
fred::b = "ab"    
strcpy(c, b);
strncat(c, a, 1);

However I can't see anyway to initialize c, other than to make a class for the purpose which is just a char[4], with a constructor that references fred::a and fred::b, and then replace c in fred with an instance of that class - which is awkward when referencing the c char array.

Is there a better way?

9
  • Is there some really good reason why you can't use std::string here? Commented Jun 19, 2012 at 1:38
  • I don't use string, because it will be accessed a lot, and I'm assuming (perhaps erroneously) that a naked char array with have better performance. Commented Jun 19, 2012 at 1:45
  • Just stating that should make it clear that's not how you should be doing things. Commented Jun 19, 2012 at 1:58
  • 2
    @user1425406 Never ever base your performance-related decisions on assumptions, no matter how obvious you think they are. In general sense the compiler is better at generating high-performance code than you are, so just follow "good"/common practices whenever possible and only consider alternatives whenever you have proven*(by a *benchmark) that performance is an issue. Premature optimization is the .... You know the deal. :) Commented Jun 19, 2012 at 2:08
  • @DavidSchwartz - can you please explain? What part of what I'm doing do you think I'm not doing correctly? Using a char array rather than a string? Commented Jun 19, 2012 at 2:09

3 Answers 3

4

Edit: Originally, I had wilma as a friend of fred, and a static instance of wilma doing the initialization. I have changed the example to have dino declared within fred since the OP said he thought that would be cleaner.

You can create a static instance of a class inside fred whose job is to initialize c for you.

class fred {
    static char *a;
    static char *b;
    static char c[4];
    static struct dino { dino (); } dino_flintstone;
};

char *fred::a;
char *fred::b;
char fred::c[4];
fred::dino fred::dino_flintstone;

fred::dino::dino () {
    fred::a = "1234";
    fred::b = "ab";
    strcpy(fred::c, fred::b);
    strncat(fred::c, fred::a, 1);
}
Sign up to request clarification or add additional context in comments.

8 Comments

I tried this, and it looks like it works, thanks! I don't quite understand what you mean by calling the class something else if I want to define wilma inside fred (which would indeed be a bit neater).
@user1425406: Terrific, yabba dabba doo! +1 on your question from me.
@user1425406 He means it just as a joke. There's no real reason or need to rename it if it's a class member. :)
@user1425406: I modified the example to have dino inside of fred. Regards
Hmm, Fred inside Wilma, Dino inside Fred. Double entendre if I ever saw one.
|
1

Consequtive string literals are concatenated at compile-time...

#define A "1234"
#define B "ab"
fred::a = A;
fred::b = B;    
fred::c = A B;

1 Comment

Doesn't quite work because c becomes { 'a', 'b', '1', '2' } instead of "ab1".
1

You can't in a clean way, but you can use a trick to simulate an static constructor as shown here.

In that static constructor, you can initialize c. This would be a possible implementation:

class fred {
    static char *a = "1234";
    static char *b = "ab";
    static char c[4];
    public:
    fred(){
       strcpy(fred::c, fred::b);
       strncat(fred::c, fred::a, 1);
    }    
}

class Fred_staticInitializer{  
    static fred staticInitializer;  //This does the trick (launches constructor)
}

Hope it helps. Cheers

3 Comments

This is a good trick if fred is never instantiated by anyone else. It should probably have at least a check to see if it is already initialized.
I think the asker can do that with this guideline. But you're totally right
I don't quite understand, won't the fred constructor be called everytime I create a fred instance? This would not be desirable..

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.