6

Is there any interest to forward a class as a struct and vice versa ? It seems to be perfectly legal, but some use cases exist ?

struct Bar;
class Foo
{
Bar * bar_;
};

class Bar
{

};
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145

4 Answers4

3

There isn't a use case any more or less than there is a use case for forward declaring with the same keyword. It makes no difference to the meaning of the program. The class-key identifier only makes a difference when defining the class.

The above applies to standard compliant compilers. Some non compliant ones might handle the declarations differently in which case there is a case for using the same keyword in particular.


Ok, here is a practical use case. Let's say you've implemented a class with the struct keyword. Over time, the class is widely used across multiple code bases and it is declared in many headers using the same keyword. At a later time, perhaps after adding a ton of features, you decide that classwould be more appropriate and you refactor the code. Now, there isn't a point in refactoring all the unrelated depending code bases to use the new keyword.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2
struct Bar;
class Bar;

These are equivalent, it doesn't matter.

but some use cases exist ?

No special ones IIRC.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

From the language's point of view, there is no difference between forward-declaring a class as struct or class.

class Foo;
struct Foo;
class Bar;
struct Bar;
class Foo{}; // 100% ok
struct Bar{}; // 100% ok

Unfortunately, some compilers mangle those keywords differently. As described here, this can be nasty when you're depending on name mangling being the same.

Consider:

// process.h
struct Foo;
void processIfNotNull(Foo*);

// foo.h
class Foo{};

If you export such function in your shared library, your users will not be able to call processIfNotNull unless they have the definition of Foo.

Community
  • 1
  • 1
krzaq
  • 16,240
  • 4
  • 46
  • 61
0

It's not usefull.

In C++ the difference between struct and class is that for struct members are public by default.

Considering this, in your example it could just confuse the code reader to determine if your class will behavior as a struct or a class.

You can read this post for further information : When should you use a class vs a struct in C++?

Community
  • 1
  • 1
Dali
  • 344
  • 1
  • 10
  • That's about when using one or the other. This is solely about forward declarations. – Hatted Rooster Oct 20 '16 at 08:06
  • @GillBates which is important knowledge to understand why it might be useful for a programmer to know the keyword used in definition by reading only the declaration. – eerorika Oct 20 '16 at 08:11
  • What I mean is that forward declaration of a class as a struct is allowed, but has no intererest and can only confuse the code comprehension. – Dali Oct 20 '16 at 08:11
  • *"Considering this, in your example it could just confuse the code reader to determine if your class will behavior as a struct or a class." (sic)* There is no difference in "behaviour" between the two. It is only confusing for people who (wrongly) think there is a difference. – juanchopanza Oct 20 '16 at 08:13
  • - Members are public by default in a struct, private by default in a class. - Classes can't be used when interfacing to C - Classes create a namespace to encapsulate its members Those are differences that might be confusing if you switch between class and struct in your forward declaration. – Dali Oct 20 '16 at 08:15
  • 2
    If C++ classes cannot be used for interfacing with C, then neither can C++ structs, because except for the default access, they are *exactly the same*. The differences you list after your first sentence in the comment above are bogus. `struct` and `class` are keywords that can be used to define exactly the same classes. – juanchopanza Oct 20 '16 at 08:26
  • @abel C++ doesn't have structs, it has classes, and the keyword `struct` is a way to declare a class with public visibility by default. – TartanLlama Oct 20 '16 at 08:29
  • Imo it is useful to follow the idiomatic distinction between class and struct, at least not to confuse C# or D programmers reading the code. But it's not a language rule and definitely not an answer to question asked here. – krzaq Oct 20 '16 at 08:29
  • @krzaq There is no idiomatic distinction. There are many - often differing - coding conventions. Better to understand that there really is no difference. – juanchopanza Oct 20 '16 at 08:30
  • 1
    @juanchopanza you can unserstand and still use struct for dumb types for clarity, though. – krzaq Oct 20 '16 at 08:31
  • @krzaq And the definition of "dumb types" is? – juanchopanza Oct 20 '16 at 08:32
  • @juanchopanza broad and fuzzy. – krzaq Oct 20 '16 at 08:34
  • 2
    quoting Stroustrup's "The C++ Programming Language", 4th edition, section 16.2.4: These two definitions of S are interchangeable, though it is usually wise to stick to one style. Which style you use depends on circumstances and taste. I tend to use struct for classes that I think of as "just simple data structures." If I think of a class as "a proper type with an invariant," I use class. Constructors and access functions can be quite useful even for *struct*s, but as a shorthand rather than guarantors of invariants. – Dali Oct 20 '16 at 08:35
  • 1
    @Abel `Classes create a namespace to encapsulate its members Those are differences...` No, there is no difference between classes defined with `struct` vs `class` in this regard. – eerorika Oct 20 '16 at 09:17