Jonathan
Jonathan

Reputation: 77

Why use 'struct' keyword in class pointer declaration in C++

When and why should we use the 'struct' keyword when declaring a class pointer variable in C++?

I've seen this in embedded environments so I suspect that this is some kind of hold over from C. I've seen plenty of explanations on when to use the 'struct' keyword when declaring a struct object as it relates to namespaces in C (here), but I wasn't able to find anyone talking about why one might use it when declaring a class pointer variable.

Example, in CFoo.h:

class CFoo
{
public:
    int doStuff();
};

inline Foo::doStuff()
{
    return 7;
}

And later in a different class:

void CBar::interesting()
{
    struct CFoo *pCFoo;

    // Go on to do something interesting with pCFoo...
}

Upvotes: 3

Views: 906

Answers (4)

Bathsheba
Bathsheba

Reputation: 234635

There's rarely a reason to do this: it's a fallover from C and in this case the programmer is simply being sentimental - perhaps it's there as a quest for readability. That said, it can be used in place of forward declarations.

In some instances you might need to disambiguate, but that's not the case here. One example where disambiguation would be necessary is

class foo{};

int main()
{
    int foo;
    class foo* pf1;
    struct foo* pf2;
}

Note that you can use class and struct interchangeably. You can use typename too which can be important when working with templates. The following is valid C++:

class foo{};

int main()
{    
    class foo* pf1;
    struct foo* pf2;
    typename foo* pf3;
}

Upvotes: 7

Pete Becker
Pete Becker

Reputation: 76245

The reason for this may be as simple as not having to include a header file whose contents aren't needed other than for announcing that CFoo names a type. That's often done with a forward declaration:

class CFoo;
void f(CFoo*);

but it can also be done on the fly:

void f(struct CFoo*);

Upvotes: 2

Lundin
Lundin

Reputation: 213306

In C, two different styles are the most common:

  • typedef struct { ... } s; with variables declared as s name;.
  • struct s { ... }; with variables declared as struct s name;

In C++ you don't need to typedef to omit the struct keyword, so the former style is far more in line with the C++ type system and classes, making it the most common style in C++.

But then there are not many cases in C++ when you actually want to use struct instead of class in the first place - structs are essentially classes with all members public by default.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

There are two reasons to do this.

The first one is if we are going to introduce a new type in the scope using an elaborated name. That is in this definition

void CBar::interesting()
{
    struct CFoo *pCFoo;

    // Go on to do something interesting with pCFoo...
}

the new type struct CFoo is introduced in the scope provided that it is not yet declared. The pointer may point to an incomplete type because pointers themselves are complete types.

The second one is when a name of a class is hidden by a declaration of a function or a variable. In this case we again need to use an elaborated type name.

Here are some examples

#include <iostream>

void CFoo( const class CFoo * c ) { std::cout << ( const void * )c << '\n'; }

class CFoo
{
public:
    int doStuff();
};


int main() 
{
    class CFoo c1;

    return 0;
}

Or

#include <iostream>

class CFoo
{
public:
    int doStuff();
};

void CFoo( void ) { std::cout << "I am hidding the class CGoo!\n"; }

int main() 
{
    class CFoo c1;

    return 0;
}

Upvotes: 5

Related Questions