what does “::” between ACountdown and MethodName mean?
Full cpp class below:
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#include "HowTo_VTE.h"
#include "Components/TextRenderComponent.h"
#include "Countdown.h"
// Sets default values
ACountdown::ACountdown()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
CountdownText->SetHorizontalAlignment(EHTA_Center);
CountdownText->SetWorldSize(150.0f);
RootComponent = CountdownText;
CountdownTime = 3;
}
// Called when the game starts or when spawned
void ACountdown::BeginPlay()
{
Super::BeginPlay();
UpdateTimerDisplay();
GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);
}
// Called every frame
void ACountdown::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void ACountdown::UpdateTimerDisplay()
{
CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));
}
void ACountdown::AdvanceTimer()
{
--CountdownTime;
UpdateTimerDisplay();
if (CountdownTime < 1)
{
// We're done counting down, so stop running the timer.
GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
//Perform any special actions we want to do when the timer ends.
CountdownHasFinished();
}
}
void ACountdown::CountdownHasFinished()
{
//Change to a special readout
CountdownText->SetText(TEXT("GO!"));
}
Thank you very much, sorry if it’s a newbie question.
o/
That’s a scope resolution operator defining a namespace given name belongs to
check C++ namespace concept:
So for example when you define:
void ACountdown::BeginPlay()
{
// function definition
}
it belongs to ACountdown namespace, which scope operator sets for given name.
If some name does not have defined namespace it by default belongs to global namespace
void BeginPlay() //this function belongs to global namespace
{
// function definition
}
Also keep in mind global namespace and scope often usen interchangeably are two different terms
A global name is one that is declared outside of any class, function, or namespace. However, in C++ even these names exist with an implicit global namespace. The scope of global names extends from the point of declaration to the end of the file in which they are declared.
@3dRaven You’re right, i’ve corrected my answer, question was about :: operator itself
Technically, they are correct, which is the best kind of correct
Basically, :: means the part after it is a piece belonging to the part before it. Most commonly used with classes, to signify that, for example ACountdown::BeginPlay this BeginPlay is part of the ACountdown class. Every Actor derived class may have a BeginPlay function inside it, so the language needs to know that this is the one for the ACountdown class.
If I’m understanding the first link you sent correctly. It says that To define a function outside a class.
we can use :: outside of the class declaration body.
// C++ program to show that scope resolution operator :: is used
// to define a function outside a class
#include<iostream>
using namespace std;
class A
{
public:
// Only declaration
void fun();
};
// Definition outside class using ::
void A::fun()
{
cout << "fun() called";
}
int main()
{
A a;
a.fun();
return 0;
}
So if we put
class A
{
public:
// Only declaration
void fun();
};
into a header file. then
put
void A::fun()
{
cout << "fun() called";
}
into a .cpp
That’s effectively what’s happening with ACountdown.h and ACountdown.cpp?
ACountdown Class is defined in .h, and all the method implementations are outside of the class defining body since they’re in the .cpp file instead?
Yes - that’s what’s happening with ACountdown.h and ACountdown.cpp - declaration is separated from definition, by using scope resolution operator on a function name.
You’ll notice in IDE’s that often You’ll be asked to GoToDefinition / GoToDeclaration - and when separated by files like that GoTo will either go to .h/.hpp file for declaration or .cpp for definition.
Class has a namespace scope yet namespaces and classes are different, but namespaces and classes both introduce a scope which may be referred to using the scope resolution operator ::
Namespaces simply declare a scope inside which other types, functions, objects, or namespaces can exist. Class on the other hand allows creating instances - namespaces do not.