consider the following sample
what is the myClass name actually for? does it even serve a function?Code:class CmyClass { int someInt, otherInt; } myClass;
consider the following sample
what is the myClass name actually for? does it even serve a function?Code:class CmyClass { int someInt, otherInt; } myClass;
>what is the myClass name actually for?
What is the myInt name actually for?Code:int myInt;
My best code is written with the delete key.
Let's see it this way...
Consider:
int myInt;
What is the purpose of Int? And the purpose of myInt?
Int defines a type and myInt is the name of a variable associated to that type.
Same with classes. Classes define a type. A user-defined type, to be more precise. Your CmyClass is the type name and myClass is the name of the variable.
CmyClass otherVar; declares otherVar of type CmyClass
EDIT: eh! I knew I should have checked before posting
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Ok listen... I guess you all think I'm ignorant because of my question there but you didn't quite get my point;Originally Posted by Prelude
I've been programming for years and I know what object names are for.. but, however, I'm new to C++ so I'm asking, again, what's the point of defining names like this:
As far as I know, the 'someName' name serves no purpose, since I can define objects of that same type, with names other than those specified after the class definitionCode:class CSomeClass { ............... } someName; // <-- THIS is what I was talking about... there's no point in defining THESE names, is there?
As far as I can see, it makes no diference wether you define names after the class block or not. Am I right?Code:CSomeClass someName; // there's no difference between the two is there?? CSomeClass someOTHERname;
Last edited by MisterT; 08-03-2006 at 02:53 PM.
it's typedefing with structs, so I assume it's the same with classes.Code:struct fadsfasdfajdkflajdsflakdhflkasdjflsadfja { int one; int two; } sillyname; sillyname thing;
works too thoughCode:struct fadsfasdfajdkflajdsflakdhflkasdjflsadfja { int one; int two; }; fadsfasdfajdkflajdsflakdhflkasdjflsadfja thing;
Last edited by twomers; 08-03-2006 at 02:55 PM.
>I guess you all think i'm ignorant because of my question
Hardly. I read your question as most people would and gave you the most logical answer. If you think I was being condescending, I apologize.
>what's the point of defining names like this:
I'm guess I'm not entirely sure what you're asking. Could you be more specific? Do you want to know why you can declare objects like that? Or do you want to know in what cases it would be useful?
>it's typedefing with structs, so I assume it's the same with classes
There's no typedef without the typedef keyword. The following two declarations are different:
a is an instance of the A structure, but b is a synonym for B.Code:struct A { //... } a; typedef struct B { //... } b;
My best code is written with the delete key.
Ok thanks I get it now...
But one more thing;
Code:class Fun { public: int howFun; } fun; // int main() { //how come these two statements both work? fun.howFun = 10; // since this code compiles, 'fun' is already instantized Fun fun; // so why can I instantize it again? fun.howFun = 3; // isn't that a bit weird? }
Last edited by MisterT; 08-03-2006 at 03:18 PM.
struct Fun {
public:
No need for the public - structs are by default public, I think. Did you mean a class?
The first fun is global. The second fun hides the first declaration. It's a local variable
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>// so why can I instantize it again?
You're playing a very subtle scope game. The first fun is declared in the global scope, and you can use it in main. However, the instant you declare the second fun, you've hidden the fun from the global scope and any unadorned use of fun uses the one declared in main:
Code:#include <iostream> using namespace std; struct Fun { int howFun; } fun; int main() { fun.howFun = 10; Fun fun; fun.howFun = 3; cout<< fun.howFun <<'\n'; // This uses the fun just declared cout<< ::fun.howFun <<'\n'; // This uses the global fun }
My best code is written with the delete key.
Yes. my face is red. Changed it nowOriginally Posted by twomers
Ok.. Consider this:
Code:class Fun {...............} fun; // <-- this defines a global Fun?? fun.howFun = 10; // this is the global Fun?? Fun fun; // and this creates a freshly baked LOCAL Fun, right? //so from here I must use; ::fun.howFun = 3; // to set the global HIDDEN one... // ...and... fun.howFun = 1; // to set the LOCAL one, right?
>right?
Right, but only if the only other fun is global. There isn't a way to access a non-global outer scope in the general sense:
Code:int main() { Fun fun; { Fun fun; // Hides the outer fun, no way to get to it } }
My best code is written with the delete key.
Yes.
Out of curiosity, if I put:
inside main, would the ::fun.whatever=whateverelse, still work?Code:struct Fun { int howFun; } fun;
Yes... however, were not the first fun global, and you would have no way of accessing it once its name was "shadowed". You can use the scope operator on this instance because it just happens the name was global. And the global scope has that special name (no name at all).
So a rule of thumb is to not reuse names in scopes contained one or more levels down.
All I'm saying is obviously wrong if you use namespaces
EDIT: oops... that was in reply to MisterT
EDIT2: Yup, Twoners. You are defining a new class that shadows the global one
Last edited by Mario F.; 08-03-2006 at 03:33 PM.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.