The base class can have a protected constructor which only derived
classes will invoke. When it's called the base class object knows
it is constructing for a derived class, otherwise it is constructing
for a base class.
My first reaction to your question was that this was poor design.
This was because you phrased your question as if the base class
constructor should "know" something about classes derived from it.
Since a user is able to add a derived class later, you cannot code
base classes with any knowledge of as-yet-unwritten derived classes.
It is more reasonable to say that the derived class is aware of its
base class and calls a special constructor. In this instance it is
just a difference in point of view, but I think you'd be better off
avoiding this if you can. For example, there's nothing to stop
someone from adding a derived class that calls your base class's
public constructor.
For what it's worth...
--
jim mullens
mul...@jamsun.ic.ornl.gov (128.219.64.31)
voice: 615-574-5564
FAX: 615-574-4058
>Is there any way within a constructor to determine whether or not the
>constructor is being called with respect to a base or a derived class
The constructor is being called ``with respect to'' its class.
It sounds like your question is really:
Is there some way of telling in the base class constructor
whether or not the base class object is a sub-object of a
derived class object.
The easiest way of detecting this would be by using an argument
to the constructor: For ex,
#include <iostream.h>
struct base {
protected:
base(int) /* construct sub object */ { }
public:
base(void) /* construct main object */ { }
};
struct derived : base {
derived(void) : base(0) /* use protected base constructor */ { }
};
int main(void)
{
base b; /* uses base::base(void) */
derived d; /* uses base::base(int) */
return 0;
}
---
Daniel Edelson | New motto, copy-collection for C++ is out:
dan...@cis.ucsc.edu, or | ``Don't dangle, recycle, and don't
uunet!ucscc!terra!daniel | make me sweep up after you.''
|Is there any way within a constructor to determine whether or not the
|constructor is being called with respect to a base or a derived class
Hm, I thought I knew what you were asking, now I'm not too sure.
Are you asking whether there is a way to tell if a copy constructor
is being invoked with a base or derived for a parameter? Or are you
asking whether one can tell if a base constructor is being invoked
to construct a base object verses if it is being invoked to help
construct a derived object.
The answer in either case is "yes, kindof."
In either case, the object itself can't tell you, because, again,
in the base class constructor, the object *is* only a base, and thus
cannot tell you anything about any derived class. [Assuming one doesn't
give a class that capability by implementing a class Class]
So the answer is the information must be passed into the constructor
via some external agent, such as a parameter to that constructor.
[In the case of a copy constructor, the parameter could be that object
being copied, assuming object's know their type. Here's the not-a-
copy-constructor case:]
extern "C"
{
#include "stdio.h"
}
typedef const char* RuntimeType; // pretend we really had real runtime types.
class Base
{
public:
Base(RuntimeType type="Base")
{ printf("Base constructor being invoked to construct a %s\n", type); }
};
class Dirv : public Base
{
public:
Dirv(RuntimeType type="Dirv") : Base(type)
{ printf("Dirv constructor being invoked to construct a %s\n", type); }
};
main()
{
Base b;
printf("----\n");
Dirv d;
}