473,387 Members | 1,420 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,387 software developers and data experts.

Derived Class Constructors

Hi,
I apologize in advance if this is a very dumb question. I've been
struggling with this problem for some time: I have an abstract base
class called Base and n derived classes D1, D2, ....Dn. I would like
to have a constructor for each derived class that takes any of the
other derived classes as an argument so that these statements are
valid:

D1 d1;
D2 d2;

D3 d3a(d1);
D3 d3b(d2);

What is the syntax that I have to use for those statements to compile.
I realize that, for any given derived class i, I can't use Di (const
Base& b) as the constructor. Do i need to use virtual constructors? If
so, how?

Thanks for your help

May 2 '07 #1
4 1655
* nozyrev:
Hi,
I apologize in advance if this is a very dumb question.
Dumb questions are good, too clever wrong questions are ungood.

I've been
struggling with this problem for some time: I have an abstract base
class called Base and n derived classes D1, D2, ....Dn. I would like
to have a constructor for each derived class that takes any of the
other derived classes as an argument so that these statements are
valid:

D1 d1;
D2 d2;

D3 d3a(d1);
D3 d3b(d2);

What is the syntax that I have to use for those statements to compile.
I realize that, for any given derived class i, I can't use Di (const
Base& b) as the constructor.
Since that's the most obvious solution, why can't you?

Do i need to use virtual constructors?
No.

You need to define what these conversions should mean, and whether you
have an open-ended set of derived classes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 2 '07 #2
On May 2, 5:46 pm, nozyrev <drmapa...@gmail.comwrote:
Hi,
I apologize in advance if this is a very dumb question. I've been
struggling with this problem for some time: I have an abstract base
class called Base and n derived classes D1, D2, ....Dn. I would like
to have a constructor for each derived class that takes any of the
other derived classes as an argument so that these statements are
valid:

D1 d1;
D2 d2;

D3 d3a(d1);
D3 d3b(d2);

What is the syntax that I have to use for those statements to compile.
I realize that, for any given derived class i, I can't use Di (const
Base& b) as the constructor. Do i need to use virtual constructors? If
so, how?

Thanks for your help
Constructors initialize instances of a type. The 'relationship'
between the types is what governs how the system should be designed.
One does not ask "how can i make the above statements compile?" since
we don't know what the relationship is/are between D1 and D2, D1 and
D3 and the same goes with types D2 and D3, etc

For all we know D3 might be composed of a D1 member and derived from
type D2. Or the other way around. Maybe both D1 and D2 are members of
D3? D3 is derived from both D1 and D2? Perhaps D3 is derived from D2;
which is derived from D1; which is derived from Base?
May 2 '07 #3
On 5/2/07 2:46 PM, in article
11**********************@n59g2000hsh.googlegroups. com, "nozyrev"
<dr*******@gmail.comwrote:
I apologize in advance if this is a very dumb question. I've been
struggling with this problem for some time: I have an abstract base
class called Base and n derived classes D1, D2, ....Dn. I would like
to have a constructor for each derived class that takes any of the
other derived classes as an argument so that these statements are
valid:

D1 d1;
D2 d2;

D3 d3a(d1);
D3 d3b(d2);

What is the syntax that I have to use for those statements to compile.
I realize that, for any given derived class i, I can't use Di (const
Base& b) as the constructor. Do i need to use virtual constructors? If
so, how?
No, but a class template and some typedefs might help. Something along the
lines of:

class Base
{
public:
virtual ~Base() {}
virtual void f() = 0;
};

template <int I>
class D : public Base
{
public:
D() {}
D(const D& d ) {}

template <int I2>
D(const D<I2>& d) {}

// to do: add operator=

void f() {}
};

typedef D<1D1;
typedef D<2D2;
typedef D<3D3;

int main()
{
D1 d1;
D2 d2;

D3 d3a(d1);
D3 d3b(d2);
}

Greg

May 2 '07 #4
On May 2, 11:46 pm, nozyrev <drmapa...@gmail.comwrote:
I apologize in advance if this is a very dumb question. I've been
struggling with this problem for some time: I have an abstract base
class called Base and n derived classes D1, D2, ....Dn. I would like
to have a constructor for each derived class that takes any of the
other derived classes as an argument so that these statements are
valid:
D1 d1;
D2 d2;
D3 d3a(d1);
D3 d3b(d2);
What is the syntax that I have to use for those statements to compile.
I realize that, for any given derived class i, I can't use Di (const
Base& b) as the constructor.
You can, at least as far as the language rules are concerned.
Do i need to use virtual constructors?
There's no such thing.

The real problem isn't syntax: a constructor taking a reference
to the base class, or a templated constructor, both solve that
problem. The real problem is semantics. What does it mean to
construct a D3 object from a D2 or a D1? What happens to
information that is in D2 or D1, but isn't present in D3? How
do you initialize information that is present in a D3, but not
in a D1 or a D2? If all relevant information is present in the
base, and the derived classes just provide different means of
manipulating this information, a constructor from Base const& is
the obvious answer. If the necessary information is in the
derived class, but there are "standard" ways of accessing it,
and it is always present (or there is an appropriate default if
it isn't present), then a templated constructor is a solution.
If each pair Dn->Dm has distinctive semantics, there's not much
you can do but write n different constructors for each Dm.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 3 '07 #5

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

Similar topics

7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: zlf | last post by:
I am asked to complete a COM+ component, there is a class A derived from ServicedComponent. However, when executing , exception is thrown. Messaged: Unhandled Exception:...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
2
by: Ethan Strauss | last post by:
Hi, I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the...
6
by: Peng Yu | last post by:
Hi, I want B has all the constructors that A has. Obviously, the code below would not work. I could define a corresponding B's constructor for each A's constructor. But if A has many...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.