Skip to main content
gamedev.net
🔒 Locked

C++ access redeclaration

Started by Ashkan 3 replies 700+ views
Original Post
Ashkan
Ashkan
OK, I've been ignoring this problem for such a long time I got afraid C++ would be grandpas' language by the time I dare to find the solution. What is the point of access specification if they all loose their effect on inheritance? Here is some code to clarify my point:

class Base {
public:
   int GetX() { return x };
protected:
   int x;
};

class Derived: public Base {
// x is public here, not good
};

class Derived2: protected Base {
// GetX() is protected here, not good again
};
There are circumstances, one may want to restore one or more inherited members to their original access specification. Herbert Schildt in his book, C++ The Complete Reference, states that there are basically two approaches to this problem: 1) The now deprecated base-class::member method, 2) The 'using' keyword And as it turns out, I really can't help myself to like any of these. I really don't like to keep an accumulated list of all redeclared class members and add them to each new inherited class on each level of my inheritance hierarchy. I wish there was some way to keep the access specification on inheritance, something that should have been supported by the language in my opinion, but is not. Isn't this a sensible and logical expectation? Also I read access redeclaration can cause shadowing problems, so now I'm thinking maybe my design is flawed after all and I shouldn't be so harsh on enforcing encapsulation. I'm so lost here. I would greatly appreciate your helps and ideas. Thank you.
ToohrVyk
ToohrVyk
Quote:
Original post by Ashkan
class Derived: public Base {// x is public here, not good};



x isn't public in the example above, it's still protected.
Ashkan
Ashkan
Quote:
Original post by ToohrVyk
Quote:
Original post by Ashkan
class Derived: public Base {// x is public here, not good};



x isn't public in the example above, it's still protected.


Thank you, that was a relief.
Zahlman
Zahlman
Quote:
Original post by Ashkan
Herbert Schildt in his book, C++ The Complete Reference


Well, that largely explains it, then :)

(The 'using' keyword and base::member approaches are still useful when dealing with overloads and "hiding", but I've never heard anyone else suggest that base::member is deprecated.)
Ashkan
Ashkan
Quote:
Original post by Zahlman
Quote:
Original post by Ashkan
Herbert Schildt in his book, C++ The Complete Reference


Well, that largely explains it, then :)

(The 'using' keyword and base::member approaches are still useful when dealing with overloads and "hiding", but I've never heard anyone else suggest that base::member is deprecated.)


lol, bullshildt. That's funny. I personally never knew his book is so criticized. Thank you Zahlman.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.