Private members of a class are being used in the derived class. how?

hello,there is something in ue4 that is popping at my mind and i have no answer for it.in c++ we know that private members or member functions of a class cannot be inherited right ? but in a project it seems the child is using it.i have a class called TankPlayerController in c++ which is the parent of TankPlayerController_BP which is a blupring class as you see in the name. in TankPlayerController.h i have delcared some functions as below:
private:
void AimTowardsCrosshair();
ATank* GetControlledTank()const ;
const bool GetSightRayHitLocation(FVector&);
const bool GetLookDirection(FVector2D ScreenLocation, FVector&);
UPROPERTY(EditAnywhere)
float CrossHairXLocation = .5;
UPROPERTY(EditAnywhere)
float CrossHairYLocation = .3333;
float LineTraceRange = 1000000;
const bool LineTraceAndFindBlockingHit(FVector LookDirection, FVector &HitLocation);
i have implemented their functionality in TankPlayerController.cpp and actually they are being used by derived class in game.
but how can they be used by the derived class ?i mean they are being used clearly and i don’t know the reason can anyone. i hope i could express what i mean.can anyone explain me ?
thanks in advance!

They are inherited from the base class

You haven’t shown you using any of those in blueprint.

Side note

const bool GetSightRayHitLocation(FVector&);
const bool GetLookDirection(FVector2D ScreenLocation, FVector&);
const bool LineTraceAndFindBlockingHit(FVector LookDirection, FVector &HitLocation);

All of these have const on the wrong side.

i know about const keyword now but as they haven’t given me any errors i haven’t changed them. these line of codes given below are available in blueprint details tab which means the derived class has inherited these.
UPROPERTY(EditAnywhere)
float CrossHairXLocation = .5;
UPROPERTY(EditAnywhere)
float CrossHairYLocation = .3333;
and in inheritance we know that private members cannot be inherited right ? then how is it possible that they are available in blueprint class details?

They should be const functions, just because it doesn’t generate errors doesn’t mean it’s not a good thing to do. Also returning by const value prevents move semantics which I won’t get into but it’s something you don’t want to inadvertently block.

Incorrect, they are inherited you just can’t access them.

It’s not. That is the editor not blueprint. Try accessing them in the graph.

1 Like

yes they are not.i also have a tank-BP and a Tank c++ class.in tank.h i have a method like below and the BP class is derived from tank c++ class:
public:
void AimAt(FVector HitLocation);
then why when i try to write the name of this function in event graph there is nothing there ?

Because they need to be UFUNCTION's, to have functions or variables accessible in blueprint they need UFUNCTION or UPROPERTY respectively. In that case it would need UFUNCTION(BlueprintCallable)

1 Like

then whats exactly use of them being inherited ? when we cannot access them

The same reason why you would have private members without inheritance involved. Just because you can’t access them doesn’t mean they aren’t being used internally by the class.

y i had a question about this too when we use a function in for example BeginPlay() in c++ class the code is being used for the derived class in blueprint as well but i dunno why is it like that.its because an internal link which we are not aware of right ? like i’m using GetControlledTank() in c++ parent class but that c++ actually doesn’t have any pawn in the editor but its the child that has a pawn there.so it means something is happening behind the scenes right ?
(sorry that i’m bothering you so much with my questions since 2 days ago).

Super::BeginPlay calls the blueprint’s Begin Play.

Could you rephrase or expand on this? I’m not sure what you mean.

Don’t worry about it :slight_smile:

this is my TankPlayerController.h :

class BATTLETANK_API ATankPlayerController : public APlayerController
{
	GENERATED_BODY()
public:
		virtual void BeginPlay() override;
		virtual void Tick(float DeltaTime) override;
private:
 ATank* GetControlledTank()const ;
,,,
and here is .cpp file:

void ATankPlayerController::BeginPlay() {
auto ControlledTank = GetControlledTank();
if (!ControlledTank) {
UE_LOG(LogTemp,Warning, TEXT(“PlayerController is not possesing a tank”));
}
else {

	UE_LOG(LogTemp, Warning, TEXT("PlayerController is possesing: %s"), *(ControlledTank->GetName()));
}
Super::BeginPlay();

}
ATank* ATankPlayerController::GetControlledTank() const{

return Cast<ATank>(GetPawn());

}

so we are coding in c++ and in the parent class(the derived class is a BP type which is the TankPlayerController).look,BeginPlay() and GetcontrolledTank() methods are called when we have a PlayerController or better to say an instance of PLayerController.c++ type in the game right ?but we actually don’t have any instance of this parent class in the game.rather we have an instance of the derived class which is playing the role of the Tank’s controller. so why are these methods get called when we have an instance of derived class ? is it like this c++ class is recognizing the TankPlayerController_BP object as its own ?
i don’t know if i could say it explicitly or not but i hope you can understand what i mean.

Could you please format your code. Surround them with backticks ` e.g.
```
code
```

Because of inheritance.

so whenever we have inheritance as we are writing things in the base class its actually working for both base and derived class. but i don’t think its like that in normal c++ is it ?.if it is could you provide an example pls so i can understand it. thanks.

1 Like

yes i know about this issue. you know it is like you have invoked that function 3 times and it gave you three different things right ?what i mean is we are calling GetControlledTank() in base class but its being called in dervied class too automatically, we haven’t invoke this function in derivedd class event graph or any other place but its being executed for that too.

  1. Class C didn’t override the function therefore just does what class B did.

I think you’re overlooking BeginPlay and Tick which is called by the engine in both cases.

so that’s the engine which is executing this method for both derived and base class.in GetControlledTank() we are returning the value like this :

return  Cast<ATank>(GetPawn());

what happens if we just return GetPawn()? I mean we don’t use that Cast keyword
there was another guy who asked this question but i couldn’t understand the answer.

To be clear, GetControlledTank is not directly called by the engine, only indirectly through functions called through either BeginPlay or Tick

  1. That’s not a keyword
  2. Could you make a separate thread for that so it can be found by others?

yes i could also just memorize that everything i write in c++ is just for its derived class in BP i just wanted to know the reason and was curious.though I should just accept it as it is.

sure i will do it in a minute.

Privacy & Terms