473,405 Members | 2,185 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,405 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 3160
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...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

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.