Re: Hungarian notation vs. namespace
On Thursday, September 13, 2012 11:41:00 AM UTC+2, F.Zwarts wrote:
So, if in your example the reserved word "struct" would be replaced with
"namespace", why would there suddenly be a less tighter scope, worse
encapsulation, etc.?
With a struct, you are forced to define all your data in one
location. With a namespace you have the freedom to define it
anywhere (namespaces can be reopened). IMO for enumerated types
that relate to one another, one location is more beneficial.
Furthermore, classes scale better when using enumerated types in
templates:
struct MyGenericEnum
{
enum Value
{
Value0, ValueN
};
enum{ min = Value0, max = ValueN, size = ValueN+1
};
template <class EnumT>
void foo( typename EnumT::Value v )
{
for( int i = EnumT::min; i < EnumT::count; ++i )
{
//Get the picture???
}
}
Classes can be forward declared. By writing a little
general class, enumerations can be forward declared:
template <class T>
struct EnumForwarder
{
EnumForwarder( typename T::Value value = T::Value() )
: value_( value )
{
}
operator typename T::Value() const{ return value_; }
private:
typename T::Value value_;
};
class MyEnum;
void set( EnumForwarder<MyEnum> myEnum );
For reasons mentioned above, I prefer using the concept class
(the reserved word "struct" and "class") for this purpose.
Kind regards,
Werner