开发者

C++ instantiation question

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 ch开发者_开发技巧aracter_encoding is set to "cp_1252"; So, is all that logic valid?

Jim


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...)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