继承的构造函数 [英] inherited constructors

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

问题描述

大家好,


请注意以下代码...为什么当你使用带有参数的构造函数调用

子元素时,父构造函数

被默认调用?如何告诉Child(int)构造函数使用

父(int)构造函数?


我的结果是:

父亲默认构造函数

儿童Arg构造函数


//

//示例代码:

//

#include< iostream.h>


//父类

类父亲

{

public:

父亲(int newX):x(newX)

{cout<< Father Arg Constructor << ENDL; }


父亲():x(0)

{cout<< 父默认构造函数 << endl;}


私人:

int x;

};


//继承自父类的子类

class子项:public Father

{

public:

Child(int newX)

{cout<< Child Arg Constructor << endl;}

};

int main(无效)

{

Child xyz(4);

返回0;

}

解决方案



" Brian Genisio" <峰; br ********** @ yahoo.com>在留言新闻中写道:3f ******** @ 10.10.0.241 ...

|大家好,

|

|请注意以下代码......为什么当你打电话给

|时具有参数的构造函数的子,父构造函数

|被称为默认?如何告诉Child(int)构造函数使用

|父(int)构造函数?


[snip]


您调用它的方式与'x'完全相同:


class孩子:公共父亲

{

public:

Child(int newX) :父亲(newX)

{cout<< Child Arg Constructor <<结束;}

};


干杯。

Chris Val


< blockquote> Brian Genisio写道:

大家好,

请注意以下代码......为什么当你打电话给
孩子时使用具有参数的构造函数,父
构造函数被调用为默认值?


这就是语言的定义方式。如果你没有指定要调用的构造函数

,则使用默认构造函数。

如何告诉Child(int)构造函数使用父( int)
构造函数?


虽然是初始化列表。见下文。

我的结果是:
父亲默认构造函数
儿童Arg构造函数

//
//示例代码:
//
#include< iostream.h>


这是一个古老的标准前标题。使用< iostream>相反。

//父类
类父亲
{
公开:
父亲(int newX):x(newX)
{cout<< Father Arg Constructor << ENDL;父亲():x(0)
{cout<< 父默认构造函数 <<私有:
int x;
};

//子类,继承自父类


上面是一个完全没用评论的好例子。

class孩子:公共父亲
{
公众:
孩子( int newX)


:父亲(newX)

{cout<< Child Arg Constructor << endl;}
};

int main(无效)
{
儿童xyz(4);
返回0;
}




Rolf Magnus< ra ****** @ t-online.de>在消息中写道

news:bt ************* @ news.t-online.com ...

Brian Genisio写道:

大家好,

请注意以下代码...为什么当你用一个构造函数调用
子代时参数,父
构造函数被默认调用?



这就是语言的定义方式。如果你没有指定要调用的构造函数,则使用默认构造函数。



我在这种情况下对默认构造函数的理解是默认的
当提供构造函数时,
构造函数不在您的服务范围内。显然

这不是真的。有人可以直接解决这个问题吗(这也可能是因为OP混淆了b $ b)。谢谢!


Hi all,

Please note the following code... Why is it that when you call the
child with the a constructor that has arguments, the parent constructor
gets called as default? How do I tell the Child(int) constructor to use
the Father(int) constructor?

My results are:
Father default constructor
Child Arg Constructor

//
// Sample Code:
//
#include <iostream.h>

// The parent class
class Father
{
public:
Father(int newX) : x(newX)
{ cout << "Father Arg Constructor" << endl; }

Father() : x(0)
{ cout << "Father default constructor" << endl;}

private:
int x;
};

// The child class, that inherits from the parent class
class Child : public Father
{
public:
Child(int newX)
{ cout << "Child Arg Constructor" << endl;}
};
int main(void)
{
Child xyz(4);
return 0;
}

解决方案


"Brian Genisio" <Br**********@yahoo.com> wrote in message news:3f********@10.10.0.241...
| Hi all,
|
| Please note the following code... Why is it that when you call the
| child with the a constructor that has arguments, the parent constructor
| gets called as default? How do I tell the Child(int) constructor to use
| the Father(int) constructor?

[snip]

You invoke it in exactly the same way you did for ''x'':

class Child : public Father
{
public:
Child(int newX) : Father( newX )
{ cout << "Child Arg Constructor" << endl;}
};

Cheers.
Chris Val


Brian Genisio wrote:

Hi all,

Please note the following code... Why is it that when you call the
child with the a constructor that has arguments, the parent
constructor gets called as default?
That''s how the language is defined. If you don''t specify the constructor
to be called, the default constructor is used.
How do I tell the Child(int) constructor to use the Father(int)
constructor?
Though the initializer list. See below.
My results are:
Father default constructor
Child Arg Constructor

//
// Sample Code:
//
#include <iostream.h>
This is an ancient pre-standard header. Use <iostream> instead.
// The parent class
class Father
{
public:
Father(int newX) : x(newX)
{ cout << "Father Arg Constructor" << endl; }

Father() : x(0)
{ cout << "Father default constructor" << endl;}

private:
int x;
};

// The child class, that inherits from the parent class
The above is a nice example of a totally useless comment.
class Child : public Father
{
public:
Child(int newX)
: Father(newX)
{ cout << "Child Arg Constructor" << endl;}
};
int main(void)
{
Child xyz(4);
return 0;
}




Rolf Magnus <ra******@t-online.de> wrote in message
news:bt*************@news.t-online.com...

Brian Genisio wrote:

Hi all,

Please note the following code... Why is it that when you call the
child with the a constructor that has arguments, the parent
constructor gets called as default?



That''s how the language is defined. If you don''t specify the constructor
to be called, the default constructor is used.


My understanding about default constructor in this situation is that default
constructor is not at your service when a constructor provided. Obviously
this is not true here. Could someone straight this out(This could also be
confusing to OP.)? Thanks!


这篇关于继承的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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