so I understand I should not use const after the argument list, but since I don't want any other method to modify the value returned by this method, I should include const before char, is that correct?
Yes. I'll post an example demonstrating the two varying functions.
struct Base
{
private:
int Member;
public:
constint &GetMember( void ) const;
int &SetMember( void );
};
constint &Base::GetMember( void ) const
{
returnthis->Member;
}
int &Base::SetMember( void )
{
returnthis->Member;
}
int main( )
{
Base base;
base.GetMember( ) = 10; // Error! This method returns a constant reference to Member.
base.SetMember( ) = 10; // OK. This method returns a non-constant reference to Member
}
Since GetMember( ) returns a constant reference to Member, we cannot modify Member through GetMember( ) simply because it's promised not to modify any members (hence the const after the argument list of GetMember( )). However, since SetMember( ) returns a non-constant reference to Member, we can modify Member through SetMember( ).
References:
Constant Qualifier: http://msdn.microsoft.com/en-us/library/07x6b05d.aspx
Constant Member Functions: http://msdn.microsoft.com/en-us/library/6ke686zh(v=vs.80).aspx
gotcha....your example and references are helpful.
so, even if I remove the const before int in GetMember()
and write the function as:
1 2 3 4
int &Base::GetMember( void ) const
{
returnthis->Member;
}
it still wouldn't help because fundamentally by writing the const after the argument list, I have promised to not modify any members, which in this case includes the variable Member.
but of course when I try returning a non-const reference, the compiler gives me the following error:
error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’
error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’
Since Member is returned through GetMember( ) by reference, the method has still promised not to modify any members. When you call GetMember( ), you're calling a read-only method. Any method that has the const qualifier after the argument list is a read-only method. This means that the member returned by the method cannot be re-assigned a value through that method. Instead, the method must allow member assignments, just like SetMember( ).
I see, so GetMember in this case is a read-only method and only returns Member, doesn't reassign. if we want to make (re)assignments here, we use SetMember.
...that makes it clear....thank you for helping me understand.
hi Framework,
so, on my program, instead of using the s[] array, I'd like my program to read the values from a spreadsheet....so, I added the following code:
1 2 3 4 5 6 7 8 9 10 11
pfile.open("test.xls");
if(pfile.isopen())
{
while(pfile.good())
{
getline(pfile,line); //the file only has a series of numbers in 100 rows, do I need to use the getline command here?
cout<<line<<endl;
}
pfile.close();
}
else cout<<"Unable to open file"<<endl;
but how do I translate the if() code, so it works with the file.....I cannot use line[1] in place of s[1]??
1 2 3 4 5 6 7 8
if(trend.Flag=='U')
{
if(s[1]>s[0])
{
retrend=trend.upmove(&(s[1]),trend.Flag,trend.Ut);
ut=retrend.Ut;
cout<<"UT is now $: "<<retrend.Ut<<" and flag is: "<<retrend.Flag<<"\n";
}
can you help me understand this or point me to a good reference
You could but that would mean you extracting each integer from the string. I would lean towards this, personally:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int s[ n ] = { 0 };
std::ifstream inputstream;
inputstream.open( "test.xls" )
if( inputstream.is_open( ) )
{
while( inputstream.good( ) )
{
for( int i( 0 ); i < 100; i++ )
// After 100 passes of this loop, one single line would've been extracted.
inputstream >> s[ i ];
}
}
itcplpl wrote:
but how do I translate the if() code, so it works with the file
I'm not quite sure what you're saying. Could you elaborate further, please?
you say // After 100 passes of this loop, one single line would've been extracted.
what I am looking to do is read 100 lines, i.e. 100 records, where each record contains a double value.
regd. the if() statement, when I read the second record from the file, I want to compare its value to the first one, and if it is higher, I assign the second record an up flag, else a down or equal flag, as the case may be.
I want to do this (read, and assign flag operation) for each of the 100 records or until I hit EOF.
btw, if you want, I can communicate with you via email vs. posting here
I added line 23 in the code to see what it was reading, and
the output is
"this record is 0"
has it got anything to do with the fact that it is a .xls file?
when you say
The format itself should affect the program.
does that mean I need to go fix the format of the file....all I did is created the excel spreadsheet and insert these values in it.....
like you said, it is very simple, that's why i dont understand why it is not working?
aha....so, L B (thanks for the suggestion) what I did is just use the notepad file instead of the excel spreadsheet and that works with Framework's (thx again!) code...I was hoping I could work with Excel but obviously that is not working...
Excel is one of M$ file format. Usually for me I would use the Export as CSV option in Excel and then work on the CSV file instead to prevent any "special formatting" Excel has if I work direct with it.