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

问题描述

(如果这是一个FAQ,我道歉 - 我没看到它)


我有以下课程声明:


class TMSViewDayList:public TMSViewDayListBase {

public:

uint start;

uint end;

TMSViewDayList( ):start(0),end(0){}

};


TMSViewDayListBase是一个类似于矢量的模板类。我的

问题是,TMSViewDayListBase的默认构造函数在这里隐含地称为

,或者我需要明确地将其称为


TMSViewDayList():start(0),end(0),TMSViewDayListBase(){}




-

Christopher Benson-Manica |在你的命运转向轮子,

ataru(at)cyberspace.org |在你的课上学习。

(if this is a FAQ, I apologize - I didn''t see it)

I have the following class declaration:

class TMSViewDayList : public TMSViewDayListBase {
public:
uint start;
uint end;
TMSViewDayList() : start(0),end(0) {}
};

TMSViewDayListBase is a template class that acts something like a vector. My
question is, is TMSViewDayListBase''s default constructor implicitly called
here, or do I need to explicitly call it like

TMSViewDayList() : start(0),end(0),TMSViewDayListBase() {}

?

--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.

推荐答案




这是隐含的。


易于测试几行示例代码。


评估顺序也很有趣。



It''s implicit.

Easy to test with a few lines of example code.

The order of evaluation is interesting as well.





是。



Yes.





这很有趣。例如:


B级

{

受保护:

int i;

public:

B():i(10){}

};


A类:公共B

{

int j;

public:

A():i(20),j (30){}

};


int

main()

{

A a;

返回0;

}


; g ++ derive.cc

derive.cc:在构造函数中`A :: A()'':

derive.cc:14:错误:类'A''做没有任何名为我的字段


这里发生了什么?在A()体内分配i可以在初始化列表中工作,但不是



i(20)之前在初始化列表中调用B()也不起作用。


/ david


-

安德烈,一个简单的农民,只有一件事在他的脑海里沿着东墙悄悄地走过来:''安德烈,蠕动......安德烈,蠕动...安德烈,蠕动。''

- 未知



This is interesting. For example:

class B
{
protected:
int i;
public:
B() : i(10) {}
};

class A : public B
{
int j;
public:
A() : i(20), j(30) {}
};

int
main()
{
A a;
return 0;
}

; g++ derive.cc
derive.cc: In constructor `A::A()'':
derive.cc:14: error: class `A'' does not have any field named `i''

What is going on here? Assigning i inside the body of A() works, but not
in the initializer list. Invoking B() in the initialization list before
i(20) does not work either.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: ''Andre, creep... Andre, creep... Andre, creep.''
-- unknown


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

08-24 00:58