默认构造与具有初始值的构造 [英] Default construction versus construction with initial values

查看:54
本文介绍了默认构造与具有初始值的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解究竟什么是默认构造。是。我知道如何为构造函数提供初始值,所以如果我在

示例中,在我的代码中执行此操作:


MyClass zoot(1,2);


它将实例化MyClass,将1和2传递给我的构造函数,在

转向我会做任何我做的事情想要它。默认构造函数是否为
如下:


MyClass zoot;


没有传递参数?在我的班级宣言中,我会这样做:


班级Zoot

{

Zoot(); //默认构造函数?

Zoot(int a,int b); //具有初始值的构造函数。

....

}


Zoot :: Zoot()

{

//默认构造函数,没有参数传递,做你想做的事情吗?

}

Zoot :: Zoot(int a,int b)

{

//初始值的构造函数

}


Am我走在正确的轨道上? IOW,一个默认的构造函数只是一个构造函数

,它不接收任何参数,但你仍然可以编写代码来做你想要的b $ b想要的东西吗?

解决方案



Ook写道:

我无法理解究竟什么是默认构造。是。
来自FAQ:


A" default constructor"是一个可以在没有

参数的情况下调用的构造函数。

所以为了保持你的例子,这可能是一个默认的构造函数:


class Zoot()

{

public:

Zoot(); //默认构造函数

}


但这也可以是默认构造函数:


类Zoot( )

{

public:

Zoot(int a = 0,int b = 5); //默认构造函数

}


因为它有a和b的默认值,你仍然可以创建一个新的

对象喜欢这样:


Zoot myObj;


但是,这个......


类Zoot ()

{

public:

Zoot(int a,int b); //不是默认构造函数

}


....不是默认构造函数,编译器会为
$ b $创建一个b你含蓄地说。

我是否在正确的轨道上? IOW,一个默认构造函数只是一个构造函数
,它不接收任何参数,但你仍然可以编写代码来做你想要的东西吗?




很多。


干杯,

Andre


Ook写道:

我无法理解究竟什么是默认构造。是。我知道如何为构造函数提供初始值,所以如果我在
示例中,在我的代码中执行此操作:

MyClass zoot(1,2);

它将实例化MyClass,将1和2传递给我的构造函数,在
转向它将执行我想要的任何操作。默认构造函数是否如下所示:

MyClass zoot;

没有参数传递?


是的,它会的。

在我的班级宣言中,我会这样做:

类Zoot
{
Zoot(); //默认构造函数?
Zoot(int a,int b); //具有初始值的构造函数。
...
}
;

Zoot :: Zoot()
//默认构造函数,没有参数传递,做你想做的事情吗?
}


我不确定你的意思是做你想做什么,但是,这是

默认的c-tor。

Zoot :: Zoot(int a,int b)
//
//构造函数的初始值
}

我走在正确的轨道上? IOW,一个默认构造函数只是一个构造函数
,它不接收任何参数,但你仍然可以编写代码来做你想要的东西吗?




默认构造函数是可以在不指定任何

参数的情况下调用的构造函数。例如,


class has_def_ctor {

public:

has_def_ctor(int a = 42);

};


也宣布默认的c-tor。


V


< blockquote>

" Ook" <别给我发送任何'垃圾邮件'>在消息中写道

新闻:Tu ******************** @ giganews.com ...

我我无法理解究竟什么是默认结构。是。我知道如何为构造函数提供初始值,所以如果我在
示例中,在我的代码中执行此操作:

MyClass zoot(1,2);

它将实例化MyClass,将1和2传递给我的构造函数,在
转向它将执行我想要的任何操作。默认构造函数是否如下所示:

MyClass zoot;

没有参数传递?在我的课堂宣言中,我会这样做:

课Zoot
{Z / Z; //默认构造函数?
Zoot(int a,int b); //具有初始值的构造函数。
...

Zoot :: Zoot()
{//默认构造函数,没有传递参数,做任何你想做的事情?
} Zoot :: Zoot(int a,int b)
{
//带初始值的构造函数
}

我是在正确的轨道上吗? IOW,一个默认构造函数只是一个没有接收参数的构造函数,但你仍然可以编写代码来实现你想要的东西吗?




差不多。默认构造函数是一个构造函数,可以在没有参数的情况下调用
。它可能有也可能没有正式的

参数。如果它确实定义了参数,并且它们都具有默认值,那么它被认为是默认的

构造函数(因为它仍然可以不带参数调用)。


class C

{

public:

C(){} // 1)默认ctor

C(int i = 0){} // 2)默认ctor

C(int i = 0,int j = 42,int x = 99) // 3)默认ctor

C(int i){} // 4)不是默认的ctor

C(int i,int j = 0){} / / 5)不是默认的ctor

};


