473,241 Members | 1,563 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,241 software developers and data experts.

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 1641

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
7
by: Göran Tänzer | last post by:
Hi, i've written a class which does some calculations for my web application. These informatinos are different for each page request - the current user is not important. i have about 10 aspx...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
5
by: Diffident | last post by:
Hello All, I am designing a class based on singleton pattern. Inside this class I have multiple instance methods. My question is since there will be only one instance of this class at any...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.