C++ how to return a const object from a class variable -
so have class called musiccomposer has audiosignal object data member called music. music non-const. however, need return music const audiosignal in method called getmusic.
in source file:
const audiosignal& musiccomposer::getmusic(){ }
music declared as:
audiosignal music;
^ in header file.
i declare music non-const because music needs changed before returned getmusic method. however, can't figure out life of me how return const version of it. how can return const version of music?
c++ can automatically change mutable const
whenever. no biggie. it's going other way that's hard.
return music;
if really want explicit this, it's wierd code, , wierd bad.
return static_cast<const audiosignal&>(music);
also, cyber observed getter methods const, lets them called when musiccomposer
const
.
const audiosignal& musiccomposer::getmusic() const { ^^^^^
Comments
Post a Comment