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

static class ...err.. pointer

i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

....is this possible, or i'm i short on reading?

Jan 17 '07 #1
5 2751

evaristobo wrote:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()
Since static methods apply to the class as a whole rather than specific
instances of it, then you could achieve the above more simply with:

classStatic.staticMethod();

There's no need to have an instance of it.

Jan 17 '07 #2
On 17 Jan 2007 06:18:47 -0800, "evaristobo" <ne***********@gmail.com>
wrote:
>i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

...is this possible, or i'm i short on reading?
So you want to index different types in order to access one
identically named static method on one of them?

It's not as easy as storing multiple objects, but it's certainly
possible using a mechanism known as "reflection".

Variant 1. Store the class name (e.g. "classStatic") in your
hashtable, and then use reflection to get the TypeInfo object for the
class, then the MethodInfo object for your method, then invoke the
method. That works but it's cumbersome and slow to execute.

Variant 2. Same as 1, but when adding a new element to your hashtable
you immediately determine the TypeInfo and store that instead of the
class name. Even better, if you always call just one method you
determine its MethodInfo object and store that.

Variant 3. Same as 2, but you declare a delegate that matches your
staticMethod, and when adding a new hashtable element you store a
delegate object for the new type's method, not a MethodInfo object.

Variant 4. Same as 3, but you avoid reflection altogether by having
the static classes add themselves to the hashtable. They can
immediately create a delegate for one of their own methods, without
having to use reflection and run-time name lookups. Big disadvantage
here: each of the static classes must have an "adder" method, or else
some central "adder" method has to have all static classes hardcoded.
--
http://www.kynosarges.de
Jan 17 '07 #3
....thank you very much Chris and Bobbo, but i was trying to escape
Reflection here, you see, i'm trying to design a registry object, which
is nothing more than an hashtable for the client code to query it for
objects who will follow a strict naming convention, this convention is
drawn from what i want the application framework to be, or better, to
supply. As an example, all application will have to supply a 'logger'
object, a 'dbConnection' object, a 'threadManager' object, and so on.
And then i want to get the 'extension.functionalityX' plugin which is
loaded from the plugins folder and implements MyFramework.IPlugin
interface...what do you think about this, how do you design your app
frame?

On Jan 17, 3:03 pm, Chris Nahr <dioge...@kynosarges.dewrote:
On 17 Jan 2007 06:18:47 -0800, "evaristobo" <newsletter...@gmail.com>
wrote:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()
...is this possible, or i'm i short on reading?So you want to index different types in order to access one
identically named static method on one of them?

It's not as easy as storing multiple objects, but it's certainly
possible using a mechanism known as "reflection".

Variant 1. Store the class name (e.g. "classStatic") in your
hashtable, and then use reflection to get the TypeInfo object for the
class, then the MethodInfo object for your method, then invoke the
method. That works but it's cumbersome and slow to execute.

Variant 2. Same as 1, but when adding a new element to your hashtable
you immediately determine the TypeInfo and store that instead of the
class name. Even better, if you always call just one method you
determine its MethodInfo object and store that.

Variant 3. Same as 2, but you declare a delegate that matches your
staticMethod, and when adding a new hashtable element you store a
delegate object for the new type's method, not a MethodInfo object.

Variant 4. Same as 3, but you avoid reflection altogether by having
the static classes add themselves to the hashtable. They can
immediately create a delegate for one of their own methods, without
having to use reflection and run-time name lookups. Big disadvantage
here: each of the static classes must have an "adder" method, or else
some central "adder" method has to have all static classes hardcoded.
--http://www.kynosarges.de
Jan 17 '07 #4

evaristobo wrote:
i'm a bit confused here, a pointer is something aiming to an object,
but a static class is not an object, nevertheless i would like to have
the possibility of doing something like handling an HashTable and
getting from it a pointer to a static class :
( (classStatic)myHash("classStatic") ).staticMethod()

...is this possible, or i'm i short on reading?
Whenever this happens to me, I resort to singletons. Or, possibly,
sort-of-singletons that can participate in inheritance.

The other situation that causes me to think "singleton" is wanting a
static class that I can pass to a method as an argument, again because
I want polymorphism.

If you make your static class a singleton that implements an interface,
say IMyInterface, then you can create a hash table of IMyInterface
objects. The fact that the class is a singleton means that there can
only ever be one of them, and it's accessible from the static space,
viz:

ClassSingleton.Instance.InstanceMethod();

Jan 17 '07 #5
On Jan 17, 7:52 pm, "Bruce Wood" <brucew...@canada.comwrote:
....
If you make your static class a singleton that implements an interface,
say IMyInterface, then you can create a hash table of IMyInterface
objects. The fact that the class is a singleton means that there can
only ever be one of them, and it's accessible from the static space,
viz:

ClassSingleton.Instance.InstanceMethod();
.....that's it! that's my solution for this problem, i have an
application registry as a Registry class, itself a singleton too,
implementing an interface IRegistry, for example. Then i have a
Singleton singleton class accessible at the startup procedure when the
core framework is putting its act together, then i register my
singleton like
Registry.register("mysingleton",Singleton.getInsta nce());
then all i have to do is supply a reference to Registry for whichever
object needing to use it by the IRegistry interface, which must be
located in a public assembly, well, but that's another story...

I can thank you enough
best regards
joao viegas

Feb 7 '07 #6

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

Similar topics

5
by: Surya Kiran | last post by:
Hi all, I've 2 classes class A, and class B. and i want to use list of type B in class A. like this list<B> typeB ; till now its fine. But i want to make it a static member. like static...
2
by: Brandon | last post by:
In a templated class I wrote, there is a public typedef for a function pointer. The class has an instance of the function pointer as a private member. That pointer is declared as static so I need...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
5
by: Boris | last post by:
I've a class C with a smart pointer (I use boost::shared_ptr) which is initialized in the constructor: class C { boost::shared_ptr<D> d; public: C() : d(new d()) { } }; When the program...
4
by: Dan | last post by:
I have class B and C which inherit from class A. I have a static method: A* aRequest(unsigned char *byte_buffer, size_t length) { A *foo; if(something == true) { foo = new B;
15
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
3
by: Bryan Parkoff | last post by:
The local variables and local functions are inside class body. You define a variable to the class "Reg reg;" in the main function. The reg variable has a pointer. The pointer gives memory...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.