Suggestion - Avoid shadowing

This will compile:

	UFUNCTION(BlueprintCallable, Category = "timstuff")
		void SetMover(UCOMP_Mover* Mover);

	UPROPERTY(EditAnywhere, Category = "timstuff")
		UCOMP_Mover* Mover;

Where as the following will not:

UPROPERTY(EditAnywhere, Category = "timstuff")
		UCOMP_Mover* Mover;

	UFUNCTION(BlueprintCallable, Category = "timstuff")
		void SetMover(UCOMP_Mover* Mover);

Visual Studio Community Edition 2022 throws an error and wont compile:

Severity	Code	Description	Project	File	Line	Suppression State
Error		Function parameter: 'Mover' cannot be defined in 'SetMover' as it is already defined in scope 'ObjectProperty /Script/CryptEscape.TriggerComponent:Mover' (shadowing is not allowed)	CryptEscape	M:\UnrealEngineProjects\CryptEscape\Source\CryptEscape\TriggerComponent.h	43	

Google says:
https://www.learncpp.com/cpp-tutorial/variable-shadowing-name-hiding/

This feels to me like something that should either be addressed in this video or perhaps we should not use shadowing.

I’ve never encountered this error before so I might be misunderstanding the issue a little.

The name of the parameter on the declaration of the function is completely irrelevant and ignored by the compiler. It’s only useful for readers and IntelliSense’s autocompletion.

MSVC and other compilers provide shadowing warnings which Unreal has enabled and treated as an error.

Demo: Compiler Explorer
/we is how to enable a warning and treat it as an error with MSVC. C4458 is MSVC’s warning for shadowing a member variable. Note how the name of the parameter name in the declaration doesn’t trigger the warning, because it’s completely unused there.

I think shadowing was mentioned in a previous lecture? Though could do with repeating either way. I’ll forward that on to Sam, thanks.

1 Like

Thanks mate - i dont recall shadowing being mentioned but i could be wrong also.

OMG awesome - ive not seen compiler explorer before - but i saw it briefly today for the first time on someone’s youtube channel.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms