jeudi 27 mai 2021

Why does this compiler warning only show for int but not for string? "type qualifiers ignored on function return type"

I am a bit confused about some warnings I get when compiling my C++11 code using mingw64. This is my MWE:

class A{
    const string name;
    const int ID;

    public:
        A(string name_, int ID_) : name(name_), ID(ID_){
            // initialize non-const members
        }
        const string getName() const{return name;}
        const int getID() const{return ID;}
};

int main()
{   
    A aObj = A("Aname", 1);
    std::cout << "getName() = " << aObj.getName() << std::endl;
    std::cout << "getID() = " << to_string(aObj.getID()) << std::endl;
}

Code executes fine and does what it should, but I get this compiler warning:

,,localtest.cpp:10:9: warning: type qualifiers ignored on function return type

[-Wignored-qualifiers] const int getID() const{return ID;}

So the warning only shows for getID() but not for getName(), even though both have the same type qualifiers. Can somebody explain to me, why this warning seems only to show for string but not for int? I suppose it has something to do with int being a primitive data type - but what exactly?

Aucun commentaire:

Enregistrer un commentaire