我为数据创建了一个Product类,打算在系统中创建大量的所有产品,然后在这个结构中进行查找,但我还没弄清楚如何让它工作.这些阵列给我带来了麻烦.一些伪代码:
class Product { int m_price; int m_availability[]; // invalID,need to set a size ... etc // Constructor grabbing values for all members Product(int p,int a[],...);}static const Product products[] = { Product(99,{52,30,63,49},...),// invalID Syntax ... }
有没有办法制作像这样的作品?我唯一能想到的是按属性组织并跳过整个Product对象.我觉得这会让整个事情更难理解和维护.
有没有人对如何最好地组织这类数据有任何建议?
谢谢.
解决方法 旧式C风格的静态结构阵列听起来非常符合您的要求.在编译时初始化,零运行时开销,不使用堆栈或堆. C仍然是嵌入式世界的主要参与者并非共同发生.所以(一个配方 – 有足够的空间来改变这个细节);
// in .h file class Product { public: // putting this first means the class is really a struct int m_price; int m_availability[4]; //.... (more) }; extern const Product product_array[]; extern const int product_array_nbr;// in .cpp file const Product product_array[] = { { 23,{56,1,2,4},//....(more) },{ 24,{65,//....(more) }; const int product_array_nbr = sizeof(product_array)/sizeof(product_array[0]);总结
以上是内存溢出为你收集整理的用C组织静态数据全部内容,希望文章能够帮你解决用C组织静态数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)