命名构造函数成语 - 一个问题 [英] Named Constructor idiom - a question

查看:50
本文介绍了命名构造函数成语 - 一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我以为我为我的问题发明了一个非常简洁的解决方案,但它转了

我正在使用命名的构造函数成语。 :-)


我见过一些例子,其中空结构用于通过第一个参数识别不同的

构造函数:


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


我的解决方案是使用枚举来区分它们:


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

两种方法都有任何优点或缺点吗?

我猜这些枚举会占用存储空间,但是空结构吧?


枚举需要2个识别ers - 类型名称和值,而struct

只需要一个标识符?


枚举不需要a()来构造它们。

我不相信(不过!)。确实,是另一种方法吗?


有没有人有意见?

-

问候,

Steve


如果Hokey Cokey真的是它的全部内容怎么办?

解决方案

史蒂夫写道:

我以为我为我的问题发明了一个非常简洁的解决方案,但它转过来了我正在使用命名的构造函数成语。 :-)

我见过一些例子,其中空结构用于通过第一个参数识别不同的构造函数:

struct Type1 {};
struct Type2 {};

类Foo
{
公开:
Foo(Type1,int x);
Foo(Type2,int y);
};

Foo a = Foo(Type1(),10);
Foo b = Foo(Type2(),10);


为什么不简单


Foo a(Type1(),10);

Foo b(Type2( ),10);


???

我的解决方案是使用枚举来区分它们:

枚举Type1 {eType1 };
枚举Type2 {eType2};

类Foo
{
公开:
Foo(Type1,int x);
Foo (Type2,int y);
};

Foo a = Foo(eType1,10);
Foo b = Foo(eType2,10);


再次,你不需要''=''...

两种方法都有任何优点或缺点吗?


没有区别,AFAICS。

我猜这些枚举会占用存储空间,但空结构也是如此吗?


正确。

枚举需要2个标识符 - 类型名称和值,而结构
只需要一个标识符?


不,他们不是。您可以使用类似于结构的枚举:


Foo a(Type1(),10);


因为枚举Type1是单独的类型,你没有在你的构造函数中使用它的实际值

,Type1()只是构造一个临时的

枚举Type1类型,并给出它的值为0.

枚举不需要()来构造它们。


除非你使用枚举(类型)名称。

我不相信(但是!)。确实,是另一种方法吗?

有没有人有任何意见?




您可以制作模板构造函数:

class Type1 {};

class Type2 {};


class Foo {

public:

模板< class T> Foo(T,int){

//实现默认行为

}

};


模板<> Foo :: Foo< Type1>(Type1,int i){

//实现第一个

}


模板< ;> Foo :: Foo< Type2>(Type2,int i){

//实现另一个

}


int main(){

Foo a(Type1(),10);

Foo b(Type2(),10);

Foo ab (int(),10);

}


我不确定它的优势是什么......我唯一能做的就是

目前认为,如果你从第三种类型构建,

你将最终使用默认&专业化,来自班级

定义...不多,我猜:-)


V


22/7/05 23:03,文章

P2******************@newsread1.mlpsca01.us.to .veri o.net,Victor Bazarov

< v。******** @ comAcast.net>写道:

Foo a = Foo(Type1(),10);
Foo b = Foo(Type2(),10);
为什么不简单

Foo a(Type1(),10);
Foo b(Type2(),10);

???




嗯,是的,错误......很好看...... ;-)

枚举需要2个标识符 - 类型名称和值,而结构只需要一个标识符?



不,他们不是。你可以像使用结构一样使用枚举:

Foo a(Type1(),10);

因为枚举Type1是一个单独的类型,你不是在构造函数中使用它的实际值
,Type1()只构造一个enum Type1类型的临时值,并给它赋值0.

枚举不需要a()来构造它们。



除非你使用枚举(类型)名称。




[灯光开关......]啊,是的,当然 - 就像int()等。

你可以制作模板构造函数:

[剪掉]示例代码]
我不确定它的优势是什么......我现在唯一可以想到的是,如果你从第三种类型构建,
你会结束使用默认专业化,来自班级
定义......不多,我猜: - )




我会说那不太安全,因为我想确定只有我指定的那些

构造函数才可以使用。

非常感谢你的投入,Victor。至少我知道现在我不会去b / B
非常偏离轨道!


-

问候,

