C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

derived class init

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri May 12, 2006 12:21 pm    Post subject: derived class init Reply with quote



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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Manfred von Willich
Guest





PostPosted: Sat May 13, 2006 11:21 am    Post subject: Re: derived class init Reply with quote



You are creating class NewWork, which contains class Work (as a base
class), which contains a member of class Tool. You cannot initialise a
member of a base class in the initializer list of the derived class -
Work's constructor will already have done this before the initializers
run.

Quote:
From your description, you want to be able to substitute an actual
NewTool for Tool (not merely initialize an old Tool from a NewTool),

which means that some modification is needed to Work. I suggest
replacing the member Tool object with a reference, and modifying the
constructor of Work to allow setting of this.

Try:

class Work
{
private:
Tool & _tool;
protected:
Work(Tool * ptool); // same as other constructor, except
initializer: _tool(&ptool)
public:
Work(); // in definition, add initializer: _tool(*new Tool)
virtual ~Work(); // with body: { delete &_tool; }
void run();
}

NewWork::NewWork
:
Work(new NewTool("New Tool"))
{

}

Because you are dealing with different complete classes Tool and
NewTool, it becomes difficult to avoid using the heap. Note that ~Work
does the deletion irrespective of where the allocation occurred. If
NewTool has a non-trivial destructor (even if it is
compiler-generated), Tool must be modified to have a virtual destructor
(I would do this for safety anyway). I have made Work::~Work virtual
for safety reasons too, though you may find this unnecessary, if you
never have a pointer "new NewWork" deleted as a pointer to Work or
NewWork's destructor is trivial. For safety I recommend a virtual
destructor for EVERY class used as a base class for which the
derivation is not strictly controlled.

I haven't checked this on a compiler, but it should give you the idea.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.