Jump to content
  • Advertisement
Sign in to follow this  
HugosHoH

C++ Private Members

This topic is 3807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Why does this compile successfully under VS 8 & 9? class AAA { private: int a; void foo1(); }; AAA m1; void AAA::foo1() { m1.a = 10; } void main() { } I thought private members are only accessible thorough their object. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Member functions have access to private members of the class besides members of 'this'.

Consider the ramifications of this not being the case, especially when writing a copy constructor or assignment operator:


struct A
{
A()
:x(0)
,y(0)
,z(0)
{
}

A( A const& rhs )
:x(rhs.x)
,y(rhs.y)
,z(rhs.z)
{
}

A& operator=( A const& rhs )
{
x = rhs.x;
y = rhs.y;
z = rhs.z;

return *this;
}

private:
int x, y, z;
};

Share this post


Link to post
Share on other sites
Private members are accessible to the class's member functions, and friends of the class. There is no per-instance restriction on access. Most copy constructors take advantage of this fact.

See the official VC++ documentation for more details.

Share this post


Link to post
Share on other sites
I remember i tried the code before and it did not compile because it's trying to access private members...would it be strict ANSI, or a compiler bug in VS2005 Express Ed?

Share this post


Link to post
Share on other sites
Quote:
Original post by HugosHoH
I remember i tried the code before and it did not compile because it's trying to access private members...would it be strict ANSI, or a compiler bug in VS2005 Express Ed?


I have compiled equivalent code in VS2005EE before, without a problem. However, if you can demonstrate a complete minimal example where a member function of a class cannot access a private member of an instance of that class, then you can send it as a bug report.

Share this post


Link to post
Share on other sites
In most object oriented programming languages the accessibility of a variable is seen on class-level and not on object-level. So all private variables from an object are accessible by another object if they are the same type.

Share this post


Link to post
Share on other sites
Quote:
Original post by HugosHoH
I remember i tried the code before and it did not compile because it's trying to access private members...would it be strict ANSI, or a compiler bug in VS2005 Express Ed?


You might have been thinking about protected members in a base class being accessed by derived class functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!