How can I instance such a class?

K

key9

Hi All
I 've got no idea how to describe it on subject.
See code:

//ABC
class Keyword{}; // user's command
class Handler{}; // command's action


// A Command contain A Keyword* + A Handler*

class Command{
public:
....
private:
Keyword* kwd_i;
Handler* hnd_i;
};


//...somewhere on code ...

//... here, a real needed keyword & handler class defined ...

class KeywordSon : public Keyword{};
class HandlerSon : public Handler{};


//...ok what I need is a Command ...
//... so what 's the way I can construct Command like as follows?

/*pseudo code*/
Command* cmd_ = new Command(KeywordSon(),HandlerSon());
/*/pseudo code*/

***************
***************

//I don't want code like this

//load
KeywordSon* kwd_;
HandlerSon* hnd_;
Command* cmd_ = new Command(kwd_,hnd_);
//unload
delete cmd_;
delete kwd_;
delete hnd_;


// Some one told me a factory class can solve the problem
// but what about class "KeywordSon" have 10000 types? how I can control
them on code? They are not certain.
// and I can not write a function on a Class.
// so I think , control them by the class name is the way.

//and also I want when I write
delete cmd_;
//all the memory occupied by line "Command* cmd_ = new
Command(KeywordSon(),HandlerSon());"
//can be freed automatic.

Is that possible?

Thany you very much, sorry for my poor English & c++ experience.


key9
 
R

roberts.noah

key9 said:
Hi All
I 've got no idea how to describe it on subject.
See code:

//ABC
class Keyword{}; // user's command
class Handler{}; // command's action


// A Command contain A Keyword* + A Handler*

class Command{
public:
...
private:
Keyword* kwd_i;
Handler* hnd_i;
};

Command::Command(Keyword * k, Handler * h) : kwd_i(k), hnd_i(h) {}

You will also need non-trivial destructor, copy constructor, and
assignment op. Document in header that constructor takes ownership of
the pointer.

Option 2:

Command::Command(const Keyword & k, const Handler & h)
{
kwd_i = k.clone(); // Your polymorphic types need a virtual clone()
function that creates itself (using new) and returns the top level
super as pointer.
hnd_i = h.clone();
}

The clone method is a factory function.
//...somewhere on code ...

//... here, a real needed keyword & handler class defined ...

class KeywordSon : public Keyword{};
class HandlerSon : public Handler{};


//...ok what I need is a Command ...
//... so what 's the way I can construct Command like as follows?

/*pseudo code*/
Command* cmd_ = new Command(KeywordSon(),HandlerSon());

Command * cmd = new Command(new KeywordSon(), new HandlerSon());

Option 2 works like your "pseudo code".

//and also I want when I write
delete cmd_;
//all the memory occupied by line "Command* cmd_ = new
Command(KeywordSon(),HandlerSon());"
//can be freed automatic.

Command::~Command() { delete kwd_i; delete hnd_i; }
Is that possible?

Standard shit...
 
M

mlimber

Command::Command(Keyword * k, Handler * h) : kwd_i(k), hnd_i(h) {}

You will also need non-trivial destructor, copy constructor, and
assignment op. Document in header that constructor takes ownership of
the pointer.

Or, signify that the function takes ownership by using smart pointers,
e.g.,

Command::Command( std::auto_ptr<Keyword> k,
std::auto_ptr<Handler> h);

The copy constructor should be explicitly disabled if you make kwd_i
and hnd_i std::auto_ptrs too, but needn't be if you make them
boost::scoped_ptrs.
Option 2:

Command::Command(const Keyword & k, const Handler & h)
{
kwd_i = k.clone(); // Your polymorphic types need a virtual clone()
function that creates itself (using new) and returns the top level
super as pointer.
hnd_i = h.clone();
}

Prefer initialization lists:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
The clone method is a factory function.

Command * cmd = new Command(new KeywordSon(), new HandlerSon());

Note that the OP's code is potentially dangerous, depending on if the
constructor grabs a pointer/reference to them or copies them since the
object created by KeywordSon() and HandlerSon() will be destroyed
immediately after the statement completes, which could leave the
pointer/reference hanging.
Option 2 works like your "pseudo code".



Command::~Command() { delete kwd_i; delete hnd_i; }

Prefer RAII with smart pointers over manually deleting.

Cheers! --M
 
M

mlimber

key9 wrote:
[snip]
// Some one told me a factory class can solve the problem
// but what about class "KeywordSon" have 10000 types? how I can control
them on code? They are not certain.
// and I can not write a function on a Class.
// so I think , control them by the class name is the way.
[snip]

You could use a templatized factory like the one from the Loki library
and _Modern C++ Design_. See this post for an example and more info:

http://groups.google.com/group/comp.lang.c++/msg/e8d0d7f5d2dd6126

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top