11

I recently downloaded the Shooter Game for Unreal 4 Engine and I am just trying to pick apart the c++ but my c++ isn't the best I notice a variable called

class AShooterCharacter* MyPawn; 

Set in the header file for ShooterWeapon.h

I am trying to understand what the class part is.

[Edit] I notice people downed my question so I changed it to one question. I hope people are willing to help rather then degrade my question. Theres no such thing as a dumb question :)... Especially in programming

skrrgwasme
  • 8,245
  • 11
  • 47
  • 71
numerical25
  • 9,867
  • 30
  • 120
  • 205

3 Answers3

8

If AShooterCharacter is already in scope, then it probably means basically nothing.

class AShooterCharacter* MyPawn;
// ^ the same as just:
// AShooterCharacter* MyPawn;

In C, when naming structure types, you had to use the struct keyword:

struct Foo
{
   int x, y, z;
};

struct Foo obj;

In C++, you don't need to do that because Foo becomes a nameable type in its own right:

Foo obj;

But you still can write struct Foo if you want to.

The same applies for class, just as a consequence of how the language grammar and semantics are defined.

There are only two times when it makes a difference.

Usage 1: Disambiguation (of sorts)

You can use it to specify that you want to refer to an in-scope type name when it is otherwise being hidden by some other name, e.g.:

class Foo {};

int main()
{
   const int Foo = 3;

   // Foo x;     // invalid because `Foo` is now a variable, not a type
   class Foo x;  // a declaration of a `Foo` called `x`;
}

But if you find yourself "needing" this then, in my opinion, you have bigger problems!

Usage 2: Forward declaration

Otherwise, it is the same as doing this:

class Foo;   // a forward declaration
Foo* x;

Arguably it saves a line of code if you are going to forward declare the type and immediately declare a pointer to an object of that type.

It's not common style, though.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
2

That's so called "forward declaration".

It allows you to specify a pointer to a class/struct name so that you don't have to include the particular header file defining that class/struct.

This feature is particularly viable when breaking circular dependencies between header files.

You can verify that this is the case by checking that AShooterCharacter is not defined in any files included by ShooterWeapon.h.

arul
  • 13,742
  • 1
  • 53
  • 76
0

Let's give a small introduction ( from Wikipedia )

A class is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private). A class (declared with keyword class) in C++ differs from a structure (declared with keyword struct) as by default, members are private in a class while they are public in a structure. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.

A class is used to implement Object Oriented Programming ( OOP )

By putting the keyword class before an identifier, it becomes a data type ( like int or char ) , but the good part is that you are able to decide what all are present in it. For example

class A
{
    int num;
    char ch;
  public:
    void getvalue ( )
    {
       std::cin >> num >> ch ;
    }
    void disp ( )
    {
       std::cout << num << std::endl << ch ;
    }
};

Now A is a class and when you do

A myobject ;

myobject becomes am object of the class A , that is , it becomes a data type that can store int and char values ( you can add any other data types as well , like float ) .

And you can call the function getvalue() to take the values for num and ch and then output the value like

myobject.getvalue();
myobject.disp()
Arun A S
  • 5,531
  • 3
  • 25
  • 39
  • I understand what a class is. I just don't understand why someone would put the prefix of class when its optional. What are the benefits of doing `class A myobject;` in oppose to `A myobject;` `A` is a datatype no matter what. rather you put the `class` in front of it or not. But then you said.... "the good part is you are able to decide what all are present in it".... As in you can can ignore the public and private members and methods ?? Not sure what you mean by that. – numerical25 Apr 11 '15 at 19:13
  • 1
    @numerical25 It's mainly a throwback to C which required some extra `struct` keywords in certain places sometimes. – Neil Kirk Apr 11 '15 at 19:23
  • 1
    This is a good explanation of what classes are, but you neither understood nor answered the question. – Lightness Races in Orbit Apr 11 '15 at 21:47