在VC ++中从另一个构造函数调用 [英] Calling one constructor from another in VC++

查看:74
本文介绍了在VC ++中从另一个构造函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++和VC ++相当陌生,但在大多数情况下,它似乎完成了大部分

与Java相同的事情,只有一些语法和

结构调整。但是,有一点我无法弄清楚

是如何在一个类中调用另一个构造函数。从派生类调用基类的构造函数很容易

,但那不是我想要做的事情。 。


例如,在Java(或J#)中,这很容易做到:


公共类圈

{

private int PointX;

private int PointY;

private int Radius;


public Circle()

{

this.PointX = 0;

this.PointY = 0;

this.Radius = 0;

}


public Circle(int x,int y)

{

this();

this.PointX = x;

this.PointY = y;

}


public Circle(int x,int y,int r)

{

this(x,y);

this.Radius = r;

}

}


然而,尝试在VC ++中做同样的事情并不是'工作:


// Sphere.h(部分文件)

class Sphere

{

public:

Sphere(void);

Sphere(int x,int y);

Sphere(int x,int y,int r);

~Sphere(无效);


私人:

int PointX;

int PointY;

int Radius;

};


// Circle.cpp(部分文件)

#include"。 \ circle.h"


Circle :: Circle(无效)

{

this-> PointX = 0;

this-> PointY = 0;

this-> Radius = 0;

}


Circle :: Circle(int x,int y)

{

Circle();

this-> PointX = x ;

this-> PointY = y;

}


Circle :: Circle(int x,int y,int r)

{

Circle(x,y);

this-> Radius = r;

} $ / $

在C ++版本中,我也尝试使用this()而不是Circle(),

导致编译错误。我也试过没有这个 - >说明者,

没有任何区别。如果你调用第二个构造函数

的最终结果是X和Y值已经设置,并且Radius未初始化。

如果你使用第三个构造函数,你得到完全相反的结果。 (并且

我并不是说它们的值为零;用调试器查看它们,我/ b $ b看到未初始化成员的值为-842150451。)


那么在C ++中是否有任何方法让一个构造函数构建在另一个构建器上,例如

这个?


谢谢你任何帮助。

解决方案

Peter E. Granger写道:

我是C ++和VC ++的新手,但是在大多数情况下,它似乎做了大部分可以在Java中完成的事情,仅进行了一些语法和结构调整。


实际上,你可以在C ++中做很多事情,根本没有Java中的对应物。你会及时发现它们。

然而,有一件我无法理解的是如何从一个类中调用另一个构造函数
。从派生类中调用基类的构造函数很容易,但这不是我想要做的。



在C ++中无法做到这一点。与Java不同,在C ++开头的

构造函数体的第一个语句中,所有成员都已经初始化了(如果他们的构造函数已经运行)拥有他们)。在Java中

在构造函数体运行之前,所有内容都默认初始化,然后构造函数体(可能是冗余的)初始化所有内容。


获得相同的构造函数重用在C ++中,你必须将

初始化代码放入(私有/受保护)成员函数中,并从构造函数中调用它来获得



-cd


Peter E. Granger写道:

我很新C ++和VC ++,但在大多数情况下,它似乎完成了大部分与Java相同的事情,仅进行了一些语法和结构调整。但是,有一点我无法弄明白
是如何在一个类中调用另一个构造函数。从派生类调用基类的构造函数是很容易的,但那不是我想要做的事情。


< snip>

Circle :: Circle(int x,int y)
{
Circle();


上面的陈述创建了一个临时的Circle对象,该对象在声明结束时立即被销毁。

这个 - > PointX = x;
this-> PointY = y;
}

Circle :: Circle(int x,int y,int r)
{
Circle(x,y);


上面的陈述创建了一个临时的Circle对象,该对象在声明结束时立即被销毁。

这个 - > Radius = r;
}

在C ++版本中,我也尝试使用this()而不是Circle(),这会导致编译错误。我也试过没有这个 - >说明者,
没有区别。如果你调用第二个构造函数的最终结果
是已经设置了X和Y值,并且Radius未初始化。
如果使用第三个构造函数,则会得到完全相反的结果。 (并且
我不是说它们的值为零;用调试器查看它们,我看到未初始化成员的值为-842150451。)

所以在C ++中是否有任何方法让一个构造函数构建在另一个构建器上,如
这个?




对不起,没有。有时您可以将常见的初始化分解为一个单独的init()函数,但当然您在

成员初始化列表中没有提到的成员会有默认构造的时间

你可以调用init()。如果你有const成员,引用成员或没有默认ctors的
成员,你必须在每个

ctor'的成员初始化列表中初始化它们。


-

Doug Harrison

Microsoft MVP - Visual C ++


< blockquote>

" Doug Harrison [MVP]" < ds*@mvps.org>在消息中写道

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

Peter E. Granger写道:

我是C ++和VC ++的新手,但在大多数情况下它似乎做得最多
可以在Java中完成相同的事情,仅进行一些语法和结构调整。但是,有一件事我无法想象
outis如何在一个类中调用另一个构造函数。很容易
足以从派生类调用基类的构造函数,但那是'b $ b而不是我想要做的。
< snip>

Circle :: Circle(int x,int y)
{
Circle();



上述声明创建一个临时的Circle对象,



在语句结束时立即销毁。




对 - 那个'这是我预期会发生的事情。事实上,我以为我在原始消息中说过

,但是我看到我省略了它。

那么在C ++中是否有任何方法让一个构造函数构建在另一个构建器上,比如
这个?



抱歉,没有。有时您可以将公共初始化分解为单独的init()函数,但当然,您在
成员初始化列表中未提及的成员将在时间<默认构造你可以调用init()。如果你有const成员,引用成员或没有默认ctors的成员,你必须在每个
ctor'的成员初始化列表中初始化它们。




那是不幸的。这真的是一个方便的功能。但是你猜不到

所有的东西,语言,我猜。


感谢您的回复。


- 彼得


I''m fairly new to C++ and VC++, but for the most part it seems to do most of
the same things that can be done in Java, with just some syntactic and
structural adjustments. However, one thing I haven''t been able to figure out
is how to call one constructor from another within a class. It''s easy enough
to call the base class''s constructor from the derived class, but that''s not
what I''m trying to do.

For example, in Java (or J#) it''s easy to do this:

public class Circle
{
private int PointX;
private int PointY;
private int Radius;

public Circle()
{
this.PointX = 0;
this.PointY = 0;
this.Radius = 0;
}

public Circle(int x, int y)
{
this();
this.PointX = x;
this.PointY = y;
}

public Circle(int x, int y, int r)
{
this(x, y);
this.Radius = r;
}
}

However, trying to do the same thing in VC++ doesn''t work:

// Sphere.h (partial file)
class Sphere
{
public:
Sphere(void);
Sphere(int x, int y);
Sphere(int x, int y, int r);
~Sphere(void);

private:
int PointX;
int PointY;
int Radius;
};

// Circle.cpp (partial file)
#include ".\circle.h"

Circle::Circle(void)
{
this->PointX = 0;
this->PointY = 0;
this->Radius = 0;
}

Circle::Circle(int x, int y)
{
Circle();
this->PointX = x;
this->PointY = y;
}

Circle::Circle(int x, int y, int r)
{
Circle(x, y);
this->Radius = r;
}

In the C++ version, I also tried using this() instead of Circle(), which
caused a compiler error. I also tried doing it without the this-> specifier,
which made no difference. The end result if you call the second constructor
is that the X and Y values have been set, and the Radius is uninitialized.
If you use the third constructor, you get exactly the opposite results. (And
I don''t mean they have values of zero; looking at them with the debugger, I
see values of -842150451 for the uninitialized members.)

So is there any way in C++ to let one constructor build on another like
this?

Thanks for any help.

解决方案

Peter E. Granger wrote:

I''m fairly new to C++ and VC++, but for the most part it seems to do
most of the same things that can be done in Java, with just some
syntactic and structural adjustments.
Actually, there are many things you can do in C++ that have no counterpart
in Java at all. You''ll discover them in time.
However, one thing I haven''t
been able to figure out is how to call one constructor from another
within a class. It''s easy enough to call the base class''s constructor
from the derived class, but that''s not what I''m trying to do.



There''s no way to do this in C++. Unlike Java, in C++ at the start of the
first statement of a constructor''s body all of the members have been
initialized (their constructors have been run if they have them). In Java
everything gets default initialized before the constructor body is run, and
then the constructor body (perhaps redundantly) initializes everything.

To get the same "constructor re-use" effect in C++, you have to put the
initialization code into a (private/protected) member function and call it
from the constructor(s).

-cd


Peter E. Granger wrote:

I''m fairly new to C++ and VC++, but for the most part it seems to do most of
the same things that can be done in Java, with just some syntactic and
structural adjustments. However, one thing I haven''t been able to figure out
is how to call one constructor from another within a class. It''s easy enough
to call the base class''s constructor from the derived class, but that''s not
what I''m trying to do.
<snip>
Circle::Circle(int x, int y)
{
Circle();
The statement above creates a temporary Circle object, which is immediately
destroyed at the end of the statement.
this->PointX = x;
this->PointY = y;
}

Circle::Circle(int x, int y, int r)
{
Circle(x, y);
The statement above creates a temporary Circle object, which is immediately
destroyed at the end of the statement.
this->Radius = r;
}

In the C++ version, I also tried using this() instead of Circle(), which
caused a compiler error. I also tried doing it without the this-> specifier,
which made no difference. The end result if you call the second constructor
is that the X and Y values have been set, and the Radius is uninitialized.
If you use the third constructor, you get exactly the opposite results. (And
I don''t mean they have values of zero; looking at them with the debugger, I
see values of -842150451 for the uninitialized members.)

So is there any way in C++ to let one constructor build on another like
this?



Sorry, no. Sometimes you can factor the common initialization out into a
separate init() function, but of course members you don''t mention in the
member-initialization list will have been default-constructed by the time
you can call init(). If you have const members, reference members, or
members that don''t have default ctors, you must initialize them in each
ctor''s member initialization list.

--
Doug Harrison
Microsoft MVP - Visual C++



"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:ct********************************@4ax.com...

Peter E. Granger wrote:

I''m fairly new to C++ and VC++, but for the most part it seems to do most ofthe same things that can be done in Java, with just some syntactic and
structural adjustments. However, one thing I haven''t been able to figure outis how to call one constructor from another within a class. It''s easy enoughto call the base class''s constructor from the derived class, but that''s notwhat I''m trying to do.
<snip>

Circle::Circle(int x, int y)
{
Circle();



The statement above creates a temporary Circle object, which is


immediately destroyed at the end of the statement.



Right -- that''s what I expected would happen. In fact, I thought I''d said
that in my original message, but I see I omitted it.

So is there any way in C++ to let one constructor build on another like
this?



Sorry, no. Sometimes you can factor the common initialization out into a
separate init() function, but of course members you don''t mention in the
member-initialization list will have been default-constructed by the time
you can call init(). If you have const members, reference members, or
members that don''t have default ctors, you must initialize them in each
ctor''s member initialization list.



That''s unfortunate. It''s really a handy feature. But you can''t get
everything in one, language, I guess.

Thanks for the reply.

- Peter


这篇关于在VC ++中从另一个构造函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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