Object names

This is a discussion on Object names within the C++ Programming forums, part of the General Programming Boards category; consider the following sample Code: class CmyClass { int someInt, otherInt; } myClass; what is the myClass name actually for? ...

  1. #1
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19

    Object names

    consider the following sample
    Code:
    class CmyClass
    {
        int someInt, otherInt;
    } myClass;
    what is the myClass name actually for? does it even serve a function?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what is the myClass name actually for?
    Code:
    int myInt;
    What is the myInt name actually for?
    My best code is written with the delete key.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    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
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  4. #4
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19
    Quote Originally Posted by Prelude
    >what is the myClass name actually for?
    Code:
    int myInt;
    What is the myInt name actually for?
    Ok listen... I guess you all think I'm ignorant because of my question there but you didn't quite get my point;

    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:
    Code:
    class CSomeClass
    {
    ...............
    } someName;  // <-- THIS is what I was talking about... there's no point in defining THESE names, is there?
    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 definition
    Code:
    CSomeClass someName; // there's no difference between the two is there??
    CSomeClass someOTHERname;
    As far as I can see, it makes no diference wether you define names after the class block or not. Am I right?
    Last edited by MisterT; 08-03-2006 at 02:53 PM.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    struct fadsfasdfajdkflajdsflakdhflkasdjflsadfja
    {
      int one;
      int two;
    } sillyname;
    
    sillyname thing;
    it's typedefing with structs, so I assume it's the same with classes.

    Code:
    struct fadsfasdfajdkflajdsflakdhflkasdjflsadfja
    {
      int one;
      int two;
    };
    
    fadsfasdfajdkflajdsflakdhflkasdjflsadfja thing;
    works too though
    Last edited by twomers; 08-03-2006 at 02:55 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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:
    Code:
    struct A {
      //...
    } a;
    
    typedef struct B {
      //...
    } b;
    a is an instance of the A structure, but b is a synonym for B.
    My best code is written with the delete key.

  7. #7
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19
    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.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    struct Fun {
    public:

    No need for the public - structs are by default public, I think. Did you mean a class?

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    The first fun is global. The second fun hides the first declaration. It's a local variable
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >// 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.

  11. #11
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19
    Quote Originally Posted by twomers
    struct Fun {
    public:

    No need for the public - structs are by default public, I think. Did you mean a class?
    Yes. my face is red. Changed it now

  12. #12
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19
    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?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Yes.

    Out of curiosity, if I put:

    Code:
    struct Fun {
      int howFun;
    } fun;
    inside main, would the ::fun.whatever=whateverelse, still work?

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Object Creation (and Management)...
    By Comrade_Yeti in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2005, 01:44 PM
  2. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM
  3. arrays as class names
    By Katle in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2002, 02:58 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM
  5. Testing object pointer for null
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21