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

Plugins in the class

I'm trying to do something like that: define class A, which contains a
handle to the object O of class B and calls this object's member
functions. I'd like the object O to be sort of a "plugin", so that
instead of class B a class inherited from it can be substituted, that
the object O can be swapped for a different one during the lifetime of
the object of class A. So far, I've implemented it like that:

class A {
private:
B * ptrB_;
}

and added the usual set of get/set routines (they return/accept a const
B& reference), Bptr_ is set to NULL in the constructor of A. I am not
satisfied with this solution, for obvious reasons. But I can't come
with anything better which would allow so much flexibility. Any
suggestions?

Jan 9 '06 #1
4 1235

"Roman Werpachowski" <ro****************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I'm trying to do something like that: define class A, which contains a
handle to the object O of class B and calls this object's member
functions. I'd like the object O to be sort of a "plugin", so that
instead of class B a class inherited from it can be substituted, that
the object O can be swapped for a different one during the lifetime of
the object of class A. So far, I've implemented it like that:

class A {
private:
B * ptrB_;
}

and added the usual set of get/set routines (they return/accept a const
B& reference), Bptr_ is set to NULL in the constructor of A. I am not
satisfied with this solution, for obvious reasons. But I can't come
with anything better which would allow so much flexibility. Any
suggestions?


That looks like a pretty standard way to handle it to me. Just be sure that
B has a virtual destructor. (By the way, private is default for class
members, so you don't really need to specify it here - unless it's preceded
by protected or public specifiers of course.)

-Howard

Jan 9 '06 #2
I was not satisfied by the solution because someone might supply A with
a reference to local variable, which would be destructed somewhere
later and leave A with a "dangling pointer". Or am I missing something?

I like to leave such specifiers like private: for clarity. So that I
don't have to remember what is default.

Jan 9 '06 #3
Roman Werpachowski wrote:
I was not satisfied by the solution because someone might supply A with
a reference to local variable, which would be destructed somewhere
later and leave A with a "dangling pointer". Or am I missing something?

Passing automatic (local) objects to be held by non-local objects is
not a good idea. For your situation, there are 2 options that I can
think of:

- make a copy of B (if it is not expensive)
- take a shared_ptr to B (see boost::shared_ptr, for example), which
means users need to allocate the object (using 'new' or some
allocator), i.e. it should not be automatic.

Hope this helps,
-shez-

Jan 10 '06 #4
First is not expensive, but unwanted: I want to exploit polymorphism,
i.e. be able to subsitute

class C: public B;

for B.

I've switched to shared_ptr.

Jan 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: censored | last post by:
Hi. I'm hoping for a bit of programming guidance here .... I have an application I wrote (it's an irc bot fyi), with which I want to use plugins. Basically, I want there to be directory called...
4
by: stu_pb | last post by:
I am designing a plugin system for a window application using .NET(C# specifically). One of the requirements of the plugin system is to be able to dynamically load/unload plugins. My initial...
5
by: ThunderMusic | last post by:
Hi, I have some code to load some plug-ins, but the code requires me to know the name of the class to load (here: SamplePlugin, derived from IPlugin) : (Here is some C# code, but I use VB.Net to...
5
by: Christoph Haas | last post by:
Dear coders... I'm working on an application that is supposed to support "plugins". The idea is to use the plugins as packages like this: Plugins/ __init__.py Plugin1.py Plugin2.py...
5
by: Michael McCarthy | last post by:
I want to develop plugin support for a system.montitor module I am working on. A lot of the modules will do mostly interop stuff for an older system, but I want to use it myself as well to monitor...
6
by: Pete Davis | last post by:
I'm confused about what precisely the limitations are on loading plugins in separate app domains. In all my previous apps that supported plugins, I've loaded them into the same domain as the app,...
0
by: Greg Conely via .NET 247 | last post by:
I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin,...
6
by: Luis Arvayo | last post by:
I need to implement plugins in my application. That is to say, DLLs that communicate with my application data. Does someone have a link to the best explain of how to accomplish the above ? ...
0
by: sleem | last post by:
Hi. I'm hacking up an irc bot in python using Joel Rosdahl's irclib. I want to be able to add plugins to my bot, without having to restart it. In specific, I would have a directory called...
6
by: Dave Challis | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm trying to write some code which: 1. Finds all modules in a plugin directory 2. Imports those modules 3. Creates an instance of each object...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.