|
Is the following way correct for inckuding a constructor in a struct
<br />
struct Stru_A{<br />
Stru_A(char Name[30], int Age) <br />
{<br />
<br />
strcpy(this->Name, Name);<br />
this->Age = Age;<br />
}<br />
char Name[30];<br />
int Age;<br />
<br />
}*pStru_A;<br />
<br />
<br />
pStru_A xyz = new Stru_A("ABC", 123);<br />
<br />
|
|
|
|
|
Rather than use C-style types such as char , use the C++ type string . This will avoid the use of memset() , strcpy() and strncpy() . Hard-coding numbers (e.g, 30) is usually not a good thing.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Might want to make sure that the OP is not going to be doing memory-style copies with the structure first before suggesting something like that. For example, if passing the address of an instance of the structure to something like fwrite(...) or WriteFile(...) , it will likely not work as expected if you have a dynamic-allocating string object in it.
DavidCrow wrote: Hard-coding numbers (e.g, 30) is usually not a good thing.
Usually? Limits are what normally define the functionality of a system. They just have to be enforced in order to prevent problems.
Peace!
-=- James 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! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
James R. Twine wrote: Usually?
I was erroring on the side of "there might be an exception." I know of no reason to use:
foo(char Name[30]); especialy if multiple of them exist. Once the size of the variable changes, every function prototype would also have to be changed.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: I was erroring on the side of "there might be an exception." I know of no reason to use:
foo(char Name[30]); especialy if multiple of them exist. Once the size of the variable changes, every function prototype would also have to be changed.
You are correct - there is no reason to write a prototype that way (except for documentation purposes) because array types in a prototype decay into pointers. So even if you did write it that way, the specified lengths would not enforced. So if you did change lengths, the functions would still compile. That is why you cannot write code like this:
void foox( char caData[ 30 ] ) { TRACE1( "foo %s\n", caData ); }
void foox( char *cpData ) { TRACE1( "foo2 %s\n", cpData ); }
void foox( char cvData[] ) { TRACE1( "foo3 %s\n", cvData ); } Because all three are really the same thing - the compiler will likely complain about duplicate function bodies. That is also why you can do the following:
void foo( char caData[ 30 ] ) { TRACE1( "foo %s\n", caData ); }
void foo2( char *cpData ) { TRACE1( "foo2 %s\n", cpData ); }
void foo3( char cvData[] ) { TRACE1( "foo3 %s\n", cvData ); }
char *cpTest = "Test";
char caTest30[ 30 ] = { "Test" };
char caTest10[ 10 ] = { "Test" };
char cvTest[] = "Test";
foo( cpTest ); foo( caTest30 );
foo( caTest10 ); foo( cvTest );
foo2( cpTest ); foo2( caTest30 );
foo2( caTest10 ); foo2( cvTest );
foo3( cpTest ); foo3( caTest30 );
foo3( caTest10 ); foo3( cvTest ) And the compiler will not complain, allowing all of the variable types to be passed to foo1/foo2/foo3 without issue.
<HR> First rule of buffers: when you pass a buffer, pass its usable length.
The point about changing lengths is exactly why you use something like const DWORD IDENTIFIER s to manage buffer lengths - change it once, change it everywhere. Combine that with a strategy like Size+1 and you have a way to manage string limits without risk of buffer overruns. All it takes to use and limit buffers correctly is a little discipline.
Peace!
-=- James 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! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
In C++ the only difference between struct and class is the default visibility of members. A constructor for a struct is defined just like a constructor for a class .
|
|
|
|
|
struct Stru_A{
char Name[30];
int Age;
}Stru_A, *pStru_A;
pStru_A foo = new Stru_A;
foo->Name(blah blah blah);
foo->Age(blah blah blah);
I win because I have the most fun in life...
|
|
|
|
|
I believe this may be what you are looking for:
struct Stru_A
{
char Name[30];
int Age;
Stru_A::Stru_A()
{
Age = 0;
memset(Name,0,30);
}
}
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Is the following way correct for inckuding a constructor in a struct
<br />
<br />
struct Stru_A{<br />
Stru_A(char Name[30], int Age) <br />
{<br />
<br />
strcpy(this->Name, Name);<br />
this->Age = Age;<br />
}<br />
char Name[30];<br />
int Age;<br />
<br />
}*pStru_A;<br />
<br />
<br />
pStru_A xyz = new Stru_A("ABC", 123);<br />
<br />
|
|
|
|
|
I believe for what you're trying to do, you'll need this (note there are two constructors here - one with no parameters and the one you are trying to use):
struct Stru_A{
char Name[30];
int Age;
Stru_A::Stru_A()
{
memset(Name, 0, 30);
Age = 0;
}
Stru_A::Stru_A(char* inName, int inAge)
{
memset(Name, 0, 30);
strncpy(Name, inName, 29);
Age = inAge;
}
}*pStru_A;
pStru_A xyz = new Stru_A("ABC", 123);
Note also that you should use strncpy to be sure the copy doesn't overwrite the 30 character size you have, and still leaves space for the null terminator.
Hope that helps.
-- modified at 16:32 Thursday 21st December, 2006
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
krmed wrote: Note also that you should use strncpy to be sure the copy doesn't overwrite the 30 character size you have, and still leaves space for the null terminator.
You also will have to manually put that terminating NUL into the Name buffer - strncpy(...) will not place a terminating NUL if reaches the specified limit.
Peace!
-=- James 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! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Actually, since I have a memset to put a zero in all elements, manually adding a terminating NULL is not required - you'll notice I only copied 29 characters into the buffer in my example.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Correct. I was speaking in the general case, because if strncpy(...) is going to be used in this particular constructor, it is going to be used elsewhere as well, and knowing the limitations of the function are still important.
Peace!
-=- James 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! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
A struct and a class are exactly the same, except for the default member access (public and private, respectively). So you can write a constructor for Stru_A just as you would if it were a class.
|
|
|
|
|
Hi,
I've got a combobox that I'm populating values in this order:
AlphaEntry
6020
Now, when the combobox displays, it reverses the value like this:
6020
AlphaEntry
What gives?
I should add, these names are the text filenames of binary files loaded at runtime. I'm NOT setting these values at design time.
|
|
|
|
|
You have to remove the CBS_SORT style (it is a default value, at design time).
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.
|
|
|
|
|
Hrm, how can I crawl out of here without anyone seeing me?
|
|
|
|
|
Don't worry about...
Such moments happen to everybody and usually you expect to find things as you leave them.
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.
|
|
|
|
|
there's a CComboBox style(CBS_SORT) option when creating the combobox, be certain that it's not there when you create it.
|
|
|
|
|
Maximilien wrote: be certain that it's not there when you create it.
Actually it is the default on Visual Studio resource editor.
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.
|
|
|
|
|
Like2Byte wrote: 6020
AlphaEntry
What gives?
6 has an ASCII value of 54, while A has an ASCII value of 65. That's why 6020 precedes AlphaEntry.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Yeah, this I *do* know.
|
|
|
|
|
Hi everyone,
does exist a event or something like this that informs that the Frame is now maximized.
Because during the Maximization of the Frame, more than one OnSize-Event occurs.
Now i need a indice that the maximization is over and i can perform my actions...
Does any one have an idea?
Big thanks for your help
|
|
|
|
|
have a look to GetWindowPlacement .
Hope that helps.
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.
|
|
|
|
|
It has each times (3x) the same value (SW_SHOWMAXIMIZED)
nervertheless thanks
|
|
|
|