C++ Class Internal Type Conversion Question, const indicator

Asked 2 years ago, Updated 2 years ago, 115 views

MyString.h (header source)

// CMyString class internal instance and method declaration

class CMyString
{

public:
    explicit CMyString(const char* paramstr);
    CMyString(const CMyString &rhs);
    ~CMyString(void);
    ***operator char*(void) { return m_pszdata;}* // problem**

Private: // Access instruction controller is set to Private to block external value changes
    char* m_pszdata; // Instance to store the address value of dynamic memory containing the string received from the user code
    intm_nlength; // string length

public:
    int SetString(const char* pszparam); // Method to receive a string and create and allocate dynamic memory
    Const char* GetString() const; // string return constant method (constant because no value is written)
    void Release(); // Dynamic Memory Deallocation Method
    CMyString&operator=(const CMyString&rhs); // Multi-Operator(=) Definition Method (simple substitution operator definition)
};

MyString.Cpp (Method Definition Department)

// CMyString class internal instance and method declaration

class CMyString
{

public:
    explicit CMyString(const char* paramstr);
    CMyString(const CMyString &rhs);
    ~CMyString(void);
    operator char*() const{ return m_pszdata; }

Private: // Access instruction controller is set to Private to block external value changes
    char* m_pszdata; // Instance to store the address value of dynamic memory containing the string received from the user code
    intm_nlength; // string length

public:
    int SetString(const char* pszparam); // Method to receive a string and create and allocate dynamic memory
    Const char* GetString() const; // string return constant method (constant because no value is written)
    void Release(); // Dynamic Memory Deallocation Method
    CMyString&operator=(const CMyString&rhs); // Multi-Operator(=) Definition Method (simple substitution operator definition)
};

Main code

// Code part of user using CmyString object

#include "stdafx.h"
#include "MyString.h"

CMyString type object param referring to void TestFunc (const CMyString &param) // argument strData

{
    cout << param <<< endl; // <<<--- point of issue**
}

int_tmain(int argc, _TCHAR* argv[]) // Task 4: Transformation generator and transform operator user code
{
    CMyString strdata("Hello");
    ::TestFunc(strdata);
    ::TestFunc(CMyString("World"));

    return 0;
}

The problem point was annotated in the code.

User Code

*cout << param << endl; An error such as a picture occurs in .

View the commentary in the Mystring.h header file

operator char*() { return m_pszdata;} part

The error disappears when you const { return m_pszdata;}operator char*() const { return m_pszdata;} constant the method.

As far as I know, adding a const after a method name means declaring it a constant method "The constantized method does not allow write access to member variables and functions in a general way." as I understand it.

However, it is difficult to understand why this error is fundamentally resolved by "constant the variant patient function." (It just reads the member function m_pszdata and returns m_pszdata.)

I need your help.

c++ casting const

2022-09-21 11:59

1 Answers

As you know, the value contained in the variable of the data type with the const keyword cannot be modified.

If you look at the code you uploaded main.cpp, there is a part as follows.

void TestFunc(const CMyString& param)
{
    std::cout << param << std::endl;
}

As you can see, we are receiving the const CMyString& type as a function factor.

From the compiler's point of view, the operator charge*() function within CMyString is considered to be a function that does not exist at all when a variable is declared with const CMyString type.

If you define the operator char* () const function, it is accepted as a member function because guarantees that the value of the member variable does not change.

It is difficult to correct the member function implementation, but to give you two simple solutions,

I hope it will help you understand what I explained, and I wrote a simple example code.

#include <iostream>

class A {
private: 
    char* char_pointer_;

public:
    A(const char* arg) {
        this->char_pointer_ = (char*)arg;
    }

    char* GetMyCharPointer() {
        return this->char_pointer_;
    }

    char* GetMyCharPointerConst() const {
        return this->char_pointer_;
    }
};

int main() {
    A standardA("1000");
    const A constA("1000");

    standardA.GetMyCharPointer(); // No problem
    standardA.GetMyCharPointerConst(); // No problem

    ConstA.GetMyCharPointer(); // Error: Member function GetMyCharPointer itself does not exist in constA
    ConstA.GetMyCharPointerConst(); // No problem
}


2022-09-21 11:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.