注意上面包含的错误,因为一个类可以只有b $ b一个默认构造函数。所以只需要删除

1),2)和3)中的一个。


-Mike


I''m having trouble comprehending what exactly "default construction" is. I
know how to provide a constructor with initial values, so that if I, for
example, in my code do this:

MyClass zoot(1,2);

It would instantiate MyClass, passing 1 and 2 to my constructor which in
turn would do whatever I want it to. Would a default constructor be as
follows:

MyClass zoot;

With no parameters passed? In my class declaration, would I do this:

class Zoot
{
Zoot(); // default constructor?
Zoot( int a, int b); // constructor with initial values.
....
}

Zoot::Zoot()
{
// default constructor, no parameters passed, do whatever you want?
}
Zoot::Zoot( int a, int b )
{
// constructor with initial values
}

Am I on the right track? IOW, a default constructor is simply a constructor
that receives no parameters, but you can still write code to do what you
want with it?

解决方案


Ook wrote:

I''m having trouble comprehending what exactly "default construction" is. From the FAQ:
A "default constructor" is a constructor that can be called with no
arguments.
So to stay with your example, this could be a default constructor:

class Zoot()
{
public:
Zoot(); // Default constructor
}

But this could be a default constructor as well:

class Zoot()
{
public:
Zoot( int a = 0, int b = 5); // Default constructor
}

Since it has default values for a and b, you can still create a new
obect like so:

Zoot myObj;

However, this ...

class Zoot()
{
public:
Zoot( int a, int b); // NOT a default constructor
}

.... is NOT a default constructor and the compiler will create one for
you implicitly.
Am I on the right track? IOW, a default constructor is simply a constructor
that receives no parameters, but you can still write code to do what you
want with it?



Pretty much.

Cheers,
Andre


Ook wrote:

I''m having trouble comprehending what exactly "default construction" is. I
know how to provide a constructor with initial values, so that if I, for
example, in my code do this:

MyClass zoot(1,2);

It would instantiate MyClass, passing 1 and 2 to my constructor which in
turn would do whatever I want it to. Would a default constructor be as
follows:

MyClass zoot;

With no parameters passed?
Yes, it would.
In my class declaration, would I do this:

class Zoot
{
Zoot(); // default constructor?
Zoot( int a, int b); // constructor with initial values.
...
} ;

Zoot::Zoot()
{
// default constructor, no parameters passed, do whatever you want?
}
I am not sure what you mean by "do whatever you want", but yes, this is
the default c-tor.
Zoot::Zoot( int a, int b )
{
// constructor with initial values
}

Am I on the right track? IOW, a default constructor is simply a constructor
that receives no parameters, but you can still write code to do what you
want with it?



A default constructor is one that can be invoked without specifying any
arguments. For example,

class has_def_ctor {
public:
has_def_ctor(int a = 42);
};

also declares a default c-tor.

V



"Ook" <Don''t send me any freakin'' spam> wrote in message
news:Tu********************@giganews.com...

I''m having trouble comprehending what exactly "default construction" is. I
know how to provide a constructor with initial values, so that if I, for
example, in my code do this:

MyClass zoot(1,2);

It would instantiate MyClass, passing 1 and 2 to my constructor which in
turn would do whatever I want it to. Would a default constructor be as
follows:

MyClass zoot;

With no parameters passed? In my class declaration, would I do this:

class Zoot
{
Zoot(); // default constructor?
Zoot( int a, int b); // constructor with initial values.
...
}

Zoot::Zoot()
{
// default constructor, no parameters passed, do whatever you want?
}
Zoot::Zoot( int a, int b )
{
// constructor with initial values
}

Am I on the right track? IOW, a default constructor is simply a
constructor that receives no parameters, but you can still write code to
do what you want with it?



Almost. A default constructor is a constructor which can be
invoked with no arguments. It might or might not have formal
parameters. If it does define parameters, and they all have
been given default values, then it is considered a default
constructor (because it can still be called with no arguments).

class C
{
public:
C() {} // 1) default ctor
C(int i = 0) {} // 2) default ctor
C(int i = 0, int j = 42, int x = 99) // 3) default ctor
C(int i) {} // 4) not a default ctor
C(int i, int j = 0) {} // 5) not a default ctor
};

Note that the above contains errors, because a class can
only have one default constructor. So all but one of
1), 2), and 3) would need to be removed.

-Mike


这篇关于默认构造与具有初始值的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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