tags:

views:

71

answers:

2

Want to verify that my understanding of how this works.
Have a C++ Class with one public instance variable:

char* character_encoding;  

and whose only constructor is defined as:

TF_StringList(const char* encoding = "cp_1252");  

when I use this class in either C++/CLI or C++, the first thing I do is declare a pointer to an object of this class:

const TF_StringList * categories;  

Then later I instantiate it:

categories = new TF_StringList();  

this gives me a pointer to an object of type TF_StringList whose variable character_encoding is set to "cp_1252"; So, is all that logic valid?

Jim

+1  A: 

I would say that depends on what the constructor actually does. If we assume it does this:

TF_StringList(const char* encoding)
: character_encoding(encoding)
{
}

Then your logic holds. But it could do whatever, you don't show a connection between the constructor argument and the instance's member variable.

unwind
+1  A: 

The one problem i see, is that your constructor takes a const char*, but you're storing it in a char*. That's going to cause the compiler to complain unless you cast away the constness. (Alternatively, is there any reason not to make your field a const char* ? i don't see you needing to edit the chars of the name...)

cHao
Clarification: I did not write the TF_StringList class, it is a 3rd party class for which I only have the header. So (1) I have no idea what is going on in the constructor and (2) can't change the modifiers on character_encoding. How does that effect your answers?
Jim Jones
Well, we can't guarantee that what you passed the constructor is what you'll get from reading the public field -- only the maker of the class can guarantee that, and they probably won't. (The name could be converted to uppercase, or looked up for a "nearest match", etc.)
cHao
OK, not planning on verifying the value of the public field, merely want to know that the TF_StringList object is properly instantiated so that I can start using it's insert(const char* ) method to load it with values. Thanks
Jim Jones