史蒂夫


如果Hokey Cokey真的是它的全部内容怎么办?

< br>

>


我想枚举会占用存储空间,但空结构也是如此
对吧?




实际上我认为任何一种解决方案都不会占用任何空间。

数据类型(结构,枚举)的定义不占用任何空间,因为这个

信息仅在编译时使用。


你不会分配任何东西,除了空结构的临时实例,

占用与普通指针一样多的空间= 32位上的4个字节

系统。


使用你的枚举解决方案只会转换枚举到整数常量

,它在32位系统上也占用4个字节 - 再次,类型信息

仅在编译时用于链接到正确的函数。


无论哪种方式,你得到你想要的,一切都在编译时解决

因此它不占用任何运行时资源:o)


最好的,

Mogens


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?
--
Regards,
Steve

"What if the Hokey Cokey really IS what it''s all about?"

解决方案

Steve wrote:

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 );
Why not simply

Foo a(Type1(), 10);
Foo b(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 );
Again, you don''t need the ''=''...
Are there any advantages or disadvantages to either method?
No difference, AFAICS.
I guess the enums take up storage space, but so do the empty structs right?
Right.
Enums need 2 identifiers - the type name and the value, whereas the struct
only needs one identifier?
No, they don''t. You can use the enums like you did structs:

Foo a(Type1(), 10);

Since enum Type1 is a separate type, and you''re not using the actual value
of it in your constructor, Type1() is just constructing an temporary of
the enum Type1 type, and gives it value 0.
Enums don''t need a () to construct them.
Unless you use the enumeration (type) name.
I''m not convinced either way (yet!). Indeed, is another method?

Does anyone have any views?



You can make a template constructors:

class Type1 {};
class Type2 {};

class Foo {
public:
template<class T> Foo(T,int) {
// implement the default behaviour
}
};

template<> Foo::Foo<Type1>(Type1, int i) {
// implement the first one
}

template<> Foo::Foo<Type2>(Type2, int i) {
// implement the other one
}

int main() {
Foo a(Type1(), 10);
Foo b(Type2(), 10);
Foo ab(int(), 10);
}

I am not sure what the advantage might be... The only thing I can
think of at the moment is that if you construct from some third type,
you will end up using the "default" specialisation, from the class
definition... Not much, I guess :-)

V


On 22/7/05 23:03, in article
P2******************@newsread1.mlpsca01.us.to.veri o.net, "Victor Bazarov"
<v.********@comAcast.net> wrote:

Foo a = Foo( Type1(), 10 );
Foo b = Foo( Type2(), 10 );
Why not simply

Foo a(Type1(), 10);
Foo b(Type2(), 10);

???



Ummm, yes, errr... well spotted... ;-)

Enums need 2 identifiers - the type name and the value, whereas the struct
only needs one identifier?



No, they don''t. You can use the enums like you did structs:

Foo a(Type1(), 10);

Since enum Type1 is a separate type, and you''re not using the actual value
of it in your constructor, Type1() is just constructing an temporary of
the enum Type1 type, and gives it value 0.

Enums don''t need a () to construct them.



Unless you use the enumeration (type) name.



[sound of light switch...] Ah, yes, of course - just like int(), etc.
You can make a template constructors:
[snipped example code]
I am not sure what the advantage might be... The only thing I can
think of at the moment is that if you construct from some third type,
you will end up using the "default" specialisation, from the class
definition... Not much, I guess :-)



I would say that1s less safe, since I want to make sure only those
constructors I specify can be used.
Thanks very much your input, Victor. At least I know now I''m not going
wildly off track!

--
Regards,
Steve

"What if the Hokey Cokey really IS what it''s all about?"


>


I guess the enums take up storage space, but so do the empty structs
right?



Actually I don''t think either solution will take up any space at all.
Definitions of data types (structs, enums) don''t take up any space, as this
information is only used at compile time.

You don''t allocate anything, except temporary instances of empty structs,
which take up as much space as an ordinary pointer = 4 bytes on 32 bit
systems.

Using your enum solution will just convert the enums to integer constants
which also take up 4 bytes on a 32 bit system - again, the type information
is only used at compile time to link to the right function.

Either way you get what you want, and everything is resolved at compile time
so it doesn''t take up any runtime resources :o)

The best,
Mogens


这篇关于命名构造函数成语 - 一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