1

Can I add member variables to a structure, after i have declared it? For example:

struct address 
    { 
       char name[50]; 
       char street[100]; 
    };

Can i dynamically (through code), add another member variable to the structure? Using something like address.addMember(pinCode); if something like this exists!

struct address 
    { 
       char name[50]; 
       char street[100]; 
       int  pinCode;
    };
4
  • 9
    No, you can't to this in C++. Why would you want to do this?? Commented Jan 23, 2020 at 8:12
  • 4
    You could create a new structure that inherits from the base address structure, which then contains the extra member. Commented Jan 23, 2020 at 8:15
  • No. If its worth the effort, you could create something similar to VMS' pass by descriptor to do the job. itec.suny.edu/scsys/vms/ovmsdoc073/v73/5841/5841pro_052.html
    – Uri Raz
    Commented Jan 23, 2020 at 8:36
  • That's a definition, not a declaration. A structure declaration looks like struct address;.
    – molbdnilo
    Commented Jan 23, 2020 at 9:13

4 Answers 4

4

What you strive to achieve is not possible so bluntly, however it's not impossible!

One way would be by extending the address by the new members via inheritance.

struct address 
{ 
   char name[50]; 
   char street[100]; 
};

struct pin_address : public address 
{ 
   int pinCode;
};

Other would be by implementing some kind of std::variant - std::map structure which will allow you to dynamically add/remove members.

#include <variant>
#include <map>

using Object = std::variant<int,double,std::string>;
using Members = std::map<std::string,Object>;

struct address
{
    Members m_members;

    address()
    {
        m_members.emplace("name", Object{});
        m_members.emplace("street", Object{});
        m_members.emplace("pinCode", Object{});
    }
}
4

"Can I add member variables to a structure, after i have declared it?"

No. That is not possible in C++.

4

What you ask is not possible, as already mentioned in another answer.

Depending on your needs, however, one way to address it could be to define another class inheriting from the one you have defined already, in which you add other members (variables/functions).

struct address 
{ 
    char name[50]; 
    char street[100]; 
};

struct addressForBurglars : address  
{ 
    int PinCode;
};
2

Well, first things first. Answer to your core question is 'No' in C++.

But, keeping the technicalities aside, whether feasible or not in a programming paradigm, please think about the need of such a requirement and you may address the inner problem of choice of the data structure. In my humble opinion, that needs more attention here.

Couple of thoughts around the design:

  1. If you need more fields later on, you can declare possibly all of them and hydrate the fields you need/have and have checks in place around the same.
  2. If you dynamically need to add more fields to your data,map is the data structure you would possibly be using.

Not the answer you're looking for? Browse other questions tagged or ask your own question.