0

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*()?

  • Please do not add `**` in your code snippet, these make your code impossible to copy/paste/run directly. – Holt Jan 18 '19 at 13:20
  • @Holt oh.. I didn't know that, I'll edit then – soyoon3292 Jan 18 '19 at 13:27
  • if you want to highlight certain line I'd suggest just adding `// <-----` or something at the end. `**` looks terribly wrong in cpp code – michelson Jan 18 '19 at 13:28
  • @michelson thanks I'll keep that in mind :D – soyoon3292 Jan 18 '19 at 13:31
  • `char* func() const` - `const` at the end means that the object (functions `this`) will not be modified by that function - object before calling and after calling the function will be the same. Also you may like to take a look here: https://stackoverflow.com/questions/2325121/casting-operator-const-vs-non-const – michelson Jan 18 '19 at 13:33
  • Duplicate of https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-function-declaration-of-a-class – cwschmidt Jan 18 '19 at 13:34
  • Possible duplicate of [Meaning of 'const' last in a function declaration of a class?](https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-function-declaration-of-a-class) – cwschmidt Jan 18 '19 at 13:36

2 Answers2

2

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;
Holt
  • 36,600
  • 7
  • 92
  • 139
0

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
    }
}
Phisn
  • 861
  • 8
  • 18