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

One Variable --> Different Data Types

Hello, I hope you can help me :)) The story goes as follows:
I have a class with different methods and member variables. I store
pointers to objects of this class inside a vector. Now, I would like to
have a variable 'window' which could contain pointers to different
objects (like 'class t1', 'class t2', etc.).

I tried to use templates to create variable 'T* window' but I had some
troubles later storing pointer to such a template-enabled class inside
my vector :(.

Do you think that I could store 'void *' pointers and then just use
'reinterpret_cast<type-id>(pointer)' to return to my 'class t1', 'class
t2', etc. types? Would it be safe?
I hope it's moreless clear what I want to do :))

Greetings,

TroLoo

Jul 22 '05 #1
4 3155
tr****@gmail.com wrote:
Hello, I hope you can help me :)) The story goes as follows:
I have a class with different methods and member variables. I store
pointers to objects of this class inside a vector. Now, I would like to
have a variable 'window' which could contain pointers to different
objects (like 'class t1', 'class t2', etc.).

I tried to use templates to create variable 'T* window' but I had some
troubles later storing pointer to such a template-enabled class inside
my vector :(.

Do you think that I could store 'void *' pointers and then just use
'reinterpret_cast<type-id>(pointer)' to return to my 'class t1', 'class
t2', etc. types? Would it be safe?
I hope it's moreless clear what I want to do :))


It's moreless clear that you've not designed it verywell. Why do you
think you need to keep those pointers to [unrelated?] types in that
object [window]? If they are later to be worked on by that object,
then perhaps they should have a common base class and be polymorphic
(just like many OO things are). If that object ('window') only works
as temporary storage for them and somebody else is going to work on
those objects, you could, of course use void*. But that, again, is
a definite violation of the idea of design. Why does the "window"
store the pointers? Is there an association between the "window" and
the object? If there is, it probably can be expressed in terms of
some interface, and you get your base class. If there is no known
association, why is the a "window" used to keep that pointer? Maybe
you should keep that pointer elsewhere, with somebody who knows how to
use that pointer?...

Of course, all those things cannot be simply answered in terms of C++.
If you just need a plain answer, then here it is: use void* and
reinterpret_cast it back to the object when you need to dereference it.

V
Jul 22 '05 #2
Well, I have to say that I thought about polymorphism. Maybe I will
explain exactly why do I need such a variable. I'm working on a program
which is a MFC Application. I didn't choose this technique and didn't
have any opportunity to influence this choice.

Now, I have this nice window which is containing several Dialogs. So I
created a class which contains the pointer to a Dialog. I store all
those classes in a vector. Sometimes I need to reposition or do
anything with, for instance, 'dialog no 3' and I need a handle to this
dialog. I call 'my_vector[3]->window' and I have Dialog handle which is
moreless exactly the same as the rest of Dialog handles (differences
are non-relevant to me) but it represents different data type.

I'm sorry for this mess but english is not my mother language. It's
hard for me to write clearly what's my problem :(( I hope you can
understand that.

Greetings,

TroLoo

Jul 22 '05 #3
tr****@gmail.com wrote:
Well, I have to say that I thought about polymorphism.
And what result did you arrive at?
Maybe I will
explain exactly why do I need such a variable. I'm working on a program
which is a MFC Application. I didn't choose this technique and didn't
have any opportunity to influence this choice.

Now, I have this nice window which is containing several Dialogs. So I
created a class which contains the pointer to a Dialog. I store all
those classes in a vector. Sometimes I need to reposition or do
anything with, for instance, 'dialog no 3' and I need a handle to this
dialog. I call 'my_vector[3]->window' and I have Dialog handle which is
moreless exactly the same as the rest of Dialog handles (differences
are non-relevant to me) but it represents different data type.


Why do you say that it represents a different data type? It's a handle
to some OS-specific structure. How do you use it? You send a message
to that window using OS-specific method, no?

In MFC and many other UI frameworks, dialogs are windows, and classes that
define (describe, represent) dialogs usually derive from a common base
class that defines (describes, represents) a window. That way, if you
need to do something window-specific, you just tell the object to do that
using a member of the window class. Like reposition, show/hide, resize,
refresh, et cetera.

So, you have a main window that does something to those dialogs. What
does it do to them? Is it the same operation for all of them? Is it
different for each of them? If it's the same set of operations, collect
those operations in a class and make that class common base class for
all your 'dialog' classes. If it's different for each 'dialog' type,
what business do they have being in the same window? See my point?

An example of a common base class in MFC is CToolBar. You can either
instantiate it (it's a concrete class) or you could derive from it and
make your own, customized "toolbar". Then you can have both "native"
toolbars and your new, customized, one in the same main frame window.
All the operations you can perform on a regular toolbar is OK for your
custom one too because it "is-a" CToolBar, it derives from one.

I don't want to continue this discussion here because it's not really
relevant to C++. It's all essentially about knowing your library (MFC
in your example) well and designing your classes correctly using that
framework. You definitely need more time on that.

First of all, figure out what is common between those "different" data
types. Perhaps not that little. If they have a common class, store
a pointer to that. Use those pointers because that's the only way you
will get to the common functionality of those classes. If you can't
_find_ anything in common between them, you probably didn't put it in,
since there _must_ be something in common. So, revisit the hierarchy
of classes you have, and _give_ all those "different data types" the
common base class that will contain the functionality they all need to
implement.

V
Jul 22 '05 #4
Ok, I found a solution thanks to your advices. I keep pointers to base
classes and it is enough for me.
Thank you very much for you time!!!

--
Greetings,

TroLoo

Jul 22 '05 #5

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

Similar topics

1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
2
by: Mitch Mooney | last post by:
Subject line says it all. For example: //base class class foo{ public: foo(); ~foo(); virtual ??? GetValue()=0; };
7
by: smith4894 | last post by:
Hello, I have a question regarding storage locations for different data types. For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} )...
1
by: John Smith | last post by:
How do I use two different data types with a conditional operator ? I want to cout either a character or an integer depending on a certain condition. cout << ((IsThisTrue? char:integer) The...
2
by: Schnogge | last post by:
Hi! it is possible to generate an multiple-dimensional array with different data types? Or is it possible to combine a one-dimensional array with an other which has an other data type? How...
6
by: RaulAbHK | last post by:
Dear all, I guess this is a basic question with an easy answer but I am a beginner and I would much apreciate your feedbacks. Let's say I have a library with some functionality to perform some...
4
by: WuJianWei | last post by:
Is there a way(function) to distingusih between different data types? if u can, provide me with the code... thank u...
4
by: herath | last post by:
Is there a way to join 2 tables where the 2 fields on which the tables joined are of different data types(char and varchar)?I tried with CONVERT but is does not give the desired output. My...
12
by: jlgraham | last post by:
I have an assignment that wants me to write an application that takes 5 numbers from a user. As the user inputs the numbers I have to make them into a double, float, long, int and byte. All I need...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
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
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, you’ll 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.