[Questions] Noob C++ questions

Today i finally downloaded ShooterGame and i decided to try C++ :slight_smile:

Since this is my very first time messing with C++ the whole thing kinda confuses me, so i thought its better to ask here. :slight_smile:

1: In some of the .h files i see some symbols like “*” and “&”. What do they mean? Eg: class AActor* GetAttachmentRootActor() const;. See the “multiply/star” sign after AActor. Like that what does the “&” mean in this: const FTransform& NewRelativeTransform

2: In UnrealScript we had simulated function/event etc. How does that work now in C++? Is it just “void”?

3: What is the difference between “virtual void” and “void”?

4: Am not understanding the pipeline between .h and .cpp. I know they are header and source files but how do they work together? So far what i see is functions being implemented in .cpp and properties and some “voids” in .h file. What is .h used for? I really appreciate if someone could briefly explain this question.

5: I added the macro UFunction() on one of the void in .h file. I pressed Compile in Rocket and it showed Compile Complete in a green box. However i cant find this in Blueprint. What i had to do was exit Rocket and then select VS → Rebuild Solution. Now when i start Rocket the function shows up in Blueprint. Is this the intended workflow?

  1. This should help you out: http://www.cplusplus.com/doc/tutorial/pointers/

  2. With C++, you now are declaring methods inside of classes. So you have to go into a header file to declare your functions, and then define your functions in the corresponding CPP file. So in ExampleAClass.h, you would declare it, and then in ExampleAClass.cpp, you would define it.

  3. Virtual means that the function can be overridden in an inherited class. In unrealscript, unless you had “final”, you could do this automatically, but in C++ you specifically have to specifically tell the function that it can be overridden.

  4. Header declares class methods (class functions), and CPP files define them. In the case of C++ working with UE4 architecture, you have some special treatment, such as UFUNCTION and GENERATED_UCLASS_BODY() that provides a means to link your code with Rocket’s native architecture.

  5. Have a look at my earlier thread asking some of the very same questions, here: https://rocket.unrealengine.com/questions/2314/ue4-beginner-c-questions.html

All that should be a great way to get you started. Good luck!

  1. in C++ The star operator * refers to a pointer declaration in that the operand declared after it is a pointer to that data type, I haven’t checked out rocket yet so I’m giving examples using provided C/C++ data types. For instance int* pInt - in this case pInt is a pointer to an integer - the same operator can be used again to dereference the pointer and access or modify the value held in this memory location provided that the pointer points to a writable memory location. The ampersand is the address-of operator - essentially when you need to get the memory address of an operator you add the address-of operator right before it, hence if you want pInt to point to an integer called nInt, you assign pInt to the memory location of nInt like so pInt = &nInt, in this context it is used as a reference, it’s like a pointer but less powerful.

  2. The simulated and event keyword are not C++ keywords - that’s implemented in Unreal Engine and has nothing to do with the language itself, according to the documentation a UFUNCTION can be used to enable certain specifiers. void denotes a return type, in unrealscript “function” is used, it denotes a function that returns no value to the caller.

  3. virtual void denotes a virtual function of return type void, whereas void simply declares a non-virtual function that returns no value to the caller, a virtual function’s implementation can be overriden in a derived class whereas a non-virtual function cannot - think of normal functions as virtual in Unrealscript and final functions as non-virtual.

4: A .h file is a header file that’s usually where functions are declared etc think of the header file as a declaration and the .cpp file as the implementation so in essence the header file consists of the function declaration and the .cpp file consists of the implementation of those functions - including said header file in another .cpp file would include the implemented function.

5: As for the last question I simply cannot answer as I haven’t tried Rocket yet.

Thank you very much everyone :). That .h <–> .cpp relation was the most confusing to me. Now i understand it. :slight_smile:

1: * means it is a pointer to the object, not the object itself. & means the address of the object. It’s called passing by reference with that example you gave.

2: I don’t know.

3: the void is the return type. Virtual means it is meant to be overridden in your subclass, you “generally” put virtual before your redefinition in your subclass, then it will use the subclass version.

4: Class Declarations go in the .h file, the Definitions go in the .cpp file. It is good form to keep them separate. The form the #includes required in UE4 are a little more specific than in general C++.

5: I think you have to recompile the code in Visual Studio, then launch the Editor to see the changes to the Blueprint, but I’m not positive. I saw some initial demo stuff where it looked like you could recompile on the fly, but I’m not convinced that is real. I’m not sure.

Bah, looks like you beat me to it. Oh well :p.