Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

could anyone explain this?

Started by E-Coli Jul 3, 2004 at 1:56 PM 13 replies 1.4k views
Original Post
E-Coli
E-Coli
i was reading a book on 3D maths in c++ and i came across this: class RotationMatrix { public: ... ... ... Vector3 inertialToObject(const Vector3 &v) const; Vector3 objectToInertial(const Vector3 &v) const; }; what does the const at the end of the function do? thanks.
What d'you mean I believe in GOD!?!...Talk to him every day!
Ranor
Ranor
It means that the method may not modify any member data. This also means that the method can't call any member methods that aren't 'const'.
ApochPiQ
ApochPiQ
You can also call const functions without instantiating the class. For example:

Vector3 SomeVector = RotationMatrix::objectToInertial(SomeOtherVector);


With a non-const function, you have to first create a copy of the class:

RotationMatrix SomeMatrix;Vector3 SomeVector = SomeMatrix.objectToInertial(SomeOtherVector);
graveyard filla
graveyard filla
i didnt know that... so putting const at the end will make it behave kind of like it was static ?
FTA, my 2D futuristic action MMORPG
bytecoder
bytecoder
Quote:

You can also call const functions without instantiating the class. For example:

Vector3 SomeVector = RotationMatrix::objectToInertial(SomeOtherVector);



With a non-const function, you have to first create a copy of the class:



RotationMatrix SomeMatrix;

Vector3 SomeVector = SomeMatrix.objectToInertial(SomeOtherVector);

I've never heard of that. Are you sure you aren't referring to static? Once you think about it that really doesn't make any sense, because it is possible to access member variables. Here's what I mean:
class Test{    int i;public:    int getI() const    {        return i;    }};int i = Test::getI(); // what should this return if it were possible
E-Coli
E-Coli
that useful to know!thanks.

one more question.
can u use inline functions in a class, like so:

class blah
{
public:
...
...
inline int blah_blah();
}

whenever i've done it the compiler has a fit!

thanks again!
What d'you mean I believe in GOD!?!...Talk to him every day!
MagicScript
MagicScript
Quote:
Original post by bytecoder
Quote:

You can also call const functions without instantiating the class. For example:

Vector3 SomeVector = RotationMatrix::objectToInertial(SomeOtherVector);



With a non-const function, you have to first create a copy of the class:



RotationMatrix SomeMatrix;

Vector3 SomeVector = SomeMatrix.objectToInertial(SomeOtherVector);

I've never heard of that. Are you sure you aren't referring to static? Once you think about it that really doesn't make any sense, because it is possible to access member variables. Here's what I mean:
class Test{    int i;public:    int getI() const    {        return i;    }};int i = Test::getI(); // what should this return if it were possible


You are correct and ApochPiQ is incorrect.
ApochPiQ
ApochPiQ
Quote:
Original post by MagicScript
You are correct and ApochPiQ is incorrect.


Yeah, you're right... that was a brain fart. Ignore me [wink]
graveyard filla
graveyard filla
wait.. i dont understand.. what WOULD happen in bytecoders example? i mean, if i is a member variable, and getI() is a const function, then you do

int i = Test::getI();

what happends? i mean, what is returned? since i is a member data, who owns that data? whos i is it?
FTA, my 2D futuristic action MMORPG
Brother Bob
Brother Bob
Quote:
Original post by graveyard filla
wait.. i dont understand.. what WOULD happen in bytecoders example? i mean, if i is a member variable, and getI() is a const function, then you do

int i = Test::getI();

what happends? i mean, what is returned? since i is a member data, who owns that data? whos i is it?

You would get a compiler error, because getI is not a static membeber function, so it needs an object to be called, like this.
Test foo;int i = foo.getI();

The i that "belongs to" foo would be returned in this case.
graveyard filla
graveyard filla
ok, so Apoch was wrong when he said "You can also call const functions without instantiating the class. For example:

Vector3 SomeVector = RotationMatrix::objectToInertial(SomeOtherVector);

"

?? just got confused because MagicScript said he WAS right, but now your saying hes wrong...
FTA, my 2D futuristic action MMORPG
MagicScript
MagicScript
Quote:
Original post by graveyard filla
ok, so Apoch was wrong when he said "You can also call const functions without instantiating the class. For example:

Vector3 SomeVector = RotationMatrix::objectToInertial(SomeOtherVector);

"

?? just got confused because MagicScript said he WAS right, but now your saying hes wrong...



bytecoder was illustrating why what ApochPiQ said couldn't be right. He stopped short of actually confirming that his example failed to compile, so I indicated that he was in fact correct.
Spudder
Spudder
You can only call static methods without an object as static member methods don't have a this pointer so it's impossible for them to refer to non-static member variables which is why you can call them without an object of the class the function is contained in.
ApochPiQ
ApochPiQ
Moral of the story: if you post articles for people, be sure you read them first so you know what you're talking about [wink]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.