Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ question

Options
  • 07-01-2002 8:42pm
    #1
    Registered Users Posts: 255 ✭✭


    When you have a class, and you have a member function whats the difference in having const before the function name compared to after the function name

    EG

    const void time::gettime (); const; // what do each of the two const's do in this case??

    thank you.

    hertz.


Comments

  • Closed Accounts Posts: 77 ✭✭paddymee


    const void time::gettime (); const; // what do each of the two const's do in this case??

    Are you sure this even compiles?

    Is this meant to be in the header or cpp file? It doesn't look right for either place.


  • Registered Users Posts: 255 ✭✭hertz


    Sorry, my fault, Il re phrase it..

    const void time::settime(); // whats the difference in having const preceding this function

    compared to this:
    void time::settime() const;

    Deitel & Deitel have this. I think the first one is to make the function constant to the client. The second Im not sure about, but it was in the book.

    Thanks.
    Hertz.


  • Closed Accounts Posts: 77 ✭✭paddymee


    const void time::settime(); // whats the difference in having const preceding this function

    compared to this:
    void time::settime() const;

    Deitel & Deitel have this. I think the first one is to make the function constant to the client. The second Im not sure about, but it was in the book.

    Ok, I can't tell you the difference because I dont know what the first is, but I reckon I know what the second is.

    time time::gettime() const; means that the gettime member function will not modify the state of the time object. It's a way to get the state of an object and assuring the client that the state won't change because of this request.

    Your example doesn't really make sense if I'm correct! It should return something and shouldn't set anything.

    I suspect that the first is another way of typing the second, but I dont know.

    I could be wrong about all this, so your best bet is to try it out and see what happens.


    paddy


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Forgive me if I'm wrong ... (i've not been doing C++ in quite a few months, so I'm a little cold regarding its finer details), but isn't 'const' used to ensure that only one variable/object/etc can be in existence??

    Eg. int blah const ;

    and that a duplicat can't be created??

    Although I've a feeling that paddymee is right in his last post about not changing states.


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Lemming I could be wrong , but I don't think so?

    If you were intent on modifying the value of the of the time object couldn't you just do a hack like so?
    #include <iostream.h>
    #include <stdlib.h>
    
    class time{
    	public:
    	int a;
    	time()
    	{
            a=100;
    	};
    	const time timex(time*x);
    	void isonmyside(void) const;
    	};
    
    const time time::timex(time*x)
    {
     x->a=99;
     cout<<"The value of the variable a is "<<(*this).a<<endl<<" "<<x->a<<endl;
    return (*this);
    };
    
    void time::isonmyside(void) const
    {
     time *n;
     //(*this).a=100 not allowed right?
     n=(time*)&(*this);
     n->a=899;
     cout<<"The value of the variable a is "<<(*this).a<<endl;
    
    return;
    };
    
    int main(int argc,char**argv)
    {
     time t,w;
     int a,b;
     cout<<"Objects instanciated printing values now "<<t.a<<" "<<w.a<<endl;
     t.timex(&w);
     t.isonmyside();
     cout<<"Main The value of time t.a is "<<t.a<<endl;
     cout<<"Main The value of time w.a is "<<w.a<<endl;
     system("pause");
    return 0;
    }; 
    


  • Advertisement
  • Closed Accounts Posts: 77 ✭✭paddymee


    Forgive me if I'm wrong ... (i've not been doing C++ in quite a few months, so I'm a little cold regarding its finer details), but isn't 'const' used to ensure that only one variable/object/etc can be in existence??

    No, this isn't true. The const has many different meanings depending on its usage. I think you are thinking about assigning a value to a variable that cant be changed. Like the #define in ANSI C.

    The const can be very confusing. The example, const void time::settime();. I don't know what it means.
    voivoid time::isonmyside(void) const
    {
    time *n;
    //(*this).a=100 not allowed right?
    n=(time*)&(*this);
    n->a=899;
    cout<<"The value of the variable a is "<<(*this).a<<endl;

    return;
    };

    Yes you can do this. But why would you? If its an accident, that shows why C casting is not safe. A static_cast would catch this.

    If you are doing it on purpose, why bother making a "contract" by putting a const there and then going behind it's back?

    Paddy.


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    AH ... both myself and paddymee were right in our own way.

    I've just checked my little reference book (a bargain at £15 for all the handy stuff in it).

    "The C/C++ Programmer's Reference"
    Herbert Schildt
    Osborne publishing

    In it, it states that const is used to tell your compiler that a variable cannot be changed by the program. Therefore that implies that only one instance of that variable can exist during the lifecycle of the program, AND the value can't be altered


  • Closed Accounts Posts: 77 ✭✭paddymee


    In it, it states that const is used to tell your compiler that a variable cannot be changed by the program. Therefore that implies that only one instance of that variable can exist during the lifecycle of the program, AND the value can't be altered

    const has many different meaning based on it's context. The most basic form is that the variable can't be modified. It gets more complicated when pointers are involved. And functions etc..

    However I don't see how you imply that there can only be one instance in the whole lifecycle of the program because it's const. That sounds like a STATIC to me.

    Maybe you are confused there?

    There is always one instance of the variable per instance of the class. Again unless it's a static, which is shared between instances.

    Paddy


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Originally posted by paddymee

    However I don't see how you imply that there can only be one instance in the whole lifecycle of the program because it's const. That sounds like a STATIC to me.

    Maybe you are confused there?

    Well .. I'm pretty sure I recall using it in a progam I did back in college to ensure that the EXACT same variable was being called even though it was being re-created (or something .. my memory get s alittle hazy, but I recall having to use it cause something was tryign to create an object was trying to create a second instance of that variable and .. OH .. now I think I recall more. I had an array of objects. But I needed a counter for something inside each of them that referenced a specific thingymabob .. I had to use const to ensure that the variable wasn't being recreated by each object)


Advertisement