Quote:
Originally Posted by elias_28
is there any problem that might rise by by having a vector as a member of struct in c++
|
Normally no. But ...
Quote:
i am having a double pointer to A and allocating the data to that ptr with malloc. I know that i am mixing C with C++ but i want to know if there can be any memory problems predicted by having this mixture.
Especially that the constructor of b will not be called when allocating A.
|
I'm not clear on what you are allocating with malloc (even less clear on why).
The constructor/destructor that matters in the code you posted is A() and ~A(). You need to be sure those are called correctly.
For example (but I didn't test it for typos etc.):
Code:
B b;
b.ptr_A = (A**)( malloc( n * sizeof(A*) ) );
I think that is OK, because the pointers don't necessarily need construction.
Code:
b.ptr_A[0] = (A*)(malloc( sizeof(A) ) );
That one is not OK. It is skipping the constructor for A.
But regarding your question about problems arising from having a vector as a member of a struct: I think that is a very distorted point of view. The problem (if any) comes from not calling a required constructor. Blaming that problem on the aspect of the struct that requires construction is not a good software engineering viewpoint.