473,385 Members | 2,210 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,385 software developers and data experts.

Derived class constructor

Hi,

I have some legacy code looks like this:

class Tool
{
public:
Tool(char* name);

virtual void do();
};

class Work
{
private:
Tool _tool;

public:
Work();

void run();
}

The Work.cc is defined as:

Work::Work()
:
_tool("Old Tool")
{
....
}

void Work::run(){
....
_tool.do();
....
}

And some other code is using class Work.

Now I have a NewTool class:

class NewTool : public Tool
{
public:
NewTool(char* name);

virtual void do()
{
my new do...
}
}

I would like to use my NewTool in Work. But some old code is not
compatible with NewTool. So I have to keep Work class as it is and I
deceided to define a NewWork class derived from Work. All I want is to
give "_tool" a NewTool object. I want to keep code change as minimum
as possible and prefer not to change Tool or Work. But if I have to
change them, it is OK too.
class NewWork : public Work
{
public:
NewWork();
}

I am wondering how I can write the constructor of NewWork so that is
has all function from Work but uses NewTool. The following code does
not work:

NewWork::NewWork
:
_tool(NewTool("New Tool"))
{
}
Can anyone help?

Thanks,

qq

May 12 '06 #1
6 1429
qu******@yahoo.com wrote:
Hi,

I have some legacy code looks like this:

class Tool
{
public:
Tool(char* name);

virtual void do();
};

class Work
{
private:
Tool _tool;

public:
Work();

void run();
}

The Work.cc is defined as:

Work::Work()
:
_tool("Old Tool")
{
....
}

void Work::run(){
....
_tool.do();
....
}

And some other code is using class Work.

Now I have a NewTool class:

class NewTool : public Tool
{
public:
NewTool(char* name);

virtual void do()
{
my new do...
}
}

I would like to use my NewTool in Work. But some old code is not
compatible with NewTool. So I have to keep Work class as it is and I
deceided to define a NewWork class derived from Work. All I want is to
give "_tool" a NewTool object. I want to keep code change as minimum
as possible and prefer not to change Tool or Work. But if I have to
change them, it is OK too.
class NewWork : public Work
{
public:
NewWork();
}

I am wondering how I can write the constructor of NewWork so that is
has all function from Work but uses NewTool. The following code does
not work:

NewWork::NewWork
:
_tool(NewTool("New Tool"))
{
}


Work has a Tool and nothing else. You can either 1) change Tool to
NewTool in Work or 2) still in Work, make Tool a Tool* and create a
NewTool on the heap. Whatever you do, you'll end up modifying class
Work.

It is suprising (and imo broken) that Tool has virtual functions but is
created as an automatic object.
Jonathan

May 12 '06 #2
you can use Class Templates to solve this problem

so you need to modefy Work class as :

template <class Type>
class Work {
private:
Type _tool;
public:
Work();
void run();
};

then use it like
Work<Tool> work1; // _tool is Tool object
Work<NewTool> work2; // _tool is NewTool object

so you don't need to write NweWork class again

Ahmed

May 12 '06 #3
Ahmed Samieh wrote:
you can use Class Templates to solve this problem

What problem? This makes no sense on its own, please quote context form
the message you are replying to.

--
Ian Collins.
May 12 '06 #4
you can use Class Templates to solve this problem

so you need to modefy Work class as :

template <class Type>
class Work {
private:
Type _tool;
public:
Work();
void run();
};

then use it like
Work<Tool> work1; // _tool is Tool object
Work<NewTool> work2; // _tool is NewTool object

so you don't need to write NweWork class again

Ahmed

May 12 '06 #5
Could you please give me some code?

Thanks,

qq

May 12 '06 #6

qu******@yahoo.com wrote:
Could you please give me some code?

Thanks,

qq

#include <iostream>
#include <stdlib.h>
using namespace std;

class OldTool {
protected:
string buffer;
public:
OldTool();
OldTool(char* name) : buffer(name){};
~OldTool(){};
virtual void proc();
};
void OldTool::proc() {
cout << buffer << "Old" << endl;
}
class NewTool : public OldTool {
public:
NewTool() : OldTool() {};
NewTool(char* name) : OldTool(name) {};
~NewTool() {};
virtual void proc();
};
void NewTool::proc() {
cout << buffer << "New" << endl;
}
template <class Type>
class Work {
private:
Type _tool;
public:
Work() : _tool("this tool is ") {};
~Work() {};
void run() {
_tool.proc();
}
};

int main(int argc, char *argv[]) {
Work<OldTool> x; // = Work
x.run();
Work<NewTool> y; // = NewWork
y.run();
system("PAUSE");
return 0;
}

now you don't need to write NewWork, the compiler will do it for you

Ahmed

May 12 '06 #7

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: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
10
by: William Stacey | last post by:
I know the following is not allowed, but shouldn't it be? sharedObject is part of Derived and should be able to be set in the constructor - no? tia public abstract class Base1 { protected...
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: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
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...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.