473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Simple constructor question

(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.
Jul 19 '05 #1
11 2387
Christopher Benson-Manica wrote:
(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.

Jul 19 '05 #2

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bm**********@chessie.cirr.com...
(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.
Jul 19 '05 #3
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

--
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
Jul 19 '05 #4

"David Rubin" <bo***********@nomail.com> wrote in message
news:3F***************@nomail.com...
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.
Jul 19 '05 #5
jeffc wrote:

[snip]
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.


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

--
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
Jul 19 '05 #6

"David Rubin" <bo***********@nomail.com> wrote in message news:3F***************@nomail.com...
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?
Jul 19 '05 #7
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.
Jul 19 '05 #8

"David Rubin" <bo***********@nomail.com> wrote in message
news:3F***************@nomail.com...

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.


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.
Jul 19 '05 #9

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"David Rubin" <bo***********@nomail.com> wrote in message news:3F***************@nomail.com...
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.


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.)
Jul 19 '05 #10

"Julián Albo" <JU********@terra.es> wrote in message news:3F***************@terra.es...
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.
Jul 19 '05 #11
Ron Natalie escribió:
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.


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.
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
6
by: Hunter Hou | last post by:
Hello, I have this very simple program, but it can't be compiled. What's wrong here? thanks, hunter #include <iostream>
3
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
10
by: todd_montana | last post by:
Hi there, I have a class that has the private member variable called 'int frame'. I then have a constructor that sets it to 0. Now, inside the constructor i want to increment it by using: ...
1
by: Erik | last post by:
Where is the "MTAThread()" method? I'm writing a service in VB.Net. Being of a curious nature, I looked at the generated code, which has the following: > <MTAThread()> _ > Shared Sub Main()...
4
by: DBC User | last post by:
I have 2 constructors in my class, first one with no paramter and the second one is with a paramter. I want the later constructor to use the first constructor and do some more with the parameter....
13
by: aaragon | last post by:
Hi everyone, I just wanted to know if there is any difference in performance in declarating the variables in the beginning of a function or within for loops. For example: double test() {...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
4
by: David Bonnell | last post by:
I have a Windows Forms app that is intended to launch via the SendTo feature. I want it to perform work (in the background) while updating a progress bar in the main window. The problem: I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.