Static var question

Started by
15 comments, last by _Sigma 18 years, 5 months ago
Quote:Original post by _Sigma
and why is it
test::returnAB( t ); // returns 40

Can it be
test.returnAB( t ); // returns 40,
? cheers!

No. test is a namespace, and as such uses the namespace resolution operator.

CM
Advertisement
Quote:Original post by Conner McCloud
Quote:Original post by _Sigma
and why is it test::returnAB( t ); // returns 40
Can it be test.returnAB( t ); // returns 40,? cheers!

No. test is a namespace, and as such uses the namespace resolution operator.

CM


Though you can use t.returnAB( t ); because t is an instance of the class test.
how did that become a namespace? :S
Right. You can use both because returnAB is a member of the class nonetheless, though it can resemble a namespace. [edit2] It should be t.returnAB, not test.returnAB.

Also, instance is a constant reference to test object ( const test &instance ). You could implement returnAB similarly using a pointer:
static int returnAB( const test *instance ) {  return instance->a * b;}

You would then use it like this:
test t( 2 );test::returnAB( &t );

I chose to use a reference because you cannot have an invalid reference (unless you do something really weird), and because I do not need the concept of "no reference" in that example.


[edit] It is not a namespace. Conner McCloud must have skimmed over the code rather rapidly ;). [edit3] I think I see what you mean, CM. Disregard.



jfl.

[Edited by - jflanglois on November 4, 2005 10:48:34 PM]
Quote:Original post by _Sigma
how did that become a namespace? :S

All classes have a namespace [of the same name] associated with them.

CM
Quote:Original post by Conner McCloud
Quote:Original post by _Sigma
how did that become a namespace? :S

All classes have a namespace [of the same name] associated with them.

CM


Oh. Is it literally a namespace? How come I cannot use a namespace with the same name afterwards, then? [edit] Upon re-reading, I assume you mean that test.returnAB should be t.returnAB because test is effectively a namespace while he would need an instance to use that form?
Ok, ya, I meant t.blah not test.blah

I'm a tad confused, and my code isn't compiling, as this is my first time using MS VC instead of Borland. I'll read this tomorrow when I'm awake :P

This topic is closed to new replies.

Advertisement