|
|
Greetings!
I know it's a long shot and please don't mock me if I missed it completely.
Suppose I get some config file called objects.txt. It includes lines like:
phonenum|1003
colour|red
vehicle|truck|mack
first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following:
if "I know-type" phonenum {
new phonenum(1003);
} else if "I know-type" colour{
new colour(red);
} else if "I know-type" vehicle{
new vehicle(truck, mack);
}
Is that even possible? To check the type at run-time?
Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem.
I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface
addObj(abstractObj obj*);
every user defined object would have to implement methods
char* serialize(void);
void deserialize(char*);
when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters.
I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example:
if (line == "phonenum"){
new phonenum(1002);
}
...
here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time.
Best regards,
Rostfrei
|
|
|
|
|
Will typeid help you?
Code sample from MSDN :
class Base {
public:
virtual void vvfunc() {}
};
class Derived : public Base {};
using namespace std;
int main()
{
Derived* pd = new Derived;
Base* pb = pd;
Base b;
cout << typeid( b ).name() << endl;
cout << typeid( pb ).name() << endl;
cout << typeid( pb ).name() << endl;
cout << typeid( pd ).name() << endl;
cout << typeid( pd ).name() << endl;
delete pd;
}
Nibu thomas
Software Developer
|
|
|
|
|
the problem here is in "new Derived;". When I would do that I would already have to know which derived class it is.
Rostfrei
|
|
|
|
|
yes this can be done using RTTI, what framework are you using?
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg
|
|
|
|
|
What do you mean by what framework am I using? You mean developement environment/language? I use VC++ .NET 2003 and also always port the code to gcc for ARM.
Rostfrei
|
|
|
|
|
How to lock a folder using MFC application
|
|
|
|
|
You cannot.
Nibu thomas
Software Developer
|
|
|
|
|
There are two simple method in xp sp2
method 1:
1. compresss the folder
2. set password
method 2:
1. right click the folder
2. click properties
3. click secutity tab
4. set permission
OR
there are lot of software to lock the folder. but it is not secure
JAYARAJ
|
|
|
|
|
Through MFC ?
Nibu thomas
Software Developer
|
|
|
|
|
No NOT in MFC
this future is in windows xp sp2
JAYARAJ
|
|
|
|
|
Hi,
I have a DLL which has a class containing static members. In the
implementation file of the class I have done proper initialization of
the static data. These static data are public thus I want to modify
these public static data from another application using this DLL.
Somehow the static initialization is not happening if it is done in the
implementation file and while accessing the storage for static data my
VC7 compiler complains that I am trying to write to an illegal memory.
But if I do the static initialization in the header file itself then I
get no run time error. As far as I know static data initialization
should not be done in header file. So really my question is why is this
happening in the first place and what I need to do to fix it?
DLL CODE:
///////////////////////////////Header
file//////////////////////////////////////////
typedef void (*fptr)(void);
class DllMethods
{
public:
__declspec(dllexport) static int data;
__declspec(dllexport) static fptr function;
};
/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////Implementation
file//////////////////////////////
#include "DllMethods.h"
//IF THIS INITIALIZATION IS DONE IN THE HEADER FILE FOR THE DLL
//THEN I GET NO ERROR WHEN DEBUGGING WITH VC7 COMPILER
fptr DllMethods::function = 0;
int DllMethods::data = 46;
///////////////////////////////////////////////////////////////////////////////////////
APPLICATION CODE:
#include "DllMethods.h"
#include
using namespace std;
static void anotherFunction(void)
{
cout << "Inside anotherFunction" << endl;
}
int main(int argc, char ** argv)
{
//I GET RUNTIME ERROR WHEN THE FOLLOWING LINE
//EXECUTES AND WHEN THE STATIC INITIALIZATION IS
//DONE IN THE IMPLEMENTATION FILE
//BUT IF THE INITIALIZATION IS DONE IN THE HEADER ITSELF
//THEN I GET NO ERROR
//ASSIGN THE FUNCTION PTR OF THE DLL TO THE FUNCTION
//IN THE APPLICAITON
DllMethods::function = anotherFunction;
DllMethods::function();
return 0;
}
PS: I had done this posting some days back as well but got no replies, thus reposting it. IF the question is not clear, then please drop a reply so that I can reform my question.
Thanks in advance for any suggestions /comments,
Divick
|
|
|
|
|
Divick wrote: class DllMethods
{
public:
__declspec(dllexport) static int data;
__declspec(dllexport) static fptr function;
};
class __declspec(dllexport) DllMethods
{
public:
static int data;
static fptr function;
};
This is to be done in the header file when compiling the DLL but when you use it replace the following __declspec(dllexport) with __declspec(dllimport) . It should look like this
class __declspec(dllimport) DllMethods
{
public:
static int data;
static fptr function;
};
A better option would be this
#ifdef INDLL
#define LIBSPEC __declspec(dllexport)
#else
#define LIBSPEC __declspec(dllimport)
#endif Add the above to your header file. In the DLL project define the macro INDLL hence LIBSPEC will expand to __declspec(dllexport) and outside the DLL don't define INDLL hence it will expand to __declspec(dllimport) .
class LIBSPEC DllMethods
{
public:
static int data;
static fptr function;
};
Nibu thomas
Software Developer
|
|
|
|
|
Hi Nibu,
that really works, but what really is the purpose of dllimport? I normally use dlls by exporting their functions and simply linking with their .lib files and including the header for the function declarations. And I do not do dllimport.
Thanks a lot for the reply,
Divick
|
|
|
|
|
If you don't do dllimport it won't work. Wondering how it works for you.
Nibu thomas
Software Developer
|
|
|
|
|
hello friends Thank you very much for spending your valuable time for finding solution to my problem. I tried the steps suggested by both of you & both are working . I was stucked into that problem from last 1-2 hrs.
Thank once again.
Shoaib.
patel.shoaib@gmail.com
|
|
|
|
|
Hi, All
i want to draw lines and text on a picturebox.
how can i do it, also i want to update the
lines on it's paint event, but problem is that
picturebox paint event is not present in class
wizrad,so how can i generate paint event and draw
lines/text in it?
Regards
Masood
|
|
|
|
|
Just add a WM_PAINT handler.
Nibu thomas
Software Developer
|
|
|
|
|
Thanks
but it will refresh the form and pictrebox, how can i
cause only the picturebox to be refreshed. Any idea
how to obtain drawing handle for picturebox, little code
will be helpful for me.
Regards
Masood
|
|
|
|
|
If you want to do stuff like that, subclass the picturebox and handle WM_PAINT . Don't write picture box painting code inside dialog WM_PAINT handler.
Nibu thomas
Software Developer
|
|
|
|
|
Add WM_PAINT and this function OnPaint
You can use handle dc(CPaint dc(this)
dc.line..
dc.textout
|
|
|
|
|
Thanks for providing code
would not it draw the line/text on form,
i want to draw on picturebox, is there any way to
generate paint event for picturebox as provided
in vb, c# etc.
Regards
Masood
|
|
|
|
|
Hi Engineer Masood,
I m not sure because i dont test this suggestion but
I think you can derived from one class that you need and then insert wm_paint in the derived class
|
|
|
|
|
Hi ,
What is the best way for working with hardware in VC++?
Please help me,
Thanks.
Every new thing you learn,Gives you a new personality.
|
|
|
|
|
i doubt that you will get any answers with this question, please post what are you trying to do, which hardware do you want to work with?
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg
|
|
|
|