Dynamic data members?

This is a discussion on Dynamic data members? within the C++ Programming forums, part of the General Programming Boards category; Hi there! I'm currently stuck in creating a data structure with dynamic amount and types of data members/variables. To be ...

  1. #1
    ad space for sale confusalot's Avatar
    Join Date
    Feb 2005
    Posts
    4

    Question Dynamic data members?

    Hi there!

    I'm currently stuck in creating a data structure with dynamic amount and types of data members/variables.

    To be more specific - the target device I'm developping for, is the Palm PDA, so I have to look for memory efficient ways of implementation.

    I created a table which views editable user data of different types. However, this table's columns can be changed, so the data structure of a table row should be able to change, too.
    Currently the structure is fixed, so depending on the table layout configuration, a couple of data members are unused.

    Since the table row data structure is stored in a dynamic array, the PDA's heap might be "polluted" by unnecessary non-used data.

    A friend of mine advised me to create a class with a virtual intferace whose children classes contain the actual data members of different types and the methods' implementations.
    That sounded good to me, but I'm stuck in implementing it, because at some point I need a specific data type, which actually differs from the implementation of the virtual interface.

    Code:
    class CBaseClass
    {
     public:
      virtual void setValue( ??? value );
      virtual ??? getValue();
    }
    This cannot be the right attempt, because data types cannot be overridden in children classes. I probably did not properly understand my friend, since I'm not an experienced C++ programmer.

    I was looking for hours for an answer on the net and now I'm totally confused about this.
    My first idea was using an Array of void* pointers and cast the proper data type in the appropriate case, but that's no elegant solution.

    I'd appreciate any pointer (no pun intended) to the right direction.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Derived classes are not restricted to data members of inherited base classes.
    Code:
    class base
    {
    public:
    	 virtual void speak() = 0;
    };
     
    class A : public base
    {
    public:
    	 void speak() { cout << "English"<< endl;}
    	 int getAge() {return age;}
    	 void setAge(int age_) {age = age_;}
    private:
    	 int age;
    };
     
    class B : public base
    {
    public:
    	 void speak(){cout << "Spanglish" << endl;}
    	 int getSalary{ return salary;}
    	 void setSalary(int salary_) {salary = salary_;}
    	private
    	 int salary;
    };
    In this code base was made an abstract class, meaning that the user is not able to declare an object of type base. Type base is merely to provide a common interface for all subsequent derived classes. That is all classes derived from type base will have everything type base has, and they can have more, too, if desired. In this case all derived classes are to inherit the speak() function, but since the author desired to allow each derived class to implement the speak() function to suit their needs it was declared virtual. Note that type base has no data members but class A has a data member called age and class B has a data member called salary. Thus objects of class A can speak() and have an age but not a salary and objects of class B can speak()--which may or may not be the same speak() as in class A-- and have a salary but not an age.

    If you are not familiar with class inheritance rules, etc. I strongly recommend picking up a book at your favorite store and reading about them there as the topic is way to broad to discuss appropriately in a forum such as this. Used books on Amazon/Barnes and Nobles, etc. can be quite cheap. In addition there are several books available for free on line, if you can't afford even a used book. Search the board and you'll find numerous recommendations.
    You're only born perfect.

  3. #3
    ad space for sale confusalot's Avatar
    Join Date
    Feb 2005
    Posts
    4
    Thanks for your advice, but I don't think that understanding inheritance is my problem, since I'm rather new to C++ but not object-oriented programming.

    The abstract class was only an idea for solving this "dynamic data structure" issue.

    The "base" class should provide a get and a set interface method which are supposed to be overridden by descendant classes for setting and retrieving different data types.

    Given the following case:
    Code:
    class Base
    {
    };
    
    class A : public Base
    {
     public:
      int get() { return _value; }
      void set ( int value ) { _value = value; }
    
     private:
      int _value;
    };
    
    class B : public Base
    {
     public:
      string get() { return _value; }
      void set ( string value ) { _value = value; }
    
     private:
      string _value;
    };
    
    typedef CArray<Base> MyTableRow
    
    class MyTable
    {
    ....
     protected:
     CArray<MyTableRow> _myTableRows
    };
    I would like to know how to access A's or B's get and set methods from _myTableRows.

    Since I do not think, that this attempt leads to the right direction, I ask you, whether there is a different way to solve my problem of a dynamic data structure without creating lots of different methods which do actually the same on different data types.
    I hope I could clearly explain myself this time...

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    you could do something like this:

    Code:
    struct Object {
      virtual void dummy() {}
    };
    
    struct StringObject : public Object {
      std::string str;  
    };
    
    struct IntObject : public Object {
      int i;
    };
    
    
    struct TableItem {
      Object &get_value();
    };
    
    struct  A : public TableItem {
      StringObject string_object;
    
      StringObject &get_value();
    };
    
    struct  B : public TableItem {
      IntObject int_object;
    
      IntObject &get_value();
    };
    even though the return types of the get_value methods differ - they are still compatible.

    the virtual dummy function is neccessairy in order to use typeid
    Last edited by Raven Arkadon; 02-27-2005 at 10:07 AM.
    signature under construction

  5. #5
    ad space for sale confusalot's Avatar
    Join Date
    Feb 2005
    Posts
    4
    Thanks Raven - that's an interesting attempt... I'll give it a shot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamicly adding data members to structs
    By ITAmember in forum C Programming
    Replies: 11
    Last Post: 06-01-2009, 03:26 PM
  2. Global acess to dynamic allocated data
    By dereach in forum C Programming
    Replies: 2
    Last Post: 02-25-2008, 11:10 PM
  3. static data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 05:18 PM
  4. read-only data members. A question of good practices
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2006, 04:35 AM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21