2

Is it possible to change the class attribute at run-time in C++ language.For example as below :

class base
{
    public:
        //public members

    private : 
        //private members
};

class derived1 : attribute base
{
    public:
        //public members
        base::<base method name> //so that it an be made accessible from the main - outside the class.

    private:
        //private members
};

can the attribute-public,private,protected be changed at runtime, dynamically?

Rgds, softy

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Raulp
  • 7,758
  • 20
  • 93
  • 155
  • In your example, a protected method becomes public at compile time, not at runtime. In general, private/protected/public distinction in C++ vanishes by runtime, because the language does not allow *reflection*. This renders your question meaningless, unless you meant something entirely different. – Sergey Kalinichenko May 09 '12 at 12:10
  • Tell us why you want this, and we can show you a better way. – John Dibling May 09 '12 at 13:43
  • I cant tell you in detail but consider this high level thought. A game character is trying to access the powers(a class functions) , but he can access them only when he gains X(some power1 class;s functions) , if he tries to access the power w/o gaining power1 he will not be allowed (not be allowed to inherit power unless he hass power1),To the point changing the attribute of power once power1 is inherited so that its functions can be accessed(power can be used).Its messy thought but I hope you would have got the idea.have you – Raulp May 09 '12 at 14:13

3 Answers3

4

It is the compiler that makes sure you don't access private members. Once the compiler finishes its work and the binary code is generated, all information regarding private-ness is lost.

So no, you can't change that in runtime.

I don't know why you would want this, but if you want some functions to be able to be called during some times, but not the others, you can have a variable defining whether they can be called or not. Then, on the top of that function:

int Class::function(...)
{
    if (!function_is_allowed)
        return OPERATION_NOT_ALLOWED;
    ...
}
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
2

No, the access level cannot be modified, although there are some hacks to go around them.

Refer to this answer - https://stackoverflow.com/a/6886432/673730

If what you're looking for is something similar to Java reflection, where you can access private members by modifying their access level at runtime, then no.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

You cannot change the access modifiers of a class. End of story.

Disclaimer: There are hacks for just about everything, including this. Don't use them.

Based on your comments in the question when asked why you want this, it looks like what you're trying to do is control access to a class' run-time properties based on its other run-time properties. For example, maybe a Character's Powers are only accessible if Character's Level is >= 42.

This is not a technical question about the mechanics of C++ syntax, but a business logic question. You'll find the answer to this question in the design of your program and its algorithms -- not some technical C++ trick.

Classes are often used to model things. In your case, a character in a game. Maybe this character has a level and a list of powers (which I'll represent simply as strings).

In that case:

class Character
{
public:
  int level_;
  vector<string> powers_;
};

...is a simplistic representation of your character model. Now, if you want to control access to powers_ at run-time based on the value of level_, you can use an accessor method:

class Character
{
public:
  int level_;
  vector<string> Powers() const 
  {
    if( level_ >= 42 )
      return powers_;
    else
      return vector<string>();
  }
private:
  vector<string> powers_;
};

Now you can only get to the character's powers if the character is of sufficiently high level.

This is still a highly simplistic example, and the above code is not production quality. However, the idea is there -- when implementing your program's business logic, your focus should be on the algorithms you write much more than the technicalities of C++, or whatever language you're using.

John Dibling
  • 99,718
  • 31
  • 186
  • 324