Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AndreTheGiant

What the hell, I thought that was private!?

This topic is 5367 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

I accidentally wrote this code and it compiles and actually runs but I dont know why. I thought that a private variable in C++ meant that no one could access that variable except for that class itself. c.h
#include <iostream>
using std::endl;
using std::cout;

class C {
public:
	C(int init);
	void doSomeShit(C *c);
private:
	int privateVariable;
};
c.cpp
#include "c.h"

C::C(int init){
	privateVariable = init;
}

void C::doSomeShit(C *c) {
	cout<<"this privateVariable: "<<this->privateVariable<<endl;
	cout<<"that privateVariable: "<<c->privateVariable<<endl;
	cout<<"what the hell, i thought that was private!"<<endl;
}
main.cpp
#include <iostream>
using std::cout;
using std::endl;

#include "c.h"

int main() {

	C *c1 = new C(111111);
	C *c2 = new C(222222);

	c1->doSomeShit(c2);

	return 0;
}
output
 
this privateVariable: 111111
that privateVariable: 222222
what the hell, i thought that was private!
Why can c1 access c2's private shit? [edited by - AndreTheGiant on November 10, 2003 10:15:45 AM]

Share this post


Link to post
Share on other sites
Advertisement
You may have used it before in your copy constructors to access the elements of another instance of the same class.

The C++ standard simply says:



A member of a class can be
— private; that is, its name can be used only by members and friends of the class in which it is
declared.
— protected; that is, its name can be used only by members and friends of the class in which it is
declared, and by members and friends of classes derived from this class (see 11.5).
— public; that is, its name can be used anywhere without access restriction.


[edited by - Jingo on November 10, 2003 10:30:29 AM]

Share this post


Link to post
Share on other sites
I still remember the day I learned this: about 3 months after using C++ - back then I was writing member functions like this:


MyClass& MyClass::operator=(const MyClass &rhs)
{
width = rhs.GetWidth();
height = rhs.GetHeight();
}

// instead of


MyClass& MyClass::operator=(const MyClass &rhs)
{
width = rhs.width;
height = rhs.height;
}

Share this post


Link to post
Share on other sites
It makes sense, you the class author are fully in control of all private violations between instances of your own class, so it doesn''t violate encapsulation. Java, and likely most OO languages, are the same way.

Share this post


Link to post
Share on other sites
quote:
Original post by tortoise
it doesn''t violate encapsulation.

Actually, it does.

But as always - it''s a judgement call. If you find yourself writing tons of getters merely as support for writing copy constructors, thats a far worse violation of encapsulation than just accessing the fields directly.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites

  • 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!