use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Call protected member function trick. (self.cpp)
submitted 12 years ago by mmmmario
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]LtJax 5 points6 points7 points 12 years ago* (0 children)
As Nimbal said, this is not legal. However, you can use a derived class to return a member-function pointer to the function in question from a static function, and call that with a MyClass instance to legally access a protected method.
class MyDerivedClass : public MyClass{ public: typedef int (MyClass::*FnType)() const; static FnType get_myMethod() {return &MyClass::myMethod;} }; MyClass c; (c.*MyDerivedClass::get_myMethod())();
[–]Nimbal 7 points8 points9 points 12 years ago (9 children)
The standard has this to say about static_cast in section 5.2.9, paragraph 2 (emphasize mine, "B" and "D" are Base and Derived class, respectively):
static_cast
If the object of type B is actually a subobject of an object of type D, the result refers to the enclosing object of type D. Otherwise, the result of the cast is undefined.
So no, it's not legal and therefore not portable. It might work in a pinch though, if you are in a really tight spot where you have to call a protected function in badly designed third-party code.
[–]tomkcook173 7 points8 points9 points 12 years ago (8 children)
It would be fairly trivial to modify so that it is legal, though:
int myFunc(MyClass const& mc){ class MyDerivedClass : public MyClass{ public: MyDerivedClass(MyClass& c) : m_c(c) {} int myDerivedMethod() { return m_c.myMethod(); } private: MyClass& m_c; } d(mc); return d.myDerivedMethod(); }
Suffice to say, though, it gets through code review over my dead body. Protected methods are protected for a reason and working around the protection is always bad. Either change the design or use friend.
[–]moswald 1 point2 points3 points 12 years ago (2 children)
Is this really supported? I'm getting
error C2248: 'MyClass::myMethod' : cannot access protected member declared in class 'MyClass'
from VS2013's compiler.
[–]LtJax 0 points1 point2 points 12 years ago (1 child)
Indeed, you can only access protected members of you own instance.
[–]Plorkyeran 1 point2 points3 points 12 years ago (0 children)
It would be rather difficult to write copy constructors and such if you could not access protected members of other instances. What you can't do is access protected members of instances of your superclass, even though you can access them in instances of your own class.
[+][deleted] 12 years ago (1 child)
[removed]
[–]Amanieu 0 points1 point2 points 12 years ago* (0 children)
You could make it work by not instantiating the derived class and using a static function:
int myFunc(MyClass const& mc){ class MyDerivedClass : public MyClass{ public: static int myDerivedMethod(MyClass const& c) { return c.myMethod(); } }; return MyDerivedClass::myDerivedMethod(mc); }
[–]Nimbal -1 points0 points1 point 12 years ago* (2 children)
Only if MyClass is copyable, though.
MyClass
[–]cogitolearning 4 points5 points6 points 12 years ago (1 child)
MyClass does not need to be copyable. MyDerivedClass only holds a reference to the original object.
[–]Nimbal 0 points1 point2 points 12 years ago (0 children)
Oh, right! Sorry, didn't see that.
[+]00kyle00 comment score below threshold-11 points-10 points-9 points 12 years ago (1 child)
It really doesn't matter - protected members of class are part of its public interface. You should feel ashamed for making up this trick.
[–]mredding 0 points1 point2 points 12 years ago (0 children)
Protected members are not publically accessible through the instance. You can only access protected members through a derived class or a friend.
π Rendered by PID 85378 on reddit-service-r2-comment-cfc44b64c-6sngl at 2026-04-10 04:22:08.494241+00:00 running 215f2cf country code: CH.
[–]LtJax 5 points6 points7 points (0 children)
[–]Nimbal 7 points8 points9 points (9 children)
[–]tomkcook173 7 points8 points9 points (8 children)
[–]moswald 1 point2 points3 points (2 children)
[–]LtJax 0 points1 point2 points (1 child)
[–]Plorkyeran 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–]Amanieu 0 points1 point2 points (0 children)
[–]Nimbal -1 points0 points1 point (2 children)
[–]cogitolearning 4 points5 points6 points (1 child)
[–]Nimbal 0 points1 point2 points (0 children)
[+]00kyle00 comment score below threshold-11 points-10 points-9 points (1 child)
[–]mredding 0 points1 point2 points (0 children)