LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2012, 09:49 PM   #1
Netooo
LQ Newbie
 
Registered: Jun 2010
Location: Guadalajara, Jalisco, Mexico
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
Question C++ violating encapsulation (access to private vars without methods)


Code:
#ifndef ARREGLO_HPP
#define ARREGLO_HPP

using std::istream;
using std::ostream;

class Arreglo{

	friend ostream& operator<<(ostream&, const Arreglo&);
	friend istream& operator>>(istream&, Arreglo&);
	
	public:
		Arreglo(int = 10);
		Arreglo(const Arreglo&);
		~Arreglo();
		int obtieneTamanio() const;
		
		const Arreglo& operator=(const Arreglo&);
		
		bool operator==(const Arreglo&) const;
		
		bool operator!=(const Arreglo&) const;
		
		int& operator[](int);
		
		const int& operator[](int) const;
	
	private:
		int tamanio;
		int *ptr;

};

#endif
How the hell is possible to access private variable using these constructors and functions?:
Code:
Arreglo::Arreglo(const Arreglo& arregloACopiar): tamanio(arregloACopiar.tamanio){
	
	ptr = new int[tamanio];
	
	for(int i = 0; i < tamanio; ++i)
		ptr[i] = arregloACopiar.ptr[i];
}
The argument is an object of this same class, how is it possible to access private variable "tamanio" using "arregloACopiar.tamanio" and how the hell is possible to access private variable "ptr" using "arregloACopiar.ptr[i]"

Same here:
Code:
const Arreglo& Arreglo::operator=(const Arreglo& derecha){
	
	if(&derecha != this){
		
		if(tamanio != derecha.tamanio){
			
			delete[] ptr;
			tamanio = derecha.tamanio;
			ptr = new int[tamanio];	
			
		}
		
		for(int i = 0; i < tamanio; ++i)
			ptr[i] = derecha.ptr[i];
	}
	
	return *this;
}
"derecha.tamanio" and "derecha.ptr[i]" are both ilegal because "tamanio" and "ptr" are private variables.


I have a full working code using those functions, no compiling errors, no runtime errors, everything goes fine. I just don't understand why it works...

explain me, please.
 
Old 05-28-2012, 10:19 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Private variables are accessible to all objects/instances of the same class. They are not hidden from other instances of the same class. That's how C++ works.

Seems like expected behavior to me.

Last edited by dugan; 05-28-2012 at 10:24 PM.
 
  


Reply

Tags
private



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No supported authentication methods available when using a private key for login j.smith1981 Linux - Server 8 03-02-2011 08:14 AM
Can't access methods of a struct kornerr Programming 2 01-02-2006 11:19 AM
ADSL encapsulation/modulation methods MadCactus Linux - Networking 3 07-23-2003 10:25 PM
ADSL encapsulation/modulation methods MadCactus Linux - Software 0 07-19-2003 09:46 AM
Access Methods in Linux dominique Linux - Newbie 15 09-16-2001 12:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration