printf("ho_tari\n");

String4 (const_cast) 본문

C++

String4 (const_cast)

호타리 2023. 9. 8. 17:04

<main.cpp>

#include <iostream>

#include "string.h"



int main()

{

	String s1;

	String s2 = "hello, world";          // String s2("hello, world");

	//String s2 = 0;

	String s3 = s2;                      // String s3(s2);

	

	s1 = s2;

	// s1 = s1;          // self assignment??

	s1 = "hello, ";      // String tmp("hello, "); s1 = tmp; tmp.~String();

	

	String s4 = " IoT";

	s1 = s2 + s4;

	

	if (s1 == s2)

	{

		std::cout << "s1 and s2 are equal" << std::endl;

	}

	else

	{

		std::cout << "s1 and s2 are not equal" << std::endl;

	}

	

	s4 += s4;

	s1 != s2;

	

	std::cout << "s1 : " << s1.c_str() << std::endl;

	std::cout << "s2 : " << s2.c_str() << std::endl;

	std::cout << "s2 len : " << s2.size() << std::endl;

	

	std::cout << s3 << std::endl;

	

	const String s5 = "hello";

	//s5 = "world";

	std::cout << "s3 len : " << s5.size() << std::endl;

	

	char *str = const_cast<char *>(s1.c_str());



	return 0;

}

 

<compile 결과>

'C++' 카테고리의 다른 글

Complex6 (inline 함수, namespace)  (0) 2023.09.11
Complex6 (inline 함수)  (0) 2023.09.11
Shape (dynamic_cast)  (0) 2023.09.08
Pointer3 (reinterpret_cast)  (0) 2023.09.08
GenderRatio (static_cast)  (0) 2023.09.08