Forward declare a type previously declared as a class as struct

Is there any interest in transitioning a class as a structure and vice versa? This seems perfectly legal, but are there some use cases?

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

class Bar
{

};
+4
source share
4 answers

There is no use case more or less than there is a use case for a forward ad with the same keyword. It does not matter for the value of the program. The class key identifier is relevant only when defining a class.

. -, .


, . , struct. , . , , , class , . , .

+3
struct Bar;
class Bar;

, .

?

IIRC.

+2

forward-declaring a class struct class.

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

, -. , , , .

:

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

// foo.h
class Foo{};

​​ , processIfNotNull, Foo.

+1

.

In C ++, the difference between struct and class is that for members of the structure, public is the default.

Given this, in your example, this may simply confuse the code reader to determine if your class will work as a structure or a class.

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

0
source

Source: https://habr.com/ru/post/1658294/


All Articles