Topic: Q: default constructor for fundamental types?
Author: dirk@sallust.ida.ing.tu-bs.de (Dirk Herrmann)
Date: 1996/02/26 Raw View
Sorry if this has been discussed before.
When using templates, it appears that there should be
default constructors for the fundamental data types.
Example:
template <class T>
X {
public:
X() : t() {};
X(const T& tt) : t(tt) {};
private:
T t;
}
As far as i know, the default constructor of
e.g. X<int> would not compile since there is no default
constructor for an int value.
It seems to be a pity that this would not be allowed.
--
Dirk Herrmann
---
[ To submit articles: Try just posting with your newsreader. If that fails,
use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: gusty@clark.net (Harlan Messinger)
Date: 1996/02/26 Raw View
Dirk Herrmann (dirk@sallust.ida.ing.tu-bs.de) wrote:
: Sorry if this has been discussed before.
:
: When using templates, it appears that there should be
: default constructors for the fundamental data types.
Essentially, there is.
:
: Example:
:
: template <class T>
: X {
: public:
: X() : t() {};
: X(const T& tt) : t(tt) {};
:
: private:
: T t;
: }
:
: As far as i know, the default constructor of
: e.g. X<int> would not compile since there is no default
: constructor for an int value.
: It seems to be a pity that this would not be allowed.
But it appears that it is allowed. It works fine in Visual C++ 2.2.
There's are just two things: you left out the word "class" between "template
<class T>" and the word "X"; and you need a semicolon after the closing
right brace of the class definition.
The following produces output of "8" on the first line and gobbledygook
on the second line, since the effect of the "default constructor" of the
integer member function is to do nothing.
If your X class were only meant for numeric types, you could have the
default constructor of X call the constructor for t as t(0) instead of
t() if you always wanted default initialization to 0.
#include <iostream.h>
template <class T>
X {
public:
X() : t() {};
X(const T& tt) : t(tt) {};
void output() { cout << t << "\n"; }
private:
T t;
};
int main()
{
X<int> i(8);
X<int> j;
i.output();
j.output();
int x;
cin >> x; // Pause for input: this is a QuickWin program.
return 0;
}
[ To submit articles: Try just posting with your newsreader.
If that fails, use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: oliva@grande.dcc.unicamp.br (Alexandre Oliva)
Date: 1996/02/27 Raw View
Dirk Herrmann writes:
> Sorry if this has been discussed before.
> When using templates, it appears that there should be
> default constructors for the fundamental data types.
You need not invoke default constructors explicitly. By simply
removing ": t()" from the following code snippet, it will compile fine
even for X<int>. For types which have a default constructor, it will
be invoked, as it should.
template <class T>
class X {
private:
T t;
public:
X() /* : t() */ {}
};
--
Alexandre Oliva
oliva@dcc.unicamp.br
Universidade Estadual de Campinas, S~ao Paulo, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jamshid@io.com (Jamshid Afshar)
Date: 1996/02/28 Raw View
In article <9602271131.AA18225@lts.sel.alcatel.de>,
James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
>Expressions of the form `int()' have always been allowed. A (fairly)
>recent change to the draft standard states that they have the same
>value as default static initialization (in this case, 0). The earlier
>rules had the initailization undefined.
Is the following legal?
class Foo {
int x;
public:
Foo()
: x() // legal?
{}
};
and if so is it different than just:
Foo::Foo() {}
--Jam
---
[ To submit articles: try just posting with your news-reader.
If that fails, use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu.
]
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/02/28 Raw View
jamshid@io.com (Jamshid Afshar) writes:
>James Kanze <kanze@lts.sel.alcatel.de> wrote:
>>Expressions of the form `int()' have always been allowed. A (fairly)
>>recent change to the draft standard states that they have the same
>>value as default static initialization (in this case, 0). The earlier
>>rules had the initailization undefined.
>
>Is the following legal?
>
> class Foo {
> int x;
> public:
> Foo()
> : x() // legal?
> {}
> };
Yes.
>and if so is it different than just:
>
> Foo::Foo() {}
Yes, they are different. The former is equivalent to
Foo::Foo() : x(0) {}
This can be deduced from the following sections of the working paper:
[class.base.init] 12.6.2/3
"if the *expression-list* of the *mem-initializer* is
omitted, the base class or member subobject is
default-initialized (see [dcl.init])"
[dcl.init] 8.5/5
"To default-initialize an object of type T means:
--if T is a non-POD class type ...;
--if T is an array type ...;
--otherwise, the storage for the object is zero-initialized."
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ To submit articles: try just posting with your news-reader.
If that fails, use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu.
]