S
Steve
Hi,
I thought I had invented a pretty neat solution for my problem, but it turns
out I'm using the named constructor idiom.
I've seen examples where empty structs are used to identify the different
constructors by their first parameter:
struct Type1 {};
struct Type2 {};
class Foo
{
public:
Foo( Type1, int x );
Foo( Type2, int y );
};
Foo a = Foo( Type1(), 10 );
Foo b = Foo( Type2(), 10 );
My solution was to use enums to differentiate them:
enum Type1 { eType1 };
enum Type2 { eType2 };
class Foo
{
public:
Foo( Type1, int x );
Foo( Type2, int y );
};
Foo a = Foo( eType1, 10 );
Foo b = Foo( eType2, 10 );
Are there any advantages or disadvantages to either method?
I guess the enums take up storage space, but so do the empty structs right?
Enums need 2 identifiers - the type name and the value, whereas the struct
only needs one identifier?
Enums don't need a () to construct them.
I'm not convinced either way (yet!). Indeed, is another method?
Does anyone have any views?
I thought I had invented a pretty neat solution for my problem, but it turns
out I'm using the named constructor idiom.
I've seen examples where empty structs are used to identify the different
constructors by their first parameter:
struct Type1 {};
struct Type2 {};
class Foo
{
public:
Foo( Type1, int x );
Foo( Type2, int y );
};
Foo a = Foo( Type1(), 10 );
Foo b = Foo( Type2(), 10 );
My solution was to use enums to differentiate them:
enum Type1 { eType1 };
enum Type2 { eType2 };
class Foo
{
public:
Foo( Type1, int x );
Foo( Type2, int y );
};
Foo a = Foo( eType1, 10 );
Foo b = Foo( eType2, 10 );
Are there any advantages or disadvantages to either method?
I guess the enums take up storage space, but so do the empty structs right?
Enums need 2 identifiers - the type name and the value, whereas the struct
only needs one identifier?
Enums don't need a () to construct them.
I'm not convinced either way (yet!). Indeed, is another method?
Does anyone have any views?