Simple constructor question

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

(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() {}

?
 
G

Gianni Mariani

Christopher said:
(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() {}

It's implicit.

Easy to test with a few lines of example code.

The order of evaluation is interesting as well.
 
J

jeffc

Christopher Benson-Manica said:
(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

Yes.
 
D

David Rubin

Gianni Mariani wrote:

[snip]
The order of evaluation is interesting as well.

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
 
J

jeffc

David Rubin said:
Gianni Mariani wrote:

[snip]
The order of evaluation is interesting as well.

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.

i doesn't belong in the initializer list for A because it's not a member of
A. You can assign to i inside the body of A, yes, but you said "not in the
initializer list". There's a difference between initializing and assigning
that's important in this case. It's called the "initializer list" instead
of the "assignment list" because it's initialization, not assignment. If
you assign to i in the body of A, it's already been initialized by B.
 
D

David Rubin

jeffc wrote:

[snip]
i doesn't belong in the initializer list for A because it's not a member of
A. You can assign to i inside the body of A, yes, but you said "not in the
initializer list". There's a difference between initializing and assigning
that's important in this case. It's called the "initializer list" instead
of the "assignment list" because it's initialization, not assignment. If
you assign to i in the body of A, it's already been initialized by B.

The confusion is that A inherits member i from B. Therefore, why is i
not a member of A when A is being constructed? It would follow that if i
is a member of A, i can be initialized in A(), but this is clearly not
the case.

/david
 
R

Ron Natalie

David Rubin said:
The confusion is that A inherits member i from B. Therefore, why is i
not a member of A when A is being constructed? It would follow that if i
is a member of A, i can be initialized in A(), but this is clearly not
the case.

Inheritance is not the issue. The initializer list is restricted to DIRECT bases,
VIRUTAL bases, and members of the class being initialized. Members of
base classes (or non-direct base classes) are not eligible.

There's some ambiguity here. Which value should B::i be initialized with 10 or 20?
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

David Rubin escribió:
The confusion is that A inherits member i from B. Therefore, why is i
not a member of A when A is being constructed? It would follow that if i
is a member of A, i can be initialized in A(), but this is clearly not
the case.

It is a member of B, then it gets initialized during the initialization
of B. The if initilize it in the constructor of A, it will be
initialized two times, and that is not desired in C++.

Regards.
 
J

jeffc

David Rubin said:
The confusion is that A inherits member i from B. Therefore, why is i
not a member of A when A is being constructed? It would follow that if i
is a member of A, i can be initialized in A(), but this is clearly not
the case.

But i is *never* a member of A, not when A is being constructed, and not
after it's been constructed. A does have access rights to i, but i is a
member of B, not of A. I don't think it's accurate to say that A inherits
member i from B (someone correct me if I'm wrong). But even if it were OK
to use the terminology that A inherits i from B, you still would not say
that i is a member of A.
 
J

jeffc

Ron Natalie said:
Inheritance is not the issue. The initializer list is restricted to DIRECT bases,
VIRUTAL bases, and members of the class being initialized.

But inheritance is the issue from his point of view, because he believes i
IS a member of the class being initialized (precisely because A inherits
from B.)
 
R

Ron Natalie

Julián Albo said:
It is a member of B, then it gets initialized during the initialization
of B. The if initilize it in the constructor of A, it will be
initialized two times, and that is not desired in C++.

Not only not desired, it's not even possible.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Ron Natalie escribió:
Not only not desired, it's not even possible.

Yeah, and it's not possible because the designers of the language found
did not desire it. I was trying to explain why is not allowed.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top