jeudi 17 avril 2014

C++ substituer des méthodes virtuelles finales et pures - Stack Overflow


Consider a baseclass for derived classes, where the baseclass is supposed to offer some (polymorphic) methods such as armithmetic or bitweise overloaded operators for all its derivates. This operations should NOT be modifiable by the derived classes to ensure correct execution. However, at the same time I would like to have the evaluation -- the function isError() in my example -- necessary for the operator to be individual for subclasses and pure virtual, so that it MUST be defined:


class mom
{
public:
virtual bool operator && (const mom&) const final
{
return this->isError() && p_rOther.isError();
}
pritave:
virtual bool isError() = 0;
};

This does not seem to be allowed given the current standard, as the "pure virtuality" implies the necessity for subclasses to implement all virtual functions of the baseclass whereas the "final" keyword contradicts this paradigm.


Any suggestions or ideas how to handle this contradiction in re?




I assume that you have been misinformed on how pure virtual member-functions are dealt with and what they are for.


Only member-functions declared to be pure virtual must be defined in the classes inheriting from your base.


I'm guessing you are confusing it with the fact that the whole Base will become abstract.




The snippet further down in this post works just fine, and results in the behavior that you have described. Though having a virtual function made final kind of defeats it's purpose.


I'd recommend you to remove the virtual specifier.


This is unless your Base is inheriting from a base where the virtual function is present and you'd like to make this clear to future developers reading your code.




#include <iostream>




struct Base {
virtual bool operator&& (Base const& rhs) const final {
std::cerr << "Base ::operator&& ()\n";

return this->error () && rhs.error ();
}

virtual bool error () const = 0;
};

struct Child : Base {
virtual bool error () const {
std::cerr << "Child::error ()\n";

return true;
}
};



int
main (int argc, char *argv[])
{

Child ch1, ch2;
; ch1 && ch2;
}

output


Base ::operator&& ()
Child::error ()
Child::error ()



You seem to be mistaken about what a pure virtual function means.



the "pure virtuality" implies the necessity for subclasses to implement all virtual functions of the baseclass



No, the pure virtuality requires that a subclass overrides that particular function. Non-pure functions don't have to be overridden, and of course can't be if declared final.



Consider a baseclass for derived classes, where the baseclass is supposed to offer some (polymorphic) methods such as armithmetic or bitweise overloaded operators for all its derivates. This operations should NOT be modifiable by the derived classes to ensure correct execution. However, at the same time I would like to have the evaluation -- the function isError() in my example -- necessary for the operator to be individual for subclasses and pure virtual, so that it MUST be defined:


class mom
{
public:
virtual bool operator && (const mom&) const final
{
return this->isError() && p_rOther.isError();
}
pritave:
virtual bool isError() = 0;
};

This does not seem to be allowed given the current standard, as the "pure virtuality" implies the necessity for subclasses to implement all virtual functions of the baseclass whereas the "final" keyword contradicts this paradigm.


Any suggestions or ideas how to handle this contradiction in re?



I assume that you have been misinformed on how pure virtual member-functions are dealt with and what they are for.


Only member-functions declared to be pure virtual must be defined in the classes inheriting from your base.


I'm guessing you are confusing it with the fact that the whole Base will become abstract.




The snippet further down in this post works just fine, and results in the behavior that you have described. Though having a virtual function made final kind of defeats it's purpose.


I'd recommend you to remove the virtual specifier.


This is unless your Base is inheriting from a base where the virtual function is present and you'd like to make this clear to future developers reading your code.




#include <iostream>




struct Base {
virtual bool operator&& (Base const& rhs) const final {
std::cerr << "Base ::operator&& ()\n";

return this->error () && rhs.error ();
}

virtual bool error () const = 0;
};

struct Child : Base {
virtual bool error () const {
std::cerr << "Child::error ()\n";

return true;
}
};



int
main (int argc, char *argv[])
{

Child ch1, ch2;
; ch1 && ch2;
}

output


Base ::operator&& ()
Child::error ()
Child::error ()


You seem to be mistaken about what a pure virtual function means.



the "pure virtuality" implies the necessity for subclasses to implement all virtual functions of the baseclass



No, the pure virtuality requires that a subclass overrides that particular function. Non-pure functions don't have to be overridden, and of course can't be if declared final.


0 commentaires:

Enregistrer un commentaire