Advice on right data structure

Advice on right data structure

Page 1 of 1

3 Replies - 1004 Views - Last Post: 23 May 2008 - 07:28 PM Rate Topic: -----

#1 Nima221  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-May 08

Advice on right data structure

Post icon  Posted 22 May 2008 - 07:37 PM

Hi folks,

It's a question about to find the right way to implement the code.

I have a window separated in two parts, top and bottom., the top part of window contains fixed number of date(fields) but bottom part of window could be variable number of fields.
The top common part contain a popup menu that determines the contents of second part of window. The second parts can contain dynamic list (user can add items to that to whatever it need, theoretically), string, int,... that based on the selection in popup menu this items get appear or disappear for the second parts.

To implement this in C++, I was thinking to use of "union" for second part of window's content, but since the content can contain vector, I was wondering how can I define the nested structures when contains a vector? should i give the structure pointer to make the union items to have fixed size, and Max size be known? or you think there is better approach for whole this senario?


std::vector <string> myVect;
typedef myVect flist1;

struct sub_struct1 {
int i;
flist1 mylist;
};

struct sub_struct2 {
int k;
bool j;
};


std::vector < sub_struct2 > myVect2;
typedef myVect2 flist2;


struct sub_struct3 {
int kkk;
flist2 myList3;
};
.
.

struct mainStruct{

// my common top list item
int i;
int k;
..
// second part of window, that i know should be change ????????
union {
sub_struct1; // if I give the structure pointer here would be fine?
sub_struct3; // if I give the structure pointer here would be fine?
}u;

}


Thanks,
Nima :rolleyes:

Is This A Good Question/Topic? 0
  • +

Replies To: Advice on right data structure

#2 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: Advice on right data structure

Posted 23 May 2008 - 12:45 AM

It may be simpler to use the same structure for both windows, both containing a vector to list the elements of the page. The only difference is that the elements of the second window can be changed by the user, and that it shouldn't be able to change the elements of the first window.
Was This Post Helpful? 0
  • +
  • -

#3 Nima221  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-May 08

Re: Advice on right data structure

Posted 23 May 2008 - 07:17 AM

View Postjoske, on 23 May, 2008 - 12:45 AM, said:

It may be simpler to use the same structure for both windows, both containing a vector to list the elements of the page. The only difference is that the elements of the second window can be changed by the user, and that it shouldn't be able to change the elements of the first window.



Well, think I didn't explain it clearly.
Let's go to the question:
What is the right way to use "union" in C++ in following cases:

I want to declare this

union {

int k;

myStruct1;
myStruct2;

};

where i have declared the myStruct1 like this before:


struct myStruct1{
int l;
std::vector<someStructInCode> myVector; // someStructInCode is a struct defined earlier

};

i believe the declaring union above is wrong since there is in conflict with union definition that needs a know size of elements. Should i add the pointer of myStruct1 to "union" to fix this issue?

Thanks,
Nima
Was This Post Helpful? 0
  • +
  • -

#4 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Advice on right data structure

Posted 23 May 2008 - 07:28 PM

Using class types in a union is usually a bad idea. Everytime you want to use the other members in the union, it's likely that you'd need to reconstruct that member (as writing to the other member of the union corrupts the internal state of the object), and such usually isn't possible as there is no explicit method to construct the object besides the constructors usually.

There are several other ways that one could possibly deal with (though seemingly less convenient)....

One could use pointers (and using pointers in a union is fine, as long as one keeps close track as to what is there)...

struct mainStruct {
	int i; 
	int k;
	
	typedef enum { type1, type2 } data_type;
	data_type dt;	
	void* data;
};

switch(s.dt) {
   case type1:
	   do_something(reinterpret_cast<struct struct1*>(data));
 ........
}

--- Or ---

struct mainStruct {
	int i; 
	int k;
	
	typedef union {
		struct struct1* p1;
		struct struct3* p2;
	} u;
};

--- Or ---

struct mainStruct {
	int i; 
	int k;
	
	struct struct1* p1;
	struct struct3* p2;
};




(Pointers do not consume very much memory for this case, so it might not make much sense to use a union at all. You could use NULL to denote what field has been allocated, and non-NULL to indicate the converse).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1