int getSomePrivateInt(void) const { return somePrivateInt; }
Thanks for looking, Nicholi, but I'm thinking that's not the answer I'm looking for as I don't think that tells the compiler the code in 'otherClassFunction' cannot change the pointer passed to it as 'theClass'.
Trying some 'const correctness' in my code - and getting "call non-const method for constant object"
class someClass
{ ...
public: int getSomePrivateInt(void){ return( somePrivateInt ); }
} *someClassPtr;
class otherClass
{ ...
void otherClassFunction( const someClass* theClass ) // also (someClass* const theClass ) - The objective is to tell the compiler the pointer 'theClass' is never to be modified by this method.
{
int anInt = theClass.getSomePrivateInt() // produces compiler error "call non-const method for constant object"
}
}
What is the right way to tell the compiler the object pointer is not to be modified by the method?
This code is working as expected :
class otherClass { //... public: void otherClassFunction(someClass* const theClass) { theClass=new someClass; // 'theClass' - constant cannot be modified int anInt=theClass.getSomePrivateInt(); } };
Then you need to provide the rest of your code because it compiles fine for me.
Thanks for your help. Code sample attached. I can see that one form works and the other doesn't.
fails: setBarNum( const c_WorkingBarValues* barInfo )
compiles: setBarNum( c_WorkingBarValues* const barInfo )
I miss-read this and other text like it, as meaning the pointer p is constant, versus saying the X is constant.
"https://isocpp.org/wiki/faq/const-correctness": What does “const X* p” mean?
Read it right-to-left: “p is a pointer to an X that is constant.”
Thanks for your help. Code sample attached. I can see that one form works and the other doesn't.
fails: setBarNum( const c_WorkingBarValues* barInfo )
compiles: setBarNum( c_WorkingBarValues* const barInfo )
I think the form that fails should work, but as it is is fine by me (now I know).
https://isocpp.org/wiki/faq/const-correctness: What does “const X* p” mean?
Read it right-to-left: “p is a pointer to an X that is constant.”
No it should not work. You declare your object as constant and you call a non constance method. If you want a constant object do as nicholishen show you.
The objective is to tell the compiler the pointer 'theClass' is never to be modified by this method.
Make your mind, do you want a pointer to a constant object (you need to declare the method to use as constant) or a constant pointer to a (modifiable) object (you need to declare as I showed you) ?
EDIT: Looking at your code you probably want both :-D
void setBarNum( const c_WorkingBarValues*const barInfo )
This is how it should look.
//+------------------------------------------------------------------+ //| Non_const_method.mq4 | //| | //| | //+------------------------------------------------------------------+ #property strict #property indicator_separate_window class WorkingBarValues { private: ENUM_TIMEFRAMES m_indiTF; datetime m_firstBarTime; public: void WorkingBarValues(void){ m_indiTF = PERIOD_H1; this._init_first_bar_time(); } ENUM_TIMEFRAMES getIndiTF(void) const { return m_indiTF; } datetime getFirstBarTime(void) const { return m_firstBarTime; } protected: void _init_first_bar_time(void){ m_firstBarTime = iTime(_Symbol,this.getIndiTF(), iBarShift(_Symbol,this.getIndiTF(),Time[Bars-1],false) ); } }; // class Signals { private: int m_barNum; public: void Signals(void):m_barNum(0){} void setBarNum(const WorkingBarValues &barInfo){ setBarNum(&barInfo); } void setBarNum(const WorkingBarValues *barInfo){ m_barNum = iBarShift(_Symbol, barInfo.getIndiTF(), barInfo.getFirstBarTime(), false ); } int getBarNum(void) const { return m_barNum; } }; // WorkingBarValues *bar_values; Signals *signals; int OnInit() { bar_values = new WorkingBarValues(); signals = new Signals(); return(INIT_SUCCEEDED); } // void OnDeinit(const int reason) { delete bar_values; delete signals; Comment(""); } // int OnCalculate( const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long& tick_volume[], const long& volume[], const int& spread[] ) { signals.setBarNum(bar_values); Comment( "First Bar Num is: "+IntegerToString(signals.getBarNum()) ); return(rates_total); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Trying some 'const correctness' in my code - and getting "call non-const method for constant object"
class someClass
{ ...
public: int getSomePrivateInt(void){ return( somePrivateInt ); }
} *someClassPtr;
class otherClass
{ ...
void otherClassFunction( const someClass* theClass ) // also (someClass* const theClass ) - The objective is to tell the compiler the pointer 'theClass' is never to be modified by this method.
{
int anInt = theClass.getSomePrivateInt() // produces compiler error "call non-const method for constant object"
}
}
What is the right way to tell the compiler the object pointer is not to be modified by the method?