473,387 Members | 1,493 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.

Making dervied class from base class

I'm looking for the best way to solve the following problem:

public class Foo
{
public void Bar() {}
}

public class Alice : Foo
{
public void Zed() {}
}

Foo foo = new Foo();
What I want to do is to make a new instance of Alice using the data from my
instance of foo like so:

Alice alice = (Alice)foo;
or
Alice alice = new Alice(foo);

Right now, the best solution I have found is to implement a Copy method on
the base class which takes a base class instance and overwrites the local
instance's data with the passed instance's data (e.g. public void Copy(Foo
foo)).

Any better suggestions?

Tom
Jul 21 '05 #1
1 1079
If you have an instance of Foo, there is nothing you can do to promote it to
Alice. It's a Foo, not an Alice!

Now, if you had an instance of Alice, you could treat it as a Foo, since an
Alice is a Foo by definition. But not necessarily the other way around.

Now, if you add a constructor to Alice that takes a Foo, and then it copies
all pertinent information from Foo, then that will work. But you are pretty
much restricted to this type of copy, like in your method.

"Thomas C" <th****@newsgroup.nospam> wrote in message
news:%2*****************@TK2MSFTNGP12.phx.gbl...
I'm looking for the best way to solve the following problem:

public class Foo
{
public void Bar() {}
}

public class Alice : Foo
{
public void Zed() {}
}

Foo foo = new Foo();
What I want to do is to make a new instance of Alice using the data from my instance of foo like so:

Alice alice = (Alice)foo;
or
Alice alice = new Alice(foo);

Right now, the best solution I have found is to implement a Copy method on
the base class which takes a base class instance and overwrites the local
instance's data with the passed instance's data (e.g. public void Copy(Foo
foo)).

Any better suggestions?

Tom

Jul 21 '05 #2

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

Similar topics

8
by: Lars-Erik Aabech | last post by:
Hi! I've been looking around for an attribute that tells the intellitip thingy to skip some field or operation. Is there any? IE. a ListControl object has the DataSource property, but not all of...
3
by: John B | last post by:
I have a variable in a base class defined as: protected Control validationControl; How can I ensure that it is set is any dervied classes. I see that you cannot set variables to abstract. ...
2
by: Dennis | last post by:
I have a control that inheirts from "Panel". I want to prevent the user from having access to the MoueUp event. I tried making a sub in my control that handles MyBase.MouseUp but still this event...
1
by: Thomas C | last post by:
I'm looking for the best way to solve the following problem: public class Foo { public void Bar() {} } public class Alice : Foo { public void Zed() {}
1
by: amitwagh | last post by:
class BaseEx { public : virtual void Display() { cout<<"BaseEx:: Display()"<<endl; } }; class DervEx : publjc BaseEx
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
2
by: Clive Dixon | last post by:
Does anyone know of any way with VS 2005 source editor of navigating to class(es) derived from a particular class/interface, in the same way Reflector does?
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.