user1908181
user1908181

Reputation: 175

What are the repercussions of redefining access modifiers?

What would be some possible repercussions of redefining C++ access modifiers via preprocessed commands for the sake of inducing C#/Java like syntax?

#include <iostream>

// The access modifiers are redefined here.
#define public public:
#define protected protected:
#define private private:

class Halo
{
    public Halo(int xx)
    {
        x = xx;
    }

    public int getX()
    {
        return x;
    }

    private int x;
};

int main()
{
    Halo* halo = new Halo(3);

    std::cout << halo->getX();

    return 0;
}

Upvotes: 3

Views: 148

Answers (3)

Mark B
Mark B

Reputation: 96291

Anything could happen as redefining a language keyword is undefined behavior.

Generally speaking you should write idiomatic code for the language you're using. If you want to use Java/C# syntax just write your code in those languages.

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129504

Aside from the already mentioned consequence of syntax errors. Doing this would get other people confused. A long time ago, people would program in Pascal, and then when moving to C use #define BEGIN { and #define END }, which led to code that sort of looked like pascal, but of course wasn't at all like pascal in many other ways.

You are programming in a different language. So why pretend that it isn't.

I guess it's because you think it's Java that you forgot to delete your halo object as well?

Upvotes: 3

David G
David G

Reputation: 96845

class B : public A {};

expected '{' before ':' token
expected unqualified-id before ':' token
expected class-name before ':' token

Upvotes: 9

Related Questions