没有默认构造函数 - 糟糕的形式? [英] no default constructor -- bad form?

查看:70
本文介绍了没有默认构造函数 - 糟糕的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写一个程序,需要知道它的一个对象成员

才能初始化它。我的

问题(这是一个C ++问题,而不是一个Windows问题)并不重要,但是这个类

用于Windows程序并且有一个HWND成员(一个

窗口的句柄),并且理解该类必须知道该成员是什么。


但是这会让我进入没有默认构造函数的位置,和

我听说这被认为是不好的形式。

处理这种情况的正确方法是什么?或者是否可以拥有一个没有

默认构造函数的类?


我在档案中找到了另一个关于这个主题的帖子,但我没有' '$

了解所提出的解决方案,其中涉及使用工厂

设计。我不太明白这是如何解决这个问题的,如果有相关的话,我会非常喜欢这个解释。 (代码如下)


感谢您的帮助,

cpp


------ BEGIN SNIPPET -----

我遇到过许多实例,当没有默认构造函数时,它似乎是最正确封装的类。
例如,我最终会得到类似的结果:

A级{
私人:
const B& x_;
公开:
A(const B& x):x_(x){}
//其他东西
};

在此例如,我必须在构造类时将x_初始化为有意义的东西。更重要的是,我希望构造函数
将A保持在有效状态,其中A仅在x_有意义地初始化时才有效(对于只有调用者可以确定的内容)。


工厂怎么样?


A级{

私人:

A():x_(sX ){};

静态B& sX;

public:

static A * makeArrayOfA(const B& x,const long numA){sX = x;返回

new

A [numA];};

....

};


--------- END SNIPPET ----------


解决方案

On Sun,2004年5月2日03:40:35 GMT,cppaddict< he *** @ hello.com>写道:



我正在编写一个程序,需要知道它的一个对象成员
才能初始化。这对我的问题(这是一个C ++问题,而不是一个Windows问题)并不重要,但该类
用于Windows程序并且有一个HWND成员(一个句柄来一个
window),并且要理解该类必须知道该成员是什么。

但这使我处于没有默认构造函数的位置,并且
我已经听说被认为是不好的形式。处理这种情况的正确方法是什么?或者是否可以使用没有
默认构造函数的类?




有人说它是糟糕的形式没有默认的构造函数?我不会b
得到它。也许在某些特定的情况下,但一般来说,

类对于默认初始化它们毫无意义是没有默认构造函数的b / b
更好...因为你之所以有这样的原因,你会怎么做?如果你没有价值,你如何初始化/需要/价值的东西?


在我教授的课程中,通常有一类代表一种或另一种类型的银行账户,数据成员代表账户持有人的姓名,

帐号等等。允许这样的

类默认初始化是没有意义的。


但是,如果你需要创建这样的对象的内置数组,或者你使用的是STL工具,那么你遇到麻烦的一些情况就是这样。 br />
要求所涉及元素的默认初始化(例如,如果

你使用std :: vector,并使用resize ()增加向量中元素的数量

)。否则,我不会感到内疚只是因为你的一类

没有默认的构造函数。

-leor


-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt .html




" cppaddict" <他*** @ hello.com>在消息中写道

news:d0 ******************************** @ 4ax.com ...



我正在编写一个程序,需要知道它的一个对象成员才能初始化。这对我的问题(这是一个C ++问题,而不是一个Windows问题)并不重要,但该类
用于Windows程序并且有一个HWND成员(一个句柄来一个
window),并且要理解该类必须知道该成员是什么。

但这使我处于没有默认构造函数的位置,并且
我已经听说被认为是不好的形式。处理这种情况的正确方法是什么?或者是否可以使用没有
默认构造函数的类?


如果您没有默认构造函数,那么使用您的类的某些代码

不会编译,例如数组声明。所以如果可以的话,你应该添加一个默认的

构造函数。但是,如果它真的是无意义的默认

构造函数然后不这样做。

我在档案中找到了另一个关于这个主题的帖子,但是我并没有理解所提出的解决方案,其中涉及使用工厂设计。我不太明白这是如何解决这个问题的,如果有相关的话,我会很乐意解释这个问题。 (代码如下)

感谢您的帮助,
cpp

------ BEGIN SNIPPET -----

我遇到过许多实例,当没有默认构造函数时,它似乎是一个类
最正确的封装。
例如,我可能最终得到类似的东西:

A级{
私人:
const B& x_;
公开:
A(const B& x):x_(x){}
//其他东西
};

在此例如,我必须在构造类时将x_初始化为有意义的东西。更重要的是,我希望构造函数
将A保持在有效状态,其中A仅在x_有意义地初始化时才有效(对于只有调用者可以确定的内容)。

工厂怎么样?

A级{
私人:
A():x_(sX){};
静态B& sX;
public:
static A * makeArrayOfA(const B& x,const long numA){sX = x;返回

A [numA];};
...
};




答案描述了在没有公共

默认构造函数的情况下创建数组的方法,这不是你的问题所在。


john


" cppaddict" <他*** @ hello.com>在消息中写道

news:d0 ******************************** @ 4ax.com ...



我正在编写一个程序,需要知道它的一个对象成员才能初始化。这对我的问题(这是一个C ++问题,而不是一个Windows问题)并不重要,但该类
用于Windows程序并且有一个HWND成员(一个句柄来一个
window),并且要理解该类必须知道该成员是什么。

但这使我处于没有默认构造函数的位置,并且
我已经听说被认为是不好的形式。处理这种情况的正确方法是什么?或者是否可以拥有一个没有
默认构造函数的类?

我在档案中找到了另一个关于这个主题的帖子,但我并没有理解提出的解决方案,涉及使用工厂设计。我不太明白这是如何解决这个问题的,如果有相关的话,我会很乐意解释这个问题。 (代码如下)

感谢您的帮助,
cpp




[剪断片段]


我不会将我的设计的任何方面都放在听说这样的情况是坏的形式上。如果你不想要或不需要一个默认的构造函数,不要把一个

放进去。


然而,有一个优点是默认构造函数。对于

实例,它允许您将类的成员放入容器中。这里

是几个解决方法。


1)对象通常有状态,你可以定义一个未初始化的

状态"这将是你的默认构造函数留下它的地方。然后可以调用

方法来完成对象的初始化

你准备好了。


然而,这个方法有其风险。您将不得不考虑如何避免使用单位化对象的错误以及如果您这样做将会发生什么。如果这比它的价值更麻烦,省略

默认构造函数可能更好。


2)引用计数智能指针提供复制语义,允许你

将它们放入标准容器中,无论它们指向什么。所以:


#include< boost / shared_ptr.hpp>

#include< vector>


typedef PFred boost :: shared_ptr< Fred> ;;

std :: vector< PFred> a_vec;


a_vec.push_back(PFred(new Fred(some_argument));

a_vec.back() - > fred_function();


<未经测试(==错误)代码>


如果类Fred没有默认构造函数,即使

" std :: vector< Fred> b_vec;"无效。


-

Cy
http://home.rochester.rr.com/cyhome/


Hi,

I am writing a program and needs to know one of its object members
before it can be initialized. It doesn''t really matter for my
question (which a C++ question, not a windows question), but the class
is used in a windows program and has an HWND member ( a handle to a
window), and to make an sense the class must know what the member is.

But this puts me in the position of having no default constructor, and
I''ve heard that is considered bad form. What is the right way to
handle this situation? Or is it okay to have a class which has no
default constructor?

I found one other thread on this subject in the archives, but I didn''t
understand the solution proposed, which involved using the Factory
Design. I don''t quite see how this solves the problem, and would love
an explanation of that, if it''s relevant. (code is below)

Thanks for any help,
cpp

------BEGIN SNIPPET-----

I have come across many instances where it seems like a class
is most correctly encapsulated when there is NO default constructor.
E.g., I might end up with something like:

class A {
private:
const B& x_;
public:
A(const B& x) : x_(x) {}
// other stuff
};

In this example, I have to initialize x_ to something meaningful when
the class is constructed. More importantly, I want the constructor
to leave A in a valid state, where A is valid only if x_ is meaningfully
initialized (to something only the caller can determine).


How about a factory?

class A {
private:
A() : x_(sX) {};
static B& sX;
public:
static A* makeArrayOfA(const B&x, const long numA) { sX = x; return
new
A[numA];};
....
};

---------END SNIPPET----------


解决方案

On Sun, 02 May 2004 03:40:35 GMT, cppaddict <he***@hello.com> wrote:

Hi,

I am writing a program and needs to know one of its object members
before it can be initialized. It doesn''t really matter for my
question (which a C++ question, not a windows question), but the class
is used in a windows program and has an HWND member ( a handle to a
window), and to make an sense the class must know what the member is.

But this puts me in the position of having no default constructor, and
I''ve heard that is considered bad form. What is the right way to
handle this situation? Or is it okay to have a class which has no
default constructor?



Someone says it is "bad form" not to have a default constructor? I don''t
quite get it. Perhaps in some specific situation, but generally speaking,
classes where it makes no sense for them to be default-initialized are
better off without a default constructor...because of the very reason
you''ve hit upon: how do you initialize things that /need/ a value if you
don''t have a value?

In courses I teach, there are often classes that represent bank accounts of
one type or another, with data members representing account holder''s names,
the account number, etc. It wouldn''t make a whole lot of sense to allow a
class such as that to be default-initialized.

Some situations where you''ll run into trouble, however, are if you need to
create built-in arrays of such objects, or if you use an STL facility that
requires default initialization for the elements involved (for example, if
you use a std::vector, and use resize() to increase the number of elements
in the vector). Otherwise, I wouldn''t feel guilty just because a class of
yours doesn''t have a default constructor.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html



"cppaddict" <he***@hello.com> wrote in message
news:d0********************************@4ax.com...

Hi,

I am writing a program and needs to know one of its object members
before it can be initialized. It doesn''t really matter for my
question (which a C++ question, not a windows question), but the class
is used in a windows program and has an HWND member ( a handle to a
window), and to make an sense the class must know what the member is.

But this puts me in the position of having no default constructor, and
I''ve heard that is considered bad form. What is the right way to
handle this situation? Or is it okay to have a class which has no
default constructor?

If you don''t have a default constructor then certain code using your class
won''t compile, e.g. array declarations. So you should add a default
constructor if it can be done. But if it''s really is nonsense to default
constructor then don''t do so.
I found one other thread on this subject in the archives, but I didn''t
understand the solution proposed, which involved using the Factory
Design. I don''t quite see how this solves the problem, and would love
an explanation of that, if it''s relevant. (code is below)

Thanks for any help,
cpp

------BEGIN SNIPPET-----

I have come across many instances where it seems like a class
is most correctly encapsulated when there is NO default constructor.
E.g., I might end up with something like:

class A {
private:
const B& x_;
public:
A(const B& x) : x_(x) {}
// other stuff
};

In this example, I have to initialize x_ to something meaningful when
the class is constructed. More importantly, I want the constructor
to leave A in a valid state, where A is valid only if x_ is meaningfully
initialized (to something only the caller can determine).


How about a factory?

class A {
private:
A() : x_(sX) {};
static B& sX;
public:
static A* makeArrayOfA(const B&x, const long numA) { sX = x; return
new
A[numA];};
...
};



The answer describes a way to create an array in the absence of a public
default constructor, which is not what your question was about.

john


"cppaddict" <he***@hello.com> wrote in message
news:d0********************************@4ax.com...

Hi,

I am writing a program and needs to know one of its object members
before it can be initialized. It doesn''t really matter for my
question (which a C++ question, not a windows question), but the class
is used in a windows program and has an HWND member ( a handle to a
window), and to make an sense the class must know what the member is.

But this puts me in the position of having no default constructor, and
I''ve heard that is considered bad form. What is the right way to
handle this situation? Or is it okay to have a class which has no
default constructor?

I found one other thread on this subject in the archives, but I didn''t
understand the solution proposed, which involved using the Factory
Design. I don''t quite see how this solves the problem, and would love
an explanation of that, if it''s relevant. (code is below)

Thanks for any help,
cpp



[snip the snippet]

I wouldn''t base any aspect of my design on "hearing that such-and-such is
bad form". If you don''t want or need a default constructor, don''t put one
in.

However, there is some advantage to having a default constructor. For
instance it allows you to put members of your class into a container. Here
are a couple of workarounds.

1) Objects generally have states, and you can define an "uninitialized
state" which would be where your default constructor would leave it. A
method could then be called to complete your object''s initialization when
you are ready.

However, this approach has its risks. You would have to consider how to
avoid the mistake of using an unitialized object and also what is going to
happen if you do. If this is more trouble than its worth, leaving out the
default constructor is probably better.

2) Reference counted smart pointers provide copy semantics which allow you
to put them in standard containers no matter what they point to. So:

#include <boost/shared_ptr.hpp>
#include <vector>

typedef PFred boost::shared_ptr<Fred>;
std::vector<PFred> a_vec;

a_vec.push_back(PFred(new Fred(some_argument));
a_vec.back()->fred_function();

<untested (==wrong) code>

If class "Fred" has no default constructor this will work even though
"std::vector<Fred> b_vec;" would not work.

--
Cy
http://home.rochester.rr.com/cyhome/


这篇关于没有默认构造函数 - 糟糕的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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