|
-
May 27th, 2002, 11:34 PM
#1
Thread Starter
PowerPoster
Confusing char array
I have a function inside a class called "GetCaption" that lets the user retrieve current text assigned to a button. The caption is stored in a varible in the class as:
Then the "GetCaption" function is like this:
PHP Code:
char CGButton::GetCaption()
{
if (CGButton::Handle == NULL)
{
return NULL;
}
else
{
return (char)CGButton::caption;
}
}
Now I want to use this function like this but it's giving me an eror message "Unhandled exception in...":
PHP Code:
MessageBox(NULL,(char*)mybutton.GetCaption(),"hello",MB_OK);
-
May 28th, 2002, 04:38 AM
#2
Frenzied Member
Are you sure the handle exists and the appropiate code runs?
and hmm shouldn't you dereference the pointer before returning it because the pointer will go out of scope?
also, I think the function can be shortened to:
PHP Code:
char CGButton::GetCaption()
{
if (CGButton::Handle == NULL)
return NULL;
return (char)*CGButton::caption; //Notice the derefencing, try it.
}
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
May 31st, 2002, 02:28 AM
#3
That returns only the first character. Why not return a char*? (Or const char* if you don't want the string to be edited.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 31st, 2002, 12:08 PM
#4
Monday Morning Lunatic
You shouldn't be returning a pointer anyway, since the object might go out of scope.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 31st, 2002, 03:22 PM
#5
Well, I think it is different when returning a member variable (that's what the char pointer is).
But in this case (MFC extension class) it is better to store the caption in a CString and return that.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 31st, 2002, 03:29 PM
#6
Monday Morning Lunatic
Just as an example of what I usually do and what I think you mean (feel free to pick holes):
Code:
class thingie {
public:
thingie(const string &cap) : m_caption(cap) { }
thingie& operator=(const thingie &ref) {
if(this == &ref) return *this;
m_caption = ref.m_caption;
return *this;
}
const string& caption() const { return m_caption; }
// mutator method if you want it
void caption(const string &c) { m_caption = c; }
private:
string m_caption;
};
For the accessor, this means that you're not penalised if you only need it during the lifetime of the object, but can persist it if you want.
The standard strings can be easily replaced with a CString in that (it's the return-by-const-reference that's important here).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 2nd, 2002, 02:48 AM
#7
Thread Starter
PowerPoster
Actually I got it working after a while using something like:
PHP Code:
char *CGLabel::GetCaption()
{
return (char*)CGLabel::caption;
}
Does this return a pointer to a local variable or it actually returns a pointer and sets its value to the value of CGLabel::caption pointer?
-
Jun 2nd, 2002, 02:46 PM
#8
It does not return a variable local to the accessor, but it may still be unsafe in some certain situations (but to be honest, I've never encountered one before).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 2nd, 2002, 02:50 PM
#9
Monday Morning Lunatic
Returning it like that is safe as long as you don't try and hold onto it after the object has been destroyed.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 2nd, 2002, 02:53 PM
#10
Thread Starter
PowerPoster
-
Jun 3rd, 2002, 08:47 AM
#11
I never felt tempted to do that...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 3rd, 2002, 10:24 AM
#12
Monday Morning Lunatic
Originally posted by CornedBee
I never felt tempted to do that...
Do what?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 3rd, 2002, 10:29 AM
#13
Frenzied Member
I think he means:
you don't try and hold onto it after the object has been destroyed.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|