Const functions within const functions

Hello.

I understand that when we create const functions, we should only call const functions within it. However, I’ve noticed that we call the FVector::Dist() function which is not const per se, but takes in const parameters. Is that enough to satisfy the compiler requirements?

Cheers.

1 Like

const member functions mean that it doesn’t modify this, the object executing that function. That doesn’t mean they can’t call non-const functions on other objects i.e.

struct Int
{
    int IntValue;
    void AddTen() { IntValue += 10; }
};

struct Example
{
    double ExampleValue;
    void Foo() const { Int Value; Value.AddTen(); }
};

void Bar()
{
    const Example E{ 3.14 };
    Example.Foo(); // E.ExampleValue still 3.14 after this call.
}

That’s fine because Example::Foo() doesn’t modify the object at all.

Secondly FVector::Dist is a static member function. It’s not called on any object.

1 Like

Oh I had the wrong concept then. Thanks for clarifying.

1 Like

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

Privacy & Terms