Is const_cast safe on an element pointer?

In the following code, the non constobject method calls the method of constthe same object that returns the const-inter in the object field, and then this returned pointer is called to the non version const- this is a method similar to the one in this answer: Elegant duplication solution, const and non-const, getters? [Duplicate] . However, since I put pointers in a complex data structure, I'm not sure if this will really work. Will it be?

class Struct {
private:
    Field f, g;

    std::map<std::string, const FieldBase*> const_fields_t;
    std::map<std::string, FieldBase*> fields_t;

public:
    const_fields_t get_fields() const {
        return const_fields_t { { "f", &f }, { "g", &g } };
   }

   fields_t get_fields() {
        const Foo* = this;
        fields_t result;

        foreach(auto& v : const_this->get_fields()) {
            result[v->first] = const_cast<FieldBase*>(v->second);
        }
        return result;
   }
};
+4
source share
1 answer

Yes, (I cleared your code a bit):

#include <string>
#include <functional>
#include <iostream>
#include <map>
using namespace std;

class FieldBase {public: virtual string get_data() = 0; };
class Field : public FieldBase { public: string data; virtual string get_data() { return data; } };

class Struct {
public:
    Field f, g;

    typedef std::map<std::string, const FieldBase*> const_fields_t;
    typedef std::map<std::string, FieldBase*> fields_t;

public:
    const_fields_t get_fields() const {
        cout << "const get_fields() called" << endl;
        return const_fields_t { { "f", &f }, { "g", &g } };
   }

   fields_t get_fields() {
        cout << "non-const get_fields() called" << endl;
        auto const_this = static_cast<const Struct*>(this);
        fields_t result;

        for(auto& v : const_this->get_fields()) {
            result[v.first] = const_cast<FieldBase*>(v.second);
        }
        return result;
   }
};

int main ()
{
    Struct a;
    a.f.data = "Hello";
    a.g.data = "World";

    Struct::fields_t obj = a.get_fields();

    cout << obj["f"]->get_data() << endl;
    cout << obj["g"]->get_data() << endl;

}

Live example

const , , . , .

, , , , .

+1

All Articles