3

I've got something like:

typedef struct Data_s {
  int field1;
  int field2;
} Data;

class Foo {
  void getData(Data& data);
  void useData(Data& data);
}

In another class's function, I might do:

class Bar {
  Data data_;
  void Bar::taskA() {
    Foo.getData(data_);
    Foo.useData(data_);
  }
}

Is there a way to move the Data out of the global scope and into Foo without creating a new class? Data mirrors a struct existing in a library I'm using elsewhere. (i.e. same fields, just different name. I cast Data into the other struct later. I do this because Foo is an abstract class, and the derived class using the library is just one of many.)

Currently, just pasting it inside the class and replacing Data with Foo::Data everywhere doesn't work.

class Foo {
  typedef struct Data_s {
    int field1;
    int field2;
  } Data;
  ...
}

I get 'Data' in class 'Foo' does not name a type at Bar data_;

3
  • So you want to create a class which is similar to Data_s? Or you want to copy its members' values at runtime into a class? Commented Mar 28, 2010 at 22:25
  • 1
    There are only a few differences between struct and class, what are you trying to achieve by 'moving struct to class' ? Probably some clarification about what exactly are you trying to do would help. Commented Mar 28, 2010 at 22:29
  • Added/clarified. Is that better? Commented Mar 28, 2010 at 22:55

2 Answers 2

4

You can define the struct inside the class, but you need to do it ahead of the place where you first use it. Structs, like classes themselves, must be forward-declared in order to use them:

class Foo
{
public:
    struct Data
    {
        int field1;
        int field2;
    };

    void getData(Foo::Data& data) {}
    void useData(Foo::Data& data) {}
};

void UseFooData()
{
    Foo::Data bar;
    Foo f;
    f.getData(bar);
    f.useData(bar);
}

Edit: Updated example to use the same fields/class names as listed in the original question. Please note that in order to be visible outside of the Foo class, Data needs to be declared public and other code needs to reference it as Foo::Data.

Sign up to request clarification or add additional context in comments.

1 Comment

Since the question has struct Data_s and class Foo, can you revise your answer in terms of those?
2

More precisely, Foo::Data isn't accessible in the code snippet you gave. Class members default to private visibility.

Secondly, when you're using C++, typedef for structure declarations is NOT recommended (there are other valid uses, but your usage is not).

Try this corrected version instead:

class Foo {
public:
  struct Data {
    int field1;
    int field2;
  };
  ...
}

3 Comments

just curious, when is typedef for structure declarations recommended then?
Never in C++. In C, if you didn't use a typedef, you'd have to say "struct that" everywhere, the typedef lets you say just "that". With C++ you don't need the struct, so the typedef is useless. Typedef has other uses, especially in the presence of templates, but typedef on a class or struct definition is a quick way to spot a C programmer with zero C++ experience.
Actually, I was doing that only b/c I was following the example of the minizip and unrar libraries, which are written in C. @.@

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.