Topic: Array constructor?


Author: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1997/08/02
Raw View
Hi,
John B. Williston (wconsult@ix.netcom.com) wrote:
: This has probably been hashed to death already, but it is a new issue to me
: (grin). I was wondering if it is possible in C++ (or should it be
: possible?) to define a constructor used only when initializing array
: objects?

You mean you want a constructor of an element type which is only called
if an array is constructed? Well, per se C++ does not really support
it.  What you can do is to initialize an arry elementwise:

  T t[] = { T(1), T(2), T(3) };

Of course, you can then call an arbitary constructor... You might also
be able to put the class into a wrapper which defines the default
constructor to call the constructor you want to get called. However,
this approach probably creates more problems than it solves.

In general, I recomment to avoid built-in arrays anyway. They are only
useful to implement array classes, and even there they use is limited
(often, only the pointer arithmetic for arary objects is actually
used...).

: I have a situation in which it would be terribly handy to be able
: to pass values to objects at time of construction. I know that I can
: architect classes so that this isn't necessary, but this is a case where
: code reuse is involved and I do not have access to the original source
: code. Thanks in advance!

I doubt that you are in a position then, where you can change the
constructors of involved classes anyway! Aparently, your array is
created within some compiled code (outherwise you would be able to use
some class which hands out a pointer to a manually constructed array
objet) and then you cannot change the interface of the element type.
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ 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         ]
[ 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: "John B. Williston" <wconsult@ix.netcom.com>
Date: 1997/07/31
Raw View
Greetings,

This has probably been hashed to death already, but it is a new issue to me
(grin). I was wondering if it is possible in C++ (or should it be
possible?) to define a constructor used only when initializing array
objects?  I have a situation in which it would be terribly handy to be able
to pass values to objects at time of construction. I know that I can
architect classes so that this isn't necessary, but this is a case where
code reuse is involved and I do not have access to the original source
code. Thanks in advance!

John

--
http://www.netcom.com/~wconsult
___        ___
\  \  __  /  /            Williston Consulting
 \  \/  \/  / __________  makes software worth buying.
  \   /\   / /  _______/  wconsult@ix.netcom.com
   \_/  \_/ /  /
           /  /_______
          /__________/
---
[ 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
]