Question about classes, and which way is best

This is a discussion on Question about classes, and which way is best within the C++ Programming forums, part of the General Programming Boards category; I was thinking about this earlier, and it seemed a little unusual. I was hoping that you guys may be ...

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Question about classes, and which way is best

    I was thinking about this earlier, and it seemed a little unusual. I was hoping that you guys may be able to give me an answer to it.

    If a class is declared like so:
    Code:
    class functions{
    public:
    void function1();
    };
    
    functions Function;
    Then in the main code, both of the following ways of calling function1() will work. Which one is the best way of doing it, and what is the advantage(s) of doing that way? And why do we declare an object instance if it isn't essential?

    Code:
    int main()
    {
    // Lots of previous code
    functions::function1();
    // Some more code
    }
    Code:
    int main()
    {
    // Lots of previous code
    Function.function1();
    // Some more code
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first of your main() samples will only work if function1 is a static function. This also means that there is no "this" pointer, and there is no need for an object. This is useful for special cases, such as when you just want a bunch of functions, but you have no data inside the class.

    In most cases, you want the second version, which automatically passes the object to the function, and you can easily get to the functions member variables and functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by Swarvy View Post
    Code:
    int main()
    {
    // Lots of previous code
    functions::function1();
    // Some more code
    }
    With your current class definition, this doesn't work. This only works on static class members.

    Code:
    int main()
    {
    // Lots of previous code
    Function.function1();
    // Some more code
    }
    This is the real way of calling a class method.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    cheers guys. That makes alot of sense. I'm glad you've clarified that.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And just to clarify - one isn't "better" than the other, just like a bin lorry isn't better than a fire-engine - they just serve different purposes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    lol. Not too many "bin lorries" on this side of the pond. And NONE in Texas!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    lol. Not too many "bin lorries" on this side of the pond. And NONE in Texas!
    No, suppose they are called refuse lorries - funny that both the US and England sort of speak the same language, yet the language is quite different in places. (There's quite a few web-sites about the difference between British English and US English).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    "Garbage Truck"
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

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