struct definition / declaration - style question

hey guys,

I'm just curious how you guys would define & declare a struct which is only used by one class and others don't need to know about it.

Right now i like to do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class MyClass
{
// at the beginning of the class definition
    struct MyStruct
    {
        int a;
        float b;
    };
    
    //.........

private:
    MyStruct s;
    void Abc(const MyStruct& my_struct);
    //... whatever ....
};


Here the struct can be still read through the .h file, but can't be used by others.

What would you guys recommend? :P
Last edited on
closed account (48bpfSEw)
There is nothing wrong with your code. The struct is defined and known only within the class.

The programming language "pascal" has sub-functions. I simulate them sometimes with classes!

Example

1
2
3
4
5
6
7
8
9
10
11
12
program HalloWelt;
    procedure foo;
        procedure bar;
        begin
            writeln('Hallo Welt');
        end;
    begin
        bar
    end;
begin 
    foo;    
end.



1
2
3
4
5
6
7
void HelloWorld() {
   class Foo {
      public: Foo() { out << "hello world!" << endl; }
   } foo;

  return;
}
I'm just asking, because some like to avoid to put structs into class definitions.
Some define them outside of the class.

My version might be annoying for some programmers, because having multiple struct definitions in the class might be disturbing when reading the class definition.
Last edited on
If your class definition needs to use the struct type - e.g. to define one of the data members - then you have to have the struct definition in the header with it.

If it doesn't need to use it - e.g. if the struct type is only used within the function definitions - then it makes sense to move it to the .cpp file, so it's not included and compiled as part of every translation unit that includes the header.
hmm, thats true.
just never had the case where a struct is only used in function definitions :)
One example: if you only use pointers or references to the struct type in the header, then you can simply forward declare it there, and have the actual definition in the source file.
Last edited on
yeah of course :)

but wouldn't it be still good to put it inside the header, so you can straight see the struct definition without the .cpp file, which makes it much more convenient for a user of the class, but as you mentioned already this would come with the cost of the compilation for every translation unit.
Last edited on
With your simple example I would first question whether the structure and the functions that use the structure even need to be part of the class. Perhaps the functions and that structure would be better as non-class "private" functions that the user doesn't ever need to know exist.
Last edited on
If the structure is a necessary part of the interface to the class, then, yes, the user will need to know about it. For example, if it contains data that needs to be passed to the class from the calling code.

If it's not part of the interface, then there's nothing to be gained by showing the user the definition.

(It's a feature of C++ that you have to expose some aspects of the class's implementation to the calling code, as the private and public class members have to be declared in the same place. But it's probably best to minimize that. Of course, you can always use the pImpl pattern if you really want to hide things from the calling code.)
Topic archived. No new replies allowed.