|
you are right and thanks for notation. The first one was my blunder and i corrected it. anyway have got any idea about my problem?
|
|
|
|
|
why not just initialize them ?
|
|
|
|
|
This class does the computations of a 2d ellipsoid, modelling the earth and has lots of variables which are very likely not being initialized. so I thought I would throw the exception whenever i call a function which requires one of the parameters not being initialized yet. have got any comment?
|
|
|
|
|
How would you differentiate between an initialized variable vs. a variable having an invalid value?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
the is no concept of "uninitialized" built into C++. either you initialize the variables yourself or you live with the effects of random garbage in your program.
just initialize the variables.
|
|
|
|
|
shahin namini wrote: if(class_variable==NULL)
//throw the exception
If the exception is thrown, that means that class_variable was initialized (to NULL ).
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
don't care for words. Whatever you call it, I am seeking for the answer!
|
|
|
|
|
Hello,
If I need to use a function in a C library that I can't modify, let's say:
extern "C"
{
void register_callback(void (*callback) (void));
}
... if I also have a class like this:
class MyClass
{
int m_counter;
public:
MyClass() : m_counter(0) {}
void OnEvent()
{
m_counter++;
}
}
Is there any solucion to use MyClass::OnEvent with the funcion register_callback()? In other words, I would like to do something like this (Of course this doesn't work):
int main (void)
{
MyClass my;
register_callback(&(my.MyClass::OnEvent))
}
It's very easy to make MyClass a singleton and define MyClass::OnEvent() as 'static' or use a singleton inside MyClass, which is enough in almost all the cases but I don't want to modify MyClass at all.
I've tried this one:
int main (void)
{
register_callback(sigc::mem_fun(*this,&MyClass::OnEvent));
...
}
That doesn't work. One of the problems is that sig::mem_fun() return an object created in the stack and the parameter for register_callback must be a 'const' one.
Thank you in advance,
Miguel
|
|
|
|
|
You can't do this cleanly in C++. I.e. member functions cannot be used where static functions are specified. If you want to hack things and possibly add some cryptic assembler, then it's possible. But that does violate the constraints of the language. There are some articles here on CP that demonstrate this kind of hack if you want to delve into it.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
VCF Blog
|
|
|
|
|
Thank you Jim, you're right. I have read a lot about this and I haven't find anything useful.
|
|
|
|
|
the callback function doesn't take any parameters ? that sucks.
for this very reason, whenever we do a callback hook in one of our libraries, we specify our that the callback function must take at least one parameter large enough to hold a pointer. for example:
typedef BOOL (CALLBACK* IS40_SourceCloseFn)(const IS_CALLBACK_DATA uUserInfo);
you can pass a 32-bit value into the 'register callback' function and all the callback calls will pass that parameter back to you. so, you use an object pointer as the 32-bit value, the lib passes that value to the callback function, and you cast it back to the object type in the callback.
otherwise, you're stuck using global variables in the callback (and tricky synchronization schemes to make sure only one call is in there at a time).
|
|
|
|
|
The callback function and MyClass can't be modified. I've make it up so in order to focus the problem.
This is the code that you can't modify:
extern "C"<br />
{<br />
void register_callback(void (*callback) (void)); <br />
}
... if I also have a class like this:
class MyClass<br />
{<br />
int m_counter;<br />
public:<br />
MyClass() : m_counter(0) {}<br />
void OnEvent() <br />
{<br />
m_counter++;<br />
}<br />
}
Thanks
|
|
|
|
|
elmendavies wrote: extern "C"
{
void register_callback(void (*callback) (void));
}
Is this the signature of the real callback function? If it takes a context argument (generally void* ) then the solution is simple and well known.
Steve
|
|
|
|
|
I've make it up. It's not a real library function but it could be. Remember for example the signal function in UNIX and the definition of its callback:
http://www.hmug.org/man/3/signal.php
How to solve it?
|
|
|
|
|
If sizeof(int) is big enough to hold a pointer then something like the following will work.
extern "C"
{
void register_callback(void (*callback)(int), int arg);
}
class MyClass
{
int m_counter;
public: MyClass() : m_counter(0) {}
void OnEvent()
{
m_counter++;
}
}
void MyClassOnEventThunk(int arg)
{
MyClass *pInstance = reinterpret_cast<MyClass*>(arg);
pInstance->OnEvent(pInstance);
}
MyClass g_inst;
register_callback(&MyClassOnEventThunk, reinterpret_cast<int>(&g_inst));
If sizeof(int) is ***NOT*** big enough to hold a pointer you will have to maintain a mapping yourself.
Steve
|
|
|
|
|
my project is for grapic in 3 views, when closed program (visual c++ .net 2005, MFC) appear message error.
My project It is called stressinv.
The main window divided into 2 views within one creates two views. Grafico "OnDraw" at each hearing.
At the start of each hearing, uses "CSpritterWnd."
for more details please, written me to satw14@gmail.com.
sorry for my english, but speak spanish.
-------------------------------------------------------
Excepcion no controlada en 0x7821036a (mfc80d.dll) en stressinv.exe:
0xC0000005: Infraccion de acceso al leer la ubicacion Oxfeeefeee.
|
|
|
|
|
satw14 wrote: Excepcion no controlada...
An unhandled exception? So what are you doing when the application exits?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
//-MainFrm.cpp
Removed roles created by default and inclusions classes (include).
Here I try to create a view or two panels in the main view.
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
m_wndSplitter=NULL;
}
CMainFrame::~CMainFrame()
{
if (m_wndSplitter != NULL) delete m_wndSplitter;
}
//--Cleft and AreaView These classes are now aggregated
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs,
CCreateContext* pContext)
{
m_wndSplitter=new CSplitterWnd();
if(!m_wndSplitter->CreateStatic(this,1,2))return false;
if(!m_wndSplitter->CreateView(0,0,RUNTIME_CLASS(CLeft),CSize(200,200),pContext))return false;
if(!m_wndSplitter->CreateView(0,1,RUNTIME_CLASS(AreaView),CSize(100,100),pContext))return false;
return TRUE;
}
// CMainFrame diagnostics
// when When I finished the program sends the mistake and put me here
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid(); //here
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
thanks David
-- modified at 18:24 Wednesday 17th October, 2007
|
|
|
|
|
satw14 wrote: m_wndSplitter=new CSplitterWnd();
Why are you not just using a stack-based member variable, rather than unnecessarily getting the memory manager involved?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
satw14 wrote: 0xC0000005: Infraccion de acceso al leer la ubicacion Oxfeeefeee.
Access Violation while trying to access a freed object? The 0xFEEEFEEE is often seen in things like data members of previously deallocated objects (debug).
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Hi
i hav converted an integer to a string using the following method,
CString str;
int i;
char TempBuffer[50];
_itoa(i,TempBuffer,10)
str=TempBuffer;
Now how can i convert a Float to a string .
any can please tell mi the solution(easy if the soln is similar to the above)
thanks
|
|
|
|
|
itoa is only between int and char.
if you want float or double try dtostr / strtod
And still better, if you are using CString, then use CString::Format
CString szValue = "";
szValue.Format (_T("%f"), doubleVariable);
This works with hexadecimal, int, binary... take a look in documentation.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
CString t;
t.Format("%f", floatVal);
|
|
|
|
|
the result from google search "c++ float to string" give this example...
int main()<br />
{<br />
char* str = new char[30];<br />
float flt = 2.4567F;<br />
<br />
sprintf(str, "%.4g", flt ); <br />
<br />
cout<<str<<endl;<br />
return 0;<br />
}
[Insert Witty Sig Here]
|
|
|
|
|
Please tell us the site where you got that from so that it can be blacklisted! Or at least the original author so other developers here can watch out for their name on a resume...
It is stupid enough to use new to allocate 30 bytes that are never used beyond the scope of allocation. It is also pretty dumb not to delete[] them. It is beyond my understanding why someone would make such code public. I mean, come on, at least show stuff like this to your friends first...
Oh, and the %g specifier is technically for double s, not float s. And mixes C and C++ style output (could format using the stream), etc...
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|