일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 배열
- Windows
- 함수
- 티스토리
- 지식나눔강좌
- CS
- 연산자
- 백준
- 리뷰
- Win32
- doit코틀린프로그래밍
- 이지스퍼블리싱
- Javascript
- c#
- 문법
- 프로그래밍
- Desktop
- 알고리즘
- c++
- Programming
- Tips프로그래밍강좌
- c
- 포인터
- Visual Studio
- VS ERROR
- Tips강좌
- Kotlin
- 김성엽
- Direct2D
- tipssoft
- Yesterday
- Today
- Total
F.R.I.D.A.Y.
지식인: C++의 String.data() 본문

[C++] 예제 코드의 내용중 이해가 안되는 부분이 있습니다.
#include <iostream> using namespace std; class Parent { public: virtual string Class_Message()...
kin.naver.com
Index
- data()를 어디에 사용하는지 설명해주세요.
- Code.
- data 함수는 string 자료형의 내부 함수입니다.
- 이 함수는 어디에서 온걸까
- Reference.
data()를 어디에 사용하는지 설명해주세요.copy^
Test_Function, Test_Function2 함수를 보면 data 함수를 추가로 사용하는 것을 알 수 있습니다. data 함수를 어디에 사용하나요?
Code.copy^
#include <iostream>
using namespace std;
class Parent {
public:
virtual string Class_Message() {
return "Parent";
}
};
class Child : public Parent {
public:
string Class_Message() {
return "child";
}
};
void Test_Function (Parent parent) {
cout << parent.Class_Message().data() << endl;
return;
}
void Test_Function2 (Parent* parent) {
cout << parent->Class_Message().data() << endl;
}
int main (void) {
Parent parent;
Child child;
Test_Function (parent);
Test_Function (child);
cout << endl;
Test_Function2 (&parent);
Test_Function2 (&child);
return 0;
}
data 함수는 string 자료형의 내부 함수입니다.copy^
data 함수는 C++ string 객체의 함수입니다. 이 함수는 string 객체가 가지고 있는 문자열을 c-string 방식으로 이용할 수 있는 포인터를 반환합니다.
이 함수는 어디에서 온걸까copy^
data 함수를 사용하는 두 함수를 보면 Parent 클래스의 Class_Message 함수를 호출하는 것을 볼 수 있습니다.
void Test_Function (Parent parent) {
cout << parent.Class_Message().data() << endl;
return;
}
void Test_Function2 (Parent* parent) {
cout << parent->Class_Message().data() << endl;
}
그렇다면 Parent 클래스를 볼 필요가 있습니다.
class Parent {
public:
virtual string Class_Message() {
return "Parent";
}
data라는 이름의 함수는 눈 씻고 찾아봐도 보이지 않습니다. 정의/선언하지 않는 함수를 사용하면 오류가 발생합니다. 그런데도 오류가 안나는 이유는 반환 자료형이 string이기 때문입니다.
data 함수는 string 객체이므로 string 객체에 들어있는 data 함수를 사용하는 것입니다.
void Test_Function (Parent parent) {
cout << parent.Class_Message().data() << endl;
// cout << static_cast<string>("Parent").data() << endl;
return;
}
void Test_Function2 (Parent* parent) {
cout << parent->Class_Message().data() << endl;
// cout << static_cast<string>("child").data() << endl;
}
Class_Message 함수가 반환한 string 객체의 멤버 함수인 data가 붙은 것으로 생각하면 됩니다.
Reference.copy^
string::data - C++ Reference
Complexity, iterator, access, exceptions Unspecified or contradictory specifications. Complexity Constant. Iterator validity No changes. Data races The object is accessed. Exception safety No-throw guarantee: this member function never throws exceptions.
www.cplusplus.com
'외부활동 > 지식in' 카테고리의 다른 글
지식인: static 변수의 초기화와 대입 (1) | 2020.09.03 |
---|---|
지식인 질문하기 (0) | 2019.04.02 |
지식인 : 리스트 자료구조 포인터 (0) | 2019.03.06 |
지식인 : 성적을 xls(엑셀)로 출력하기 (0) | 2019.01.16 |
지식인 : 시간 문자열을 숫자(초)로 변환하기 (0) | 2018.11.13 |