functions

Pages: 12
closed account (zb0S216C)
itcplpl wrote:
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Base
{
    private: 
        int Member;

    public:
        const int &GetMember( void ) const;
        int &SetMember( void );
};

const int &Base::GetMember( void ) const
{
    return this->Member;
}

int &Base::SetMember( void )
{
    return this->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

Wazzak
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
{
    return this->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’



Last edited on
closed account (zb0S216C)
itcplpl wrote:
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( ).

Wazzak
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.
closed account (zb0S216C)
No problem :)

Wazzak
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

closed account (zb0S216C)
itcplpl wrote:
do I need to use the getline command here?

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?

Wazzak


Last edited on
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
closed account (zb0S216C)
itcplpl wrote:
what I am looking to do is read 100 lines, i.e. 100 records, where each record contains a double value. (sic)

Then maybe you want this (?):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct Record
{
    Record( void ) : value( 0.0 ), flag( 'D' ) 
    { }

    double value;
    char flag;
};

Record records[ 100 ];

std::ifstream inputstream;
inputstream.open( "test.xls" )

if( inputstream.is_open( ) )
{
    while( inputstream.good( ) )
    {
        for( int i( 0 ); i < 100; i++ )
        {
            // Extract the record data.
            inputstream >> records[ i ].value;
  
            if( records[ 0 ].value > records[ i ].value )
                records[ i ].flag = 'D';

            else
                records[ i ].flag = 'U';
        }
    }
}

Wazzak
I think this should work..let me test it out and write you.
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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct Record
{
    Record( void ) : value( 0.0 ), flag( 'D' ) 
    { }

    double value;
    char flag;
};

Record records[ 100 ];

std::ifstream inputstream;
inputstream.open( "test.xls" )

if( inputstream.is_open( ) )
{
    while( inputstream.good( ) )
    {
        for( int i( 0 ); i < 100; i++ )
        {
            // Extract the record data.
            inputstream >> records[ i ].value;
   cout<<"this record is: "<<records[i].value<<endl;
            if( records[ 0 ].value > records[ i ].value )
                records[ i ].flag = 'D';

            else
                records[ i ].flag = 'U';
        }
    }
}
closed account (zb0S216C)
itcplpl wrote:
has it got anything to do with the fact that it is a .xls file?

I shouldn't. Can you post 3 lines from the file from which you're reading?

Wazzak
these are the first 3 lines:

1
2
3
23.8
23.33
23.67

closed account (zb0S216C)
The file format, in my eyes, is very simple. The format itself shouldn't affect the program.

[Note: Edited. Sorry for any misunderstandings :)]

Wazzak
Last edited on
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?
Open the .xls file in notepad and paste some of the first few lines from it. I'm sure you'll see very different stuff.
I did a copy and paste of the contents of the excel file into notepad, and its a series of numbers one below the other?
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...
Last edited on
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.
sounds good....I will try that...
Topic archived. No new replies allowed.
Pages: 12