Topic: union members should be able to have 'blank' default constructors?
Author: "Rich Burkert" <rrburkert@hotmail.com>
Date: Mon, 23 Apr 2001 20:09:13 GMT Raw View
Okay so I understand that union members can't have constructors, the reason
being that you can't insure the integrity of all of the members at once, but
I think this neglects that sometimes one might define constructors purely
for convenience but also additionally define a "blank" default constructor
that leaves all member data uninitialized as would be the case if the type
had no constructors (I don't know if it is correct to speak of a class as
having 'no constructors', until this issue I was under the impression that
all classes that didnt have any explicitly specified constructors at least
theoretically had an automatically generated default (as well as copy)
constructor, even if it did nothing). Take the following example:
class umemA_t {
int data;
// this is just a convenience constructor
umemA_t (int _data) : data(_data) {}
// but i still want to enable an object to be constructed
uninititialized
// like would have been done if i hadnt made this convenience
constructor
// so now i need to just define a 'blank' default constructor
umemA_t () {}
};
class umemB_t {
float data;
umemB_t (float _data) : data(_data) {}
umemB_t () {}
};
class thisorthat_t {
bool usememA;
union {
umemA_t memA;
umemB_t memB;
};
};
I think I should still be able to create such a union, as the default
constructors for the members don't initialize any data. The only reason I
had to create them was because I found it useful to have some 'convenience'
constructors that are useful elsewhere, but not necessary. Wouldn't it be
proper to extend the specification of union members to be anything that
either has no constructors or has what I call a 'blank' default constructor,
that is one that literally does nothing, initializes no member data and
executes nothing? Thanks for any clarifications.
Rich
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Markus Schaaf" <m.schaaf.exp-jun2001@gmx.de>
Date: Tue, 24 Apr 2001 08:52:04 GMT Raw View
"Rich Burkert" <rrburkert@hotmail.com> wrote:
> constructors that are useful elsewhere, but not necessary. Wouldn't it be
> proper to extend the specification of union members to be anything that
> either has no constructors or has what I call a 'blank' default constructor,
It might get difficult to decide whether a c'tor is actually
'blank' or not. Consider for example:
class example {
example() { cout << "blah\n"; }
};
Should be logically 'blank', shouldn't it?
I'd rather prefer union c'tors to be allowed:
union example2 {
int a;
double b;
example2() : a( 3 ) {}
};
Of course you could only call one member c'tor per union c'tor.
regards
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 24 Apr 2001 23:26:11 GMT Raw View
Rich Burkert wrote:
>
> Okay so I understand that union members can't have constructors, the reason
More precisely, it can't have a non-trivial constructor.
> being that you can't insure the integrity of all of the members at once, but
> I think this neglects that sometimes one might define constructors purely
> for convenience but also additionally define a "blank" default constructor
> that leaves all member data uninitialized as would be the case if the type
> had no constructors (I don't know if it is correct to speak of a class as
> having 'no constructors', until this issue I was under the impression that
> all classes that didnt have any explicitly specified constructors at least
> theoretically had an automatically generated default (as well as copy)
> constructor, even if it did nothing). Take the following example:
All classes do have constructors. If one is not explicitly declared, one
will be implicitly declared. If an implicitly declared constructor is
not explicitly defined, it will be implicitly defined. However, per
12.1p5:
"A constructor is _trivial_ if it is an implicitly declared default
constructor and if:
-- its class has no virtual functions (10.3) and no virtual base classes
(10.1) and
-- all the direct base classes of its class have trivial constructors,
and
-- for all the non-static data members of its class that are of class
type (or array thereof), each such class has a trivial constructor."
> class umemA_t {
> int data;
> // this is just a convenience constructor
> umemA_t (int _data) : data(_data) {}
> // but i still want to enable an object to be constructed
> uninititialized
> // like would have been done if i hadnt made this convenience
> constructor
> // so now i need to just define a 'blank' default constructor
> umemA_t () {}
> };
>
> class umemB_t {
> float data;
> umemB_t (float _data) : data(_data) {}
> umemB_t () {}
> };
>
> class thisorthat_t {
> bool usememA;
> union {
> umemA_t memA;
> umemB_t memB;
> };
> };
If you wish to do something like this in standard C++, you should first
of all notice all of your constructors are implicitly declared as
private, and you have declared no friend functions. You've therefore
guaranteed that no objects of your class types can be constructed. You
need to either use "public:", or replace "class" with "struct".
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]