private 멤버가 어떻게 레퍼런스 형태로 외부에 반환이 가능한 거죠?

dltkddyd의 이미지

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
 
 
template<typename SARGT>
class Sum:public unary_function<SARGT,SARGT> {
private:
	SARGT accumalator;
public:
	Sum() {accumalator=0;}
	SARGT& operator()(SARGT& arg) {
		accumalator+=arg;
		return accumalator;
	}
};
 
int main() {
	vector<int> obj5={6,7,8,9,10,11};
	vector<int> result5(6);
	transform(obj5.begin(),obj5.end(),result5.begin(),Sum<int>());//한 조각에 대한 계산을 수행한다.
	/*
	template <class InputIterator, class OutputIterator, class UnaryOperator>
	OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op) {
		while (first1 != last1) {
			*result = op(*first1);  // or: *result=binary_op(*first1,*first2++);
	    		++result; ++first1;
	  	}
	  	return result;
	}
	*/
	for_each(result5.begin(), result5.end(), PrintWithS<int>());
	cout<<*(result.end()-1)<<endl;//결과는 51
}

op라는 것은 Sum로 선언된 객체를 의미하는 것입니다. 그리고 op(*first1)은 op.operator()(*first1)을 의미하는 것일거고요. 그리고 이 멤버함수는 누적합 accumalator 멤버변수를 반환합니다. 이 멤버변수는 private 소속입니다. 그런데 이 private 멤버를 레퍼런스로 반환하여 그 외부에서

cout<<*(result.end()-1)<<endl;

로 언급해도 출력된다는 것은 그 프라이빗 멤버에 result5가 접근했다는 것을 의미하는데, 이렇게 되면 객체의 캡슐화, 은닉성이라는 원칙이 깨지는 것 아닌가요? 프라이빗 멤버인 accumalator가 외부로 어떻게 정상적으로 반환이 가능한 것인지 궁금합니다.

jick의 이미지

private라는 건 그 멤버를 외부에서 *직접* 쓸 수 없다는 것뿐이지, public 멤버 함수는 얼마든지 private 멤버를 반환할 수 있습니다.

괜찮은 C++ 서적을 구해서 한번 첨부터 끝까지 정독하시는 걸 추천합니다.

dltkddyd의 이미지

피곤하면 별밖에 보이지 않나봅니다. 여하튼 답변 감사합니다.

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

klara의 이미지

은닉이라는건 내부 구현이 외부에 노출되지 않는 것입니다.
사용자는 반환받은 레퍼런스에 대해서 그게 private멤버인지, public 멤버인지, 심지어는 멤버인지 아닌지 조차도 모르므로 정보는 은닉된 것입니다.
반환받는 레퍼런스에 대해서 무언가를 보장할 수 있다면, 그것은 내부 구현의 일부를 외부에 노출시킨 것이고, 따라서 모든 정보를 은닉시키지는 못한 것입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.