iccaros Posted January 11, 2006 Report Share Posted January 11, 2006 (edited) ok if you have a funtion to get a int and it goes like thisint getX () const{ x = Xvalue;}what is the const outside the () mean..I mean I think a const means that this function can not change it, the value of xValue that isis it there because in the header file x is definded? so you are not reciving anything, as its there, but the funtion must be constant ?(here is my construtor Point3::Point( int xValue, int yValue) :x(xValue ), y(yValue){} ) Edited January 11, 2006 by iccaros Quote Link to post Share on other sites
Hai-Etlik Posted January 11, 2006 Report Share Posted January 11, 2006 I've only seen a const modifier like that on methods. In which case it forces (or at least tries to force) the method to not have any side effects on the receiver.If it's valid on non-methods, I would assume it prevents them from altering non local variables (no side effects). In which case the example you gave would seem to violate that restriction. It also has a return type but no return statement. Quote Link to post Share on other sites
Hai-Etlik Posted January 11, 2006 Report Share Posted January 11, 2006 I've only seen a const modifier like that on methods. In which case it forces (or at least tries to force) the method to not have any side effects on the receiver.If it's valid on non-methods, I would assume it prevents them from altering non local variables (no side effects). In which case the example you gave would seem to violate that restriction. It also has a return type but no return statement. -- After a bit of searching, it looks like const is only valid on methods, not stand along functions.Looking more carefuly at your post it looks likely that it is a method as you refer tyo a constructor (it's useful to know things like this) and that x is a member of that object?. If that's the case then the getX() method should fail to compile unless x is declared mutable (ignores constness). Besides this it should at least generate a warnning for having a return type but failing to have a return statement.If you want an "accessor method", you probably want.int Point::getX() const { return x;}If VALUE is a pointer, non copyable object, or object that is costly to copy. you probably want to return it as a const pointer or reference.Foo* const Point::getAFoo() const { return foo;}std::string& const Pointer::getString() const { return stringRep;}Note that the const goes after the * or & so as to say that it is what is pointed at or refered to that is constant, not the pointer or reference itself.const char* is a constant pointer to a mutable (C style) string.char* const is a mutable pointer to a constant string.Finaly your constructor is wrong, it needs to have the same name as the class it is the constructor for. A constractor for class Point3 can not be named Point, it must be named Point3. Quote Link to post Share on other sites
iccaros Posted January 11, 2006 Author Report Share Posted January 11, 2006 (edited) Finaly your constructor is wrong, it needs to have the same name as the class it is the constructor for. A constractor for class Point3 can not be named Point, it must be named Point3.thanks I notced that 5 in before lookin back at this page.. I was wondering why it showed up under other in the codeblock ide.also here is the full class and class cpp , I apresate your explanation, still don't understnad it (well), I get that adding const behind it it means the funtion can not alter non local varables (or privet varables in the class) but I thought you did that by making it a pointer (as I call the & or * magic that no book or 5 c++ classese faild to explain well) or decalrig const double or something..thanks.. trying to really understand this time...Point3.h#ifdef POINT3_H#define POINT3_Hclass Point3 {public: Point3 ( int = 0 , int =0 ); ~Point3 (); void setX (int); int getX() const; void setY (int); int getY () const; void print() const;private: int x; int y;};#endifPoint3.cpp#include<iostream>using std::cout;#include"Point3.h"Point3::Point3( int xValue, int yValue) :x(xValue ), y(yValue){}void Point3::setX(int xValue){ x = xValue;}int Point3::getX () const{ return x;}void Point3::setY(int yValue){ y = yValue;}int Point3::getY() const{ return y}void Point3::print() const{ cout << '[' << getX() << ", " << getY() << ']';}Point3::~Point3{ cout << "Point3 deconstructor"; print(); cout endl;}thanks.. Edited January 11, 2006 by iccaros Quote Link to post Share on other sites
Hai-Etlik Posted January 12, 2006 Report Share Posted January 12, 2006 (edited) I apresate your explanation, still don't understnad it (well), I get that adding const behind it it means the funtion can not alter non local varables (or privet varables in the class) but I thought you did that by making it a pointer (as I call the & or * magic that no book or 5 c++ classese faild to explain well) or decalrig const double or something..const at the end of the header of a method (member function) means that it does not change the object or class (static data). Unless that data is declared mutable (Caches, scratch areas, that sort of thing) or you do something ugly like a cast. Generaly if you have a "get" method (read accessor) you will want it to be const like this.Const at the start of a data type means that variables of that type can not be altered. If it's a class, only const methods can be called.const int, const Pointer3if the type is a pointer or reference, then it means that the POINTER can not be changed but what it points to is OK to change.const std::string&, const Pointer3*if the type is a pointer/reference and the const comes after the pointer/reference charater, then the pointer can be changed, but what it points to can not.std::string& const, Pointer3* constif you have const before and after, then you can't change the pointer OR the thing pointed to.const std::string& const, const Pointer3* constAs for the difference between a pointer (*) and a reference (&), a reference is basicly a pointer that is automaticaly dereferenced for you.void c_function (int* ref_arg){ do_something(*ref_arg)}int foo;c_function(&foo);void cpp_function(int& ref_arg){ do_something(ref_arg);}int foo;cpp_function(foo);Note how with a regular pointer, you have to get a pointer to foo (&foo) and then dereference that pointer (*ref_arg) With a reference, this is all done automaticaly. It's not realy all that useful and it doesn't deal with any of the real difficulties of using pointers (Making sure what is pointed to is realy there, getting rid of things when you are done with them, and it can open type safety holes)Point3.h#ifdef POINT3_H#define POINT3_HShould be #ifndef not #ifdef you want to declare the class if it HASN'T been declared, not if it allready has.Point3.cppint Point3::getY() const{ return y} Edited January 12, 2006 by Hai-Etlik Quote Link to post Share on other sites
iccaros Posted January 12, 2006 Author Report Share Posted January 12, 2006 Thanks for the explanation.. the code is from the teachers assinment, so I have not looked for (minior) errors, I had found some like the class incorrectly done.. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.