c++ - Cast a static const object to base class -


i have static const object attributes set variable. i've thought derive class , set attributes in derived class. have share const object other class, should first cast base class error.

class qaudiolib { private:     class defaultaudioformat : qaudioformat     {         defaultaudioformat()         {             setbyteorder(qaudioformat::littleendian);             setchannelcount(2);             setcodec("audio/pcm");             setsamplerate(44100);             setsamplesize(16);             setsampletype(qaudioformat::signedint);         }     };      static const defaultaudioformat default_format;  public:     qaudiolib();      static qaudioformat getdefaultformat()     {         return reinterpret_cast<qaudioformat>(default_format);     }     }; 

the compiler gets error on cast line

error: 'qaudioformat' inaccessible base of 'qaudiolib::defaultaudioformat'          return (qaudioformat)(default_format);                                              ^ 

what have do?

the compiler error because default inheritance private (for classes , public structs), rid of using

class defaultaudioformat : public qaudioformat 

besides that, should return const reference, because copy (you create new qaudioformat object instead of passing original defaultaudioformat object), results in slicing (although if every attribute part of qaudioformat not such big issue, still).

if set properly, shouldn't need typecast here. this:

static const qaudioformat & getdefaultformat() {     return default_format; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -