|
George_George wrote: code like this.
Not sure if some text is missing in your post??
George_George wrote: what is the life cycle of the exception object which reference variable e binded to?
You can try this with the debugger to see what address you catch!
std::exception e;
void Test()
{
try {
throw std::exception();
}
catch(std::exception& e) {
__asm int 3
}
}
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
1.
Maxwell Chen wrote: Not sure if some text is missing in your post??
I posted below in my question. Just the simple catch bracket, have found it?
2. What does int 3 mean?
3.
Your sample code looks confusing since you use both variable e (the same variable name for local catch block and the global variable).
My question is, is it ok to pass e outside of the catch bracket, like assigning it to some global varible? In the catch block, local e is binded to the exception object by thrown statement, I am not sure whether it is safe to bind this object outside, e.g. to some global object.
regards,
George
|
|
|
|
|
George_George wrote: I posted below in my question. Just the simple catch bracket, have found it?
Ok. I though (1) had its own code snippet and (3) had another.
George_George wrote: What does int 3 mean?
A breakpoint in ASM.
George_George wrote: 3.
Yes. See below.
You will see that p holds the same address as v .
int* p = NULL;
void SetN(int& r)
{
p = &r;
}
void Test()
{
try {
throw 5;
}
catch(int& v) {
SetN(v);
}
}
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
You doing the assignment work inside the catch block, do you think it is still safe to refer to global variable p after function Test returns?
regards,
George
|
|
|
|
|
George_George wrote: do you think it is still safe to refer to global variable p after function Test returns?
It will crash if changing my previous sample from int type to user defined type (class ). As:
class MyClass
{
public:
int a;
~MyClass() {
printf("~MyClass \n");
}
};
MyClass* p = NULL;
void SetN(MyClass& r)
{
p = &r;
}
void Test()
{
try {
MyClass m;
throw m;
}
catch(MyClass& v) {
SetN(v);
}
}
void main()
{
Test();
(*p).a;
}
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
Looks like you proved that it is not safe to access the exception object outside of the catch block? Right?
regards,
George
|
|
|
|
|
George_George wrote: Looks like you proved that it is not safe to access the exception object outside of the catch block? Right?
It depends. If you change my code into the below way, it lives.
void Test()
{
try {
MyClass* p = new MyClass();
p->a = 3;
throw *p;
}
catch(MyClass& v) {
SetN(v);
}
}
void main()
{
Test();
p->a;
}
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
1. I think you mean if the exception object is allocated on heap, it should be safe to address it later;
2. And if in your previous sample, it is local object, we are not safe to address it later.
Right?
regards,
George
|
|
|
|
|
George_George wrote: 1. I think you mean if the exception object is allocated on heap, it should be safe to address it later;
I guess so. It's just like common object allocations in the heap.
George_George wrote: And if in your previous sample, it is local object, we are not safe to address it later.
From the experiments, we get the conclusion:
1: It should be ok for simple data types (int , char , double ).
2: It crashes before main if it's of user types (class ).
Maxwell Chen
|
|
|
|
|
Seems we are both wrong, Maxwell!
I have written some code which verified that the caught object instance is not the original one we thrown. Looks like we only get a temporary exception object in catch block?
Here is my code. You can see two object instance is created.
class Foo {
public:
Foo(int input)
{
this->a = input;
cout << this << endl;
}
Foo (const Foo& input)
{
this->a = input.a;
cout << this << endl;
}
int a;
};
void foo()
{
try {
Foo *f = new Foo (100);
throw *f;
} catch (const Foo& e)
{
cout << e.a << endl;
}
}
int main()
{
foo();
}
Any comments?
regards,
George
|
|
|
|
|
George_George wrote: Looks like we only get a temporary exception object in catch block?
You are right.
Maxwell Chen
|
|
|
|
|
Thanks Maxwell!
My question is answered.
regards,
George
|
|
|
|
|
hi
how to convert CString to TCHAR* conversion (other than GETBUFFER method )
k.guru moorthy
|
|
|
|
|
The conversion should be automatic because CString has an LPCTSTR operator (check here[^], there is a code sample too).
|
|
|
|
|
Usually you don't need a pointer to TCHAR but rather a pointer to const one (i.e. const TCHAR * ). If this is your requirement then, as suggested by Cedric Moonen, simply use CString cast operator to LPCTSTR , for instance:
CString str=_T("foo");
const TCHAR * psz = str;
On the other hand, if you need a pointer to the editable string buffer, then you can't avoid using CString::GetBuffer method (and you have to be careful).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
modified on Monday, February 04, 2008 8:10:33 AM
|
|
|
|
|
CPallini wrote: On the other hand, if you need a standard pointer, then you can't avoid using CString::GetBuffer method (and you have to be careful).
what do you mean by that ?
|
|
|
|
|
Simply a pointer to editable memory, i.e. like psz in the statement
TCHAR * psz;
I used the perhaps misleading standard term to denote this kind of pointer as opposed to the one pointing to a const string, like pcsz in the following statement
LPCTSTR pcsz;
Nothing to do with standard library, of course.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
well, for me, char* is not much "standard" than const char*...
but ok, I got your point of view.
|
|
|
|
|
Probably I'm used to think about pointer as having full access to pointed memory, hence the misleading term. Anyway I also modified my original reply.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
I have a combo box and there are some data in that. now i have given two variables for this combo box. A CString variable and a control variable (CCombo). now my requirement is that the first data which is there in the list box should appear in the combo box. now its blank. what needs to be done for ths?
|
|
|
|
|
Supposing your control variable (CComboBox* ) name is MyCombo , the following statement should do the trick:
MyCombo->SetCurSel(0);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
got it done... !
|
|
|
|
|
hi,
I want to divide my main frame client rect into 2 parts, one of the parts for one window and the other for example for a view, besides they don’t have overlapped with each.
Thanks
Miss_F
|
|
|
|
|
You mean, something like windows explorer ? If yes, then you have to use a splitter window. You can found some documentation here[^]. There are also some articles[^] on CP about it.
|
|
|
|
|
No, i don't want splitter, i want sth like a tool bar,( but not it)( a window or control bar which is always there (static one at the top of the main frame))
and which has its own client rect and informs main frame the amount of space remains unallocated in the parent window's client area.
Miss_F
|
|
|
|