// casting.cpp
//
// Operator pretypovani

// Trida string reprezentujici znakovy retezec obsahuje pole znaku
// Konstruktor do tohoto pole ulozi retezec
// Operator pretypovani na const char* vrati ukazatel na toto pole
// s je instance tridy my_string
// V prikazu strcpy(ccc, s) se s automaticky konvertuje na const char*

#ifdef _WIN32
	#pragma warning(disable:4996)
	#include <tchar.h>
	#include <conio.h>
#elif (defined __linux__) || (defined _AIX) || (defined __APPLE__)
	typedef char _TCHAR;
	#define _tmain main
#endif

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

class my_string {
public:
	enum { delka = 10 };
	my_string(const char* = 0);
	operator const char* ();
private:
	char c[delka + 1];	// Navic 1 znak pro koncovou 0
};

my_string::my_string(const char* C)
{
	for (int i = 0; i < delka + 1; i++) c[i] = 0;
	if (C) {
		int n = strlen(C);
		strncpy(c, C, (n > delka ? delka : n));
	}
}

my_string::operator const char* ()
{
	return c;
}

void my_getch();

int main() {
	my_string s("Ahoj lidi tam");
	cout << endl << s << endl;  // Vypise "Ahoj lidi "
	//	printf("%s\n",s);	// Vypise "Ahoj lidi "
	char ccc[20];
	strcpy(ccc, s);	// Zde se automaticky pouzije nas operator pretypovani
	cout << ccc << endl;  // Vypise "Ahoj lidi "
	//  char *c = s; // CHYBA - nelze priradit ukazatel na konstantu ukazateli 
						  // na nekonstantni objekt
	//  strcpy(s, ccc); // CHYBA - s nelze konvertovat na char*
	char d = s[1];
	//  s[1] = 'u';   // CHYBA - chceme menit konstantni objekt
	my_getch();
	return 0;
}
//---------------------------------------------------------------------------
void my_getch()
{
#ifdef _WIN32
	_getch();
#else
	cout << endl;
#endif
}
//---------------------------------------------------------------------------
