Reputation: 3
These are sources that work:
MyString.h
#pragma once
class CMyString {
public:
CMyString();
CMyString(const CMyString &rhs);
CMyString(const char* param);
~CMyString();
private:
char* m_pszData;
int m_nLength;
public:
int setString(const char* pszParam);
const char* getString() const;
void release();
CMyString& operator=(const CMyString &rhs);
operator char*() const;
};
MyString.cpp
#include "MyString.h"
#include <iostream>
#include <cstring>
CMyString::CMyString() : m_pszData(NULL), m_nLength(0) {
}
CMyString::CMyString (const CMyString &rhs) : m_pszData(NULL), m_nLength(0) {
this->setString(rhs.getString());
}
CMyString::CMyString (const char* pszParam) : m_pszData(NULL), m_nLength(0) {
setString(pszParam);
}
CMyString::~CMyString() {
release();
}
int CMyString::setString(const char* pszParam) {
release();
if(pszParam == NULL)
return 0;
m_nLength = strlen(pszParam);
if(m_nLength == 0)
return 0;
m_pszData = new char[m_nLength + 1];
strncpy(m_pszData, pszParam, m_nLength);
return m_nLength;
}
const char* CMyString::getString() const {
return m_pszData;
}
void CMyString::release() {
if(m_pszData != NULL)
delete[] m_pszData;
m_pszData = NULL;
m_nLength = 0;
}
CMyString &CMyString::operator = (const CMyString &rhs) {
if(this != &rhs)
this->setString(rhs.getString());
return *this;
}
CMyString::operator char*() const { return m_pszData; }
StringCtrlSample.cpp
#include "MyString.h"
#include <iostream>
using namespace std;
void testFunc(const CMyString &strParam) {
cout << strParam << endl;
}
int main(int argc, char *argv[]) {
CMyString strData("Hello");
cout << strData.getString() << endl;
::testFunc(strData);
::testFunc(CMyString("World"));
return 0;
}
I tried operator overloading about char*() without "const" keyword at MyString.h and MyString.cpp but printed error at compiling. ex) operator char*();
Can someone explain me the difference about adding "const" keyword at the end of char*()?
Upvotes: 0
Views: 107
Reputation: 37606
In this function:
void testFunc(const CMyString &strParam) {
cout << strParam << endl;
}
strParam
is a const
-reference to a CMystring
, so you can only call const
-qualified method on it:
// Not const-qualified overload
operator char*()
// const-qualified overload
operator char*() const
Within a const
-qualified method, you cannot modify non-mutable
attributes or call non-const
methods, which guarantees that your object is not "modified" (at least it should not be from an external point of view).
Note that you should likely convert to const char*
from the const
-qualified method, otherwise it would be possible to do this:
void testFunc(const CMyString &strParam) {
// I am modifiying the underlying char array of const variable
static_cast<char*>(strParam)[0] = '\0';
}
So your conversion operator should be:
operator const char*() const;
Upvotes: 2
Reputation: 881
Adding the const
keyword at the end of a method forbids it to change any member values or call any non-const methods.
The only exception are mutable values, which can always be modified.
class A
{
int i1;
mutable int i2;
public:
void m() const
{
// i1 = 5 <- error
i2 = 5 // <- ok
}
}
Upvotes: 0