So I have a class called MusicComposer that has an AudioSignal object as a data member called music. Music is non-const. However, I need to return music as a const AudioSignal in a method called getMusic.
In the source file:
const AudioSignal& MusicComposer::getMusic(){
}
music is declared as:
AudioSignal music;
^ this is in the header file.
I declare music as non-const because music needs to be changed before it is returned by the getMusic method. However, I can't figure out for the life of me how to return a const version of it. How can I return a const version of music?
constis a property of the expression accessing the object, not the object itself.constwill resist non-const access, and forcing the issue (withconst_castor similarly) produces undefined behavior (i.e., may crash).