-
January 17th, 2006, 07:59 PM
#1
pointer to an array of structs
This works:
struct FOO{
int junk;
};
struct FOO *ptfoo;
ptfoo=(struct FOO*) malloc(sizeof(struct FOO)*iNumOfFoos);
So why does it not work if struct FOO is now a member of a class and the allocation occurs in a member function?
class Test{
private:
struct FOO{
int junk;
};
struct FOO *ptfoo;
int doStuff(void);
};
int Test:oStuff(){
.......
ptfoo=(struct FOO*) malloc(sizeof(struct FOO)*iNumOfFoos);
}
I get an error stating "Cannot convert from 'struct Test::FOO*' to 'struct Foo*'
Thanks!
-
January 17th, 2006, 08:10 PM
#2
Re: pointer to an array of structs
Originally Posted by eeboy
I get an error stating "Cannot convert from 'struct Test::FOO*' to 'struct Foo*'
C/C++ is case sensitive.
- petter
-
January 17th, 2006, 08:15 PM
#3
Re: pointer to an array of structs
Please do not mix C with C++. In C++, you should be using new and delete for memory allocation and deallocation rather than malloc and free. A good reason for not doing so is that new will invoke the constructor to initializing the class/structure member variable while malloc does not.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
January 17th, 2006, 09:28 PM
#4
Re: pointer to an array of structs
-
January 17th, 2006, 09:43 PM
#5
Re: pointer to an array of structs
One more thing in C++, it is not necessary to declare an instance of struct like, "struct FOO *ptfoo;"
Instead you should use "FOO *ptfoo;"
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
January 18th, 2006, 12:21 AM
#6
Re: pointer to an array of structs
Thanks! I am now using new and delete. Initially I had the same exact issue when compiling. I took Butterfly's advice and added the scope (Test: to FOO.... that worked like a champ.
-
January 18th, 2006, 04:51 AM
#7
Re: pointer to an array of structs
And since you are using C++....forget about the whole dynamic allocation and use a vector instead...
Code:
#include <vector>
class foo
{
private:
struct bar
{
int i;
};
std::vector<bar> vec;
};
-
January 18th, 2006, 05:57 AM
#8
Re: pointer to an array of structs
Apart from all that, Test::FOO and FOO are different classes (or structs) even if they happen to have the same members.
So you can't simply convert automatically between them unless you have defined the conversion.
-
January 18th, 2006, 06:48 AM
#9
Re: pointer to an array of structs
I think what wildfrog has said is true here. It is a matter of case sensitivity.
Originally posted by Butterfly
Code:
struct Test::FOO *ptfoo;
I think it is not necessary to qualify the FOO pointer inside the Test class with Test::FOO. the FOO pointer and the FOO struct are in the same scope.
if FOO structure is declared in the global level as well as in the class (Test) level, to use the global struct scope resolution operator should be used. Any function inside the Test class can use the local FOO struct without qualifying it as Test::FOO.
If there is no love sun won't shine
-
January 18th, 2006, 10:47 AM
#10
Re: pointer to an array of structs
Correct, it is not necessary to qualify Test::FOO within Test but it would be necessary to qualify ::FOO if you wanted to use it.
-
January 19th, 2006, 01:33 PM
#11
Re: pointer to an array of structs
Does a vector call the constructor? If so, that would work great!
-
January 19th, 2006, 01:43 PM
#12
Re: pointer to an array of structs
Originally Posted by eeboy
Does a vector call the constructor? If so, that would work great!
Yes. A vector has to create a copy of your object, and to do that, it must construct the object, therefore the constructor is called.
Regards,
Paul McKenzie
-
February 21st, 2006, 01:08 AM
#13
Re: pointer to an array of structs
ei, I've got a follow up question..
what if I declared an array of structs. And made it as a member variable of my main dialog. Then I've got a modal child dialog which retrieves the values and also replace the values in my struct. How will i free the struct?
Code:
MainDlg.h:
typedef struct VALUE{
int Lngth;
CString Stng;
}stVAL;
stVAL m_nOw[200];
Modal ChildDlg:
for( iT = 0; iT<= 5; iT++){
cMainDlg->m_nOw[iT].Lngth;
cMainDlg->m_nOw[iT].Stng;
}
Where and How will I free the array of structs inorder for me to reuse it?
-
February 21st, 2006, 01:17 AM
#14
Re: pointer to an array of structs
If the array is being declared as a member of your dialog class. It will only be freed when the dialog class is destroyed, e.g. when you program terminates.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
February 22nd, 2006, 01:21 AM
#15
Re: pointer to an array of structs
ei thnx,
I over looked the struct that i have created with struct pointers. I don't need to free it because it is an array and not a pointer. But thanks again.
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
|