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(Keyword Son(),HandlerSo n());
/*/pseudo code*/
***************
***************
//I don't want code like this
//load
KeywordSon* kwd_;
HandlerSon* hnd_;
Command* cmd_ = new Command(kwd_,hn d_);
//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(Keyword Son(),HandlerSo n());"
//can be freed automatic.
Is that possible?
Thany you very much, sorry for my poor English & c++ experience.
key9 4 1657
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::Comman d(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::Comman d(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(Keyword Son(),HandlerSo n());
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(Keyword Son(),HandlerSo n());" //can be freed automatic.
Command::~Comma nd() { delete kwd_i; delete hnd_i; } Is that possible?
Standard shit... ro**********@gm ail.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::Comman d(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::Comman d( std::auto_ptr<K eyword> k,
std::auto_ptr<H andler> 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_p trs. Option 2:
Command::Comman d(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(Keyword Son(),HandlerSo n()); 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(Keyword Son(),HandlerSo n());" //can be freed automatic.
Command::~Comma nd() { delete kwd_i; delete hnd_i; }
Prefer RAII with smart pointers over manually deleting.
Cheers! --M
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
Great thanks to mlimber. that helps me a lot .^_^
key9 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 confused.
I'd like to have a factory class that takes a string argument and returns
the appropriate factory method based on that string. I'd like the
instances to be callable. Like this:
fact = Factory('SomeThing')
aSomeThing = fact(some...
|
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 instance!
|
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 know the name as a string. This what I
tried:
class A:
|
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
|
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
guess I'll try to narrow it down to a few specific questions, but
any further input offered on the subject is greatly appreciated:
1. Are all of my class's methods supposed to take 'self' as their
first arg?
2. Am I then supposed to call...
| |
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 pages and 20 ascx user controls. In most of these
pages/user controls i need the informations of this class.
First i created an instance of this class in every page/user control i
|
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 that needs the class module I get an error on
web site:
Object reference not set to an instance of an object
Here is where the error is:
|
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 instance of time in the whole application there
is no use in having these methods as instance methods I can as well have
static methods....correct?
If I leave these methods as instance methods would they have any wrong
impact on my application?
|
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 only ever ONE instantiation of
this class, and this outside method in a separate thread has to access
it.
How do i do this?
|
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 clsData
Private m_objSQLClient As clsSQLClient
Private m_objUsers As clsUsers
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |