Reputation: 1187
maybe it sounds stupid but I am not sure what is the best way to handle this:
I have a class that provides access to a certain Com Port for example. To use this you have to set up a buffer with information. this buffer is realized as a typedef. right now this typedef is located in the header file of the class but not in the class as a member. the difference is the way you can access this buffer (header file included). As a class member:
CMyClass::MyBuffer MyBuf;
or just as a global located in the header:
MyBuffer MyBuf;
I have a couple of data I am not sure where to put, like some static vars that act as a unique identifer in the buffer. So we are talking about statics and typedefs here.
Technically, they belong to the class because without those you can not work with it. But accessing theses type with the class name and its scope operator is much longer.
Does it matter how to solve this?
Upvotes: 2
Views: 110
Reputation: 3471
I'm guessing that the actual name you are using is somewhat more descriptive that MyBuffer
. Does the buffer in any way belong to the class, other than being used by it? Does it somehow depend on being in that specific class, and would a typedef of the same description, put in a different class, behave any differently? If another class needed something similar, would the same typedef do?
In my opinion, if you answered "no" to the above questions, the typedef does not belong in the class at all; it seems like it is only used by that class. It makes more sense to keep it global, as you may have another class using that typedef, but would end up keeping the implication that MyBuffer
is somehow specific to CMyClass
.
Upvotes: 1
Reputation: 13382
One possible solution would be to put the class CMyClass
and the MyBuffer
into a relevant namespace and use the using
directive. Example:
namespace MyNS {
class CMyClass
{
// snip
};
typedef blah MyBuffer;
}
some_function()
{
MyNS::CMyClass a;
MyNS::MyBuffer b;
}
using namespace MyNS;
some_other_function()
{
CMyClass a;
MyBuffer b;
}
This could be a possible solution.
Upvotes: 0
Reputation: 52519
You shouldn't avoid putting something in a class which belongs to a class just because you find it inconvinient to write the class scope each time you need to access it.
Upvotes: 1