从派生类构造函数调用基类构造函数 [英] call base class constructor from derived class constructor

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

问题描述

大家好,

在使用Java的时候,我遇到了super(),它从派生类构造函数中将值传递给了

基类构造函数。我想知道

如果可以通过任何机制在c ++中实现,因为超级不是由c ++支持的
,至少是MS vc ++ 6.0。

解决方案



" Rahul" < sa ***** @ yahoo.co.inwrote in message

news:11 ********************** @ v29g2000prd.googlegr oups.com ...


大家好,


在使用Java时,我遇到了super( )从派生类构造函数将值传递给

基类构造函数。我想知道

如果可以通过任何机制用c ++实现,因为超级不是由c ++支持的
,至少是MS vc ++ 6.0。



#include< iostream>


class Base

{

int i;

public:

Base(int arg_b = 0):i(arg_b)

{

std :: cout<< 基础构造函数:i = << i<< ''\ n'';

}

};


类派生:公共基地

{

public:

Derived(int arg_d):Base(arg_d)

{

} < br $>
};


int main()

{

派生d(42);

返回0;

}

输出:

基础构造函数:i = 42

-Mike


11月5日凌晨3:12,Rahul< sam _... @ yahoo.co.inwrote:


大家好,


在使用Java的时候,我遇到了super(),它将值传递给了

基类派生类构造函数的构造函数。我想知道

如果可以通过任何机制用c ++实现,因为超级不是由c ++支持的
,至少是MS vc ++ 6.0。



您好Rahul


C ++没有超级关键字。相反,C ++基于成员

初始化列表。派生类构造函数应该调用

基类构造函数(默认构造函数可以调用

隐式)。这样的初始化,初始化派生的

类对象的基础部分。


问候,

S. Amrollahi


Rahul写道:


大家好,


工作时使用Java,我遇到了super(),它从派生类构造函数将值传递给

基类构造函数。我想知道

如果可以通过任何机制在c ++中实现,因为超级不是由c ++支持的
,至少是MS vc ++ 6.0。


C ++不是Java。在C ++中用

语言定义的序列自动调用构造函数。你无法改变NOR

你可以直接给他们打电话。


Hi Everyone,

While working with Java, i came across super() which passes values to
base class constructor from derived class constructor. I was wondering
if this could be implemented in c++ by any mechanism as super is not
supported by c++, atleast by MS vc++ 6.0.

解决方案


"Rahul" <sa*****@yahoo.co.inwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...

Hi Everyone,

While working with Java, i came across super() which passes values to
base class constructor from derived class constructor. I was wondering
if this could be implemented in c++ by any mechanism as super is not
supported by c++, atleast by MS vc++ 6.0.

#include <iostream>

class Base
{
int i;
public:
Base(int arg_b = 0) : i(arg_b)
{
std::cout << "Base constructor: i = " << i << ''\n'';
}
};

class Derived : public Base
{
public:
Derived(int arg_d) : Base(arg_d)
{
}
};

int main()
{
Derived d(42);
return 0;
}
Output:
Base constructor: i = 42
-Mike


On Nov 5, 3:12 am, Rahul <sam_...@yahoo.co.inwrote:

Hi Everyone,

While working with Java, i came across super() which passes values to
base class constructor from derived class constructor. I was wondering
if this could be implemented in c++ by any mechanism as super is not
supported by c++, atleast by MS vc++ 6.0.

Hi Rahul

C++ doesn''t have super keyword. Instead, C++ is based on Member
Initialization List. the derived class constructor should call the
base class constructor (the default constructor can be invoked
implicitly). Such initialization, initilizes the base part of derived
class object.

Regards,
S. Amrollahi


Rahul wrote:

Hi Everyone,

While working with Java, i came across super() which passes values to
base class constructor from derived class constructor. I was wondering
if this could be implemented in c++ by any mechanism as super is not
supported by c++, atleast by MS vc++ 6.0.

C++ is not Java. Contructors are automatically called in the
language-defined sequence in C++. You can not alter that NOR
can you call them directly.


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

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