468,768 Members | 1,712 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

How can I instance such a class?

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


Feb 10 '06 #1
4 1269

key9 wrote:
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...

Feb 10 '06 #2
ro**********@gmail.com wrote:
key9 wrote:
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.


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-lit....html#faq-10.6

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());


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".

//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; }


Prefer RAII with smart pointers over manually deleting.

Cheers! --M

Feb 10 '06 #3
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....d0d7f5d2dd6126

Cheers! --M

Feb 10 '06 #4
Great thanks to mlimber. that helps me a lot .^_^
key9
Feb 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Robert Ferrell | last post: by
18 posts views Thread by John M. Gabriele | last post: by
7 posts views Thread by Göran Tänzer | last post: by
45 posts views Thread by =?Utf-8?B?QmV0aA==?= | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.