Skip to main content

Posts

Showing posts from March, 2009

MBCS/Unicode enabled C++ string class.

TCHAR, the generic text mapping data type. This is Microsoft specific extension and is not ANSI-compatible. I have used this extension to create my small prototype MBCS/Unicode compatible class. This class consists of all the basic functionality required to represent a minimal string class. I have given the name for this class as "CStringUNI". // Header File #pragma once const int INIT_ALLOC_SIZE = 10; class CStringUNI { private: TCHAR *m_szBuffer; TCHAR *AllocateMemory(size_t size); public: CStringUNI(); CStringUNI(const CStringUNI&); CStringUNI(const TCHAR*); CStringUNI& operator=(const TCHAR*); CStringUNI& operator=(const CStringUNI&); CStringUNI& operator+=(const CStringUNI&); // Access Operator TCHAR operator[](const size_t n)const; TCHAR *GetBuffer() const; bool operator==(const TCHAR*) const; bool operator==(const CStringUNI&) const; virtual ~CStringUNI(); friend CStringUNI operator+(const CStringUNI& lhs, const

Importance of 'const' in C++

Lets declare a class 'Demo' class Demo { private: char a; void TestConst(char* const pData) const { // pData = "test"; // char* const protects // pData++; // pointer! // a = 'a'; // const for function // protects data member. } }; So, when we are working with C++, all data members irrespective of access specification, can be protected with 'const', appended to the declaration/definition.