I still dont understand why we make a variable sometimes in C and sometimes right in C++. What is the difference?
I don’t know the course but can you give an example?
I believe the syntax is the same for both, type name = value. Maybe what you are referring to is not the same at all.
I got the information on which course and lesson the question is attached to (actually I did some of Obstacle Assault but didn’t finish it up to now).
I quite don’t see where plain C
would come into play here at all, though…
Also, for almost everything, C++
is a superset of C
, so whatever applies in C
applies just the same in C++
.
I think the question you’re actually asking might be something entirely different but at your current point of learning you’re not having the right words for it. And possibly if you did, the question would be solved already…
So, please give is a concrete example what you mean, so this riddle can be solved.
I’m asking that right from the lecture in the course about UE5. The part aka project is called “obstacle assault”. Here we have C file and C++ file in the vscode
Sure! Thank you for your support. In VScode we have these two… files, right? Codes, files, how to say
are you referring to the header? It is still part of c++
As Bryant already said, the header file exists for C++
just the same as for C
.
The main difference is that in plain C
you don’t have classes and other features from C++
…
Now that we have this out of the way, let’s try to answer the original question about where to define a variable. A lot of that has to do with scope.
Scope basically defines where a variable (or function) exists and can be used.
Let’s start with something basic, a function in C
.
void someFunc()
{
int someInteger = 1;
if( someInteger != 0)
{
int anotherInteger = someInteger * 2;
}
printf(anotherInteger); //Error!
}
So, inside that if()
you can use someInteger
because it is defined inside of someFunc
outside of the if()
.
But after that if()
block, you cannot access anotherInteger
anymore, because it was defined inside the if()
block.
the curly brackets make a block of code and what is defined inside a block is not there outside the block.
The function’s body is a block, too. All the code inside of someFunc()
can use someInteger
, but code from another function could not.
Now let’s go into object oriented programming and what C++
adds to C
.
A class is a collection of several functions (now called “methods”), and some data that belongs to it. The general idea is that you have some data (say, a an Actor), and all code that works on it is inside the class. Other code doesn’t need to know how to change an Actor’s position in the scene, there are methods for it they can call.
The content of a header file describes a class so other classes know what methods it offers. And it defines which variables (which in Unreal often are tagged with UPROPERTY
) there are. A variable inside of the class definition is known to every method in the class. That’s why you define them in the header file, as part of the class.
// header "SomeClass.h"
class SomeClass
{
int someValue;
public:
int SomeMethod( int someParameter);
};
//////////////////////////////
// implementation "SomeClass.cpp"
#include "SomeClass.h"
int SomeClass::SomeMethod(int someParameter)
{
int SomePrivateVariable = someValue;
// now do some calculations that involves the parameter,
// and store them in the private variable
// at the end keep the result and also return it
someValue = SomePrivateVariable;
return someValue;
}
The C and C++ icons in VS Code are just the editor telling you what kind of file it is based on the file extension (.h /.cpp). We aren’t sometimes coding in C and sometimes in C++, that’s just a feature of the editor.
C uses headers files and uses the .h file extension
C++ uses header files and uses the .h file extension; some developers prefer to use .hpp, but both are used in the C++ community, Epic obviously uses .h
C does not use .cpp at all for its source files.
So that’s why you’re getting those icons. If you were to rename the .h to .hpp then it would show up with a purple C++ icon as only C++ uses that.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.