Cybercop
Cybercop

Reputation: 8694

pointer to member variable of struct

class Main
{
  Struct BranchSub
  {
    Sub()
    {
      subName[0] = '\0';
    }
    char subName[20];
  };

  struct MainSub
  {
  Sub sub[20];
  };
};

i want to have a method that will return pointer subName when subName matches with given text. For example something like:

MainSub test;
if(strcmp(test.BranchSub[5].subName, "Hello") == 0);//return pointer to `test.Branchsub[5].subName`

is it possible?? or is there other way to achieve the desired result?

Upvotes: 0

Views: 151

Answers (1)

masoud
masoud

Reputation: 56529

Yes, it's possible to use test.BranchSub[5].subName.

For second part of your question, you should use std::string:

class Main
{
  struct BranchSub
  {
    std::string subName;
  };

  struct MainSub
  {
    BranchSub sub[20];
  };
};

And then

MainSub test;
if(test.sub[5].subName == "Hello")

is more clear.

You even can use std::vector<BranchSub> instead of BranchSub sub[20].

Upvotes: 1

Related Questions