x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

What is the purpose of the 'override' keyword in header declarations?

Hello! After creating my first Actor class, I notice in the header file, that the BeginPlay() and Tick() functions are set up in a bizarre way.

virtual void BeginPlay() override;

for instance.

I've never seen a virtual function declaration with the override keyword directly in it, and was wondering if someone could explain how/why this is included? As far as my C++ knowledge goes, you set up a pure virtual function in your base class declaration, and the DERIVED classes may contain the override keyword.


Something like this:


class BaseClass{
public:
virtual void doSomething(){cout<<1;}
};

class DerivedClass: public BaseClass{
public:
void doSomething() override{cout<<2;}
}


in your main code, you could call the function and it would print 2, rather than 1. This I understand.


The way UE4 seems to have it set up, though, just looks different and is confusing to me in how it would operate, or why it is declared that way.

I know that my wording is rather piss-poor, and not very technical. I apologize for this and ask that you please bear with me. This has perplexed me all evening, searching through countless tutorials and sifting through the documentation to no avail.


If you've even taken the time to read this, Thank you; I appreciate any answers!

Product Version: Not Selected
Tags:
more ▼

asked Nov 12 '17 at 04:58 AM in C++ Programming

avatar image

Ke9an
65 2 5 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

You are correct in your understanding how the override keyword should be used.

Are you referring to the code automatically added to your class when it's generated? If so, the override keyword is used because you are overriding Actor's versions of the functions. Your class inherits from Actor.

This post explains it pretty well: https://stackoverflow.com/questions/39932391/virtual-override-or-both-c

more ▼

answered Nov 12 '17 at 05:31 AM

avatar image

staticvoidlol
2.6k 95 41 111

avatar image Ke9an Nov 12 '17 at 06:48 AM

THIS.

Thank you so, so much! This is exactly what I was referring to, and now I believe I understand! Thank you for linking extra reading for me as well, always appreciated :D

Cheers!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Technicly speaking you don´t need virtual or override in the derived class to actually override the Function. It overrides just fine aslong the basclass has the virtual Keyword.

Those Keywords make sure in case you change the baseclass to be none virtual or finalize a virtual function to throw you a Compile Time Error since there would be no functionality anymore to override. It makes spotting Bugs related to that easier since you get a Error beforehand and don´t have to figure out why your Function all of a sudden does not execute anymore. Aswell as making it easier for you to know that you override something when looking at the definition.

The override Keyword alone should be enough if you preffer it that way.

more ▼

answered Nov 12 '17 at 06:01 AM

avatar image

Nachtmahr
6.9k 143 26 172

avatar image Ke9an Nov 12 '17 at 06:47 AM

Thank you for the explanation!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question