What is default access modifier by generated macro?

Hello, forks.

I know cpp default class access modifier is private.

What I doubt is generated macro. I can access some functions which are not defined access modifier(so, I thought its private) in AGameMode such as StartMatch(), HasMatchStarted(), etc on its child class by Super keyword.

For example,

class CELLPORTAL_API ACellPortalGameMode : public AGameMode
{
    GENERATED_BODY()
    virtual void StartMatch();
    /* some code */
}

void ACellPortalGameMode::StartMatch()
{
	if (!HasMatchStarted())
	{
		// do something
	}
	Super::StartMatch();
}

However, I cannot access in below case. In this case, the compiler complaints for access violation error.

class CELLPORTAL_API ACCharacterBase : public ACharacter
{
    GENERATED_BODY()
    virtual void AccessTest() { /* code */  }
}

class CELLPORTAL_API ACCharacterJoe : public ACCharacterBase
{
	GENERATED_BODY()
	virtual void AccessTest() override { Super::AccessTest(); }
}

error C2248: ‘ACCharacterBase::AccessTest’ : cannot access private member declared in class ‘ACCharacterBase’

I tried to find what is difference, then I found out AGameMode includes GENERATED_UCLASS_BODY() macro at the first line, but others use GENERATED_BODY(). When I changed GENERATED_BODY() to GENERATED_UCLASS_BODY(), it complied (but occurred some link error for constructors).

So, I guess the auto generated macro would affect default access modifier, but I cannot go further. Does anyone have an idea about it?

Thanks! You cleared up my doubt.

Actually, GENERATED_BODY does not change the access modifier at all. So:

UCLASS()
class UThing : public UObject
{
public:
    GENERATED_BODY()

    void ThisIsPublic();
};

UCLASS()
class UThing : public UObject
{
private:
    GENERATED_BODY()

    void ThisIsPrivate();
};

The old GENERATED_UCLASS_BODY() used to change it to public in all cases.

However, I’ve just discovered that if you omit the specifier entirely then it makes everything following it public:

UCLASS()
class UThing : public UObject
{
    GENERATED_BODY()

    void ThisIsPublic();
};

This is a bug. I’ll investigate the feasibility of fixing it (it’s a trivial fix, but it might affect a lot of existing code).

Steve

Actually, it’s slightly worse than that, as the access is falling through from one class to the next. We’ll definitely get this fixed:

UCLASS()
class UThing1 : public UObject
{
public:
    GENERATED_BODY()

    void ThisIsPublic();
};

UCLASS()
class UThing2 : public UObject
{
    GENERATED_BODY()

    void ThisIsAlsoPublic();

private:
    void ThisIsPrivate();
};

UCLASS()
class UThing3 : public UObject
{
    GENERATED_BODY()

    void ThisIsAlsoPrivate();
};

Steve

////
the following is a message from Omnicypher:
speaking of privacy issues, i just gained the power to edit anyone’s posts for some reason… whether its a question or comment, it seems anything on the answerhub has an edit button for me. im not sure who i should report this too, but this is Omnicypher, editing Steve Robbs comment. I hope this isn’t a glitch where everyone can do this… I’m not sure how I should report this in a way that most people wont realize they have the power to edit anything… so i hope this message gets to the right person so this can be fixed.

Wow! What a quick support. Thanks a lot, Steve.

hi. please read your comment, it has some new information that might be helpful.

Don’t rely on the generated macro to provide access modifiers. Add them yourself.

You get your error because all class members are private by default.