Error C2248 Cannot Access Protected Member
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About protected constructor Us Learn more about Stack Overflow the company Business Learn more about hiring
Error C2248 Cannot Access Private Member Declared In Class
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the error c2248 cobject cobject cannot access private member declared in class cobject Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why can't I access a error c2248 cobject operator cannot access private member declared in class cobject protected member from an instance of a derived class? up vote 11 down vote favorite 4 I haven't done C++ in a while and can't figure out why following doesn't work: class A { protected: int num; }; class B : public A { }; main () { B * bclass = new B (); bclass->num = 1; } Compiling this produces: error C2248: 'A::num' :
Error C2248 C++
cannot access protected member declared in class 'A' Shouldn't protected members be accessible by derived classes? What am I missing? c++ inheritance share|improve this question edited Jun 8 '09 at 22:41 Shog9♦ 108k28184217 asked Jun 8 '09 at 22:32 Roman M 1,38162036 add a comment| 8 Answers 8 active oldest votes up vote 27 down vote accepted yes protected members are accessible by derived classes but you are accessing it in the main() function, which is outside the hierarchy. If you declare a method in the class B and access num it will be fine. share|improve this answer answered Jun 8 '09 at 22:35 oscarkuo 6,70743556 Thank you! I see my mistake now –Roman M Jun 8 '09 at 22:41 add a comment| up vote 11 down vote Yes, protected members are accessible by the derived class, but only from within the class. example: #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn cannot access protected member declared in class c# more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags cannot access protected member c++ Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you,
Accessing Protected Variables C++
helping each other. Join them; it only takes a minute: Sign up error C2248 : cannot access protected member declared in class up vote 2 down vote favorite I have a problem with protected constructor of base class http://stackoverflow.com/questions/967352/why-cant-i-access-a-protected-member-from-an-instance-of-a-derived-class which is used in private function of derived class: Base class: class Socket { public: virtual ~Socket(); // Constructors : Socket(); protected: Socket(SOCKET& s); }; Derived class: class Server : public Socket { public: Server(); ~Server(); private: int ServerLoop(); }; I try to create Socket object in ServerLoop function SOCKET client_sock = accept( m_socket, ( sockaddr* )&client_addr, &size ); Socket* Client = new Socket( client_sock ); ^^^^^^ But i get this error: error C2248: 'NET_SOCKETS::Socket::Socket' : cannot http://stackoverflow.com/questions/23714078/error-c2248-cannot-access-protected-member-declared-in-class access protected member declared in class 'NET_SOCKETS::Socket' in the line over the ^^^^. What is causing that error? c++ constructor protected share|improve this question edited Jan 15 '15 at 14:34 Elisha 2,41121335 asked May 17 '14 at 17:14 user3648087 1112 First, ask yourself if you really need to use dynamic memory. This may go away or give you a better clue into your design issues. –Thomas Matthews May 17 '14 at 17:24 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote It was a bit surprising to me since it's a constructor, but it's the same principle as for non-static member functions. Protected base class non-static member functions can only be called on instances of the derived class (or derived from that class again), because otherwise one could gain access to protected features of any derived class simply by deriving from the base. A workaround is to do exactly that, deriving a class specifically for the purpose of calling the protected base class constructor. E.g., replace Socket* Client = new Socket( client_sock ); with struct DSocket: Socket { DSocket( SOCKET const socket ) : Socket( socket ) {} }; Socket* Client = new DSocket( client_sock ); share|improve this answer edited May 17 '14 at 21:52 answered May 17 '14 at 17:23 Cheers and hth. - Alf 102k7104208 add a comm
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers https://msdn.microsoft.com/en-us/library/tsbce2bh.aspx Retired content Samples We’re sorry. The content you requested has been removed. You’ll http://www.gamedev.net/topic/543773-solved-c-accessing-a-protected-member-of-the-same-class-from-a-different-instance/ be auto redirected in 1 second. C/C++ Building Reference C/C++ Build Errors Compiler Errors C2200 through C2299 Compiler Errors C2200 through C2299 Compiler Error C2248 Compiler Error C2248 Compiler Error C2248 Compiler Error C2200 Compiler Error C2201 Compiler Error C2203 Compiler Error C2204 Compiler Error C2205 Compiler Error C2206 Compiler Error cannot access C2207 Compiler Error C2208 Compiler Error C2212 Compiler Error C2213 Compiler Error C2216 Compiler Error C2217 Compiler Error C2218 Compiler Error C2219 Compiler Error C2220 Compiler Error C2222 Compiler Error C2223 Compiler Error C2224 Compiler Error C2226 Compiler Error C2227 Compiler Error C2228 Compiler Error C2229 Compiler Error C2231 Compiler Error C2232 Compiler Error C2233 Compiler Error C2234 Compiler Error C2236 Compiler Error C2238 member declared in Compiler Error C2241 Compiler Error C2242 Compiler Error C2243 Compiler Error C2244 Compiler Error C2245 Compiler Error C2246 Compiler Error C2247 Compiler Error C2248 Compiler Error C2249 Compiler Error C2250 Compiler Error C2251 Compiler Error C2252 Compiler Error C2253 Compiler Error C2254 Compiler Error C2255 Compiler Error C2256 Compiler Error C2258 Compiler Error C2259 Compiler Error C2261 Compiler Error C2262 Compiler Error C2264 Compiler Error C2266 Compiler Error C2267 Compiler Error C2268 Compiler Error C2270 Compiler Error C2271 Compiler Error C2272 Compiler Error C2273 Compiler Error C2274 Compiler Error C2275 Compiler Error C2276 Compiler Error C2277 Compiler Error C2279 Compiler Error C2283 Compiler Error C2285 Compiler Error C2286 Compiler Error C2287 Compiler Error C2289 Compiler Error C2290 Compiler Error C2292 Compiler Error C2293 Compiler Error C2295 Compiler Error C2296 Compiler Error C2297 Compiler Error C2298 Compiler Error C2299 TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Compiler Error C2248 Visual Studio 2015 Other Versions Visual Studio 2013 Visual Studio 2012 Visual Studio 2010 Visual Studio 2008 Visual Studio 2005 Vi
topic ForumsMembersHelp Files Developer Journals Gallery Calendar Downloads Resources Store Classifieds Tracker Links Home For Beginners Articles All Articles Post an Article Technical Game Programming General Programming Graphics Programming and Theory DirectX and XNA OpenGL and Vulkan Multiplayer and Network Programming Artificial Intelligence Math and Physics Mobile Development Middleware, Libraries, and Tools Virtual and Augmented Reality Creative Game Design Music and Sound Visual Arts Business Breaking into the Industry Production and Management Interviews Business and Law Event Coverage Forums All Forums Technical Game Programming General Programming Graphics Programming and Theory DirectX and XNA OpenGL & Vulkan Multiplayer and Network Programming Artificial Intelligence Math and Physics Mobile & Console Development Build Systems & Source Control Middleware, Libraries, and Tools Virtual and Augmented Reality Creative Game Design Writing For Games Music and Sound Visual Arts Gallery Business Breaking into the Industry Business & Law Production & Management Community GDNet Lounge Coding Horrors Article Writing Comments, Suggestions and Ideas Your Announcements Hobby Project Classifieds Indie Project Showcase Community Developer Journals GDNet+ Subscriptions Gallery Store Classifieds Jobs Freelancers Hobby Projects Marketplace Newsletter » Home » Forums » The Technical Side » General Programming Chat Watched Content New Content 0 [SOLVED] C++ Accessing a protected member of the same class from a different instance Started by Matias Goldberg, Aug 08 2009 06:40 AM Old topic! Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic. You cannot reply to this topic 3 replies to this topic #1 Matias Goldberg Crossbones+ -Reputation: