Thread: prototyping a class

  1. #1
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39

    prototyping a class

    Hello,

    I was wondering if it's possible to do something like this in c++, ie. prototype a class:

    Code:
    class ClassName;
    
    int main() {
    	ClassName test;
    	
    	return 0;
    }
    
    class ClassName {
    };
    Obviously the above dosn't work but I hope you understand what I'd like to accomplish here.
    I know you can prototype functions and such but I've been unsuccessfull in doing the same with class definitions.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's what header files are for.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can't instantiate a class like that, but you can do something like the following:

    Code:
    class ClassName;
    
    int main() {
    	ClassName* test;
    	
    	return 0;
    }
    
    class ClassName {
    };

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Forward declarations for classes are insufficient for creating instances for the simple reason that forward declarations only provide a hint that the class exists somewhere. In order for a class to be instantiated, the compiler needs to know exactly how big the class is, so it needs to have already encountered that class's definition.

    As cpjust mentioned, the most common approach is to declare your class in a separate header file to your main program.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Particularly because one of the benefits fo using a class is the code is reuseable. Thats true if its in a header and you canjust use that header in another project. Its not true if its in a cpp file and you have to strip it out to reuse it. Eventually you will end up putting it into a header anyway, so why not just start out doing that.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    And unless your writing template classes, you generally only include the class declaration in the header file and put the definition (the actual code) in a .cpp file.
    Ex.

    Code:
    // TestClass.h
    #ifndef TEST_CLASS_H_INCLUDED
    #define TEST_CLASS_H_INCLUDED
    
    class TestClass
    {
    public:
        TestClass();
        ~TestClass();
        const char* GetString() const;
        void SetString( const char* str );
    
    private:
        char* m_Str;
    };
    
    #endif  // TEST_CLASS_H_INCLUDED
    Code:
    // TestClass.cpp
    #include <cstring>
    #include <cassert>
    #include "TestClass.h"
    
    TestClass::TestClass()
    :   m_Str( NULL )
    {
        // Intentionally left blank.
    }
    
    TestClass::~TestClass()
    {
        delete [] m_Str;
    }
    
    const char*
    TestClass::GetString() const
    {
        return m_Str;
    }
    
    void
    TestClass::SetString( const char*  str )
    {
        assert( str != NULL );
    
        if ( m_Str != NULL )
        {
            delete [] m_Str;
        }
    
        m_Str = new char( strlen( str ) + 1 );
        strcpy( m_Str, str );
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Careful about those forward declarations. While they may be good, they can also scew things up. If the compiler only has a forward declaration when you use the class in your code (pointer to or references to that class are exceptions), the compiler doesn't know anything about the class and can't generate the correct code. For example, it can't call the constructor or destructor.

    In your .cpp files, always include the actual header file for the declaration. In header files, when you have functions that take pointers or references to a class, you can get away with using a forward declaration.
    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.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe the only time you need a forward declaration of a class is when you have two classes that both refer to each other.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Usually, yes (can also happen with circular includes)... but when they do, you get the same above mentioned problem! All references to the forward declared class must be either a pointer or a reference. Otherwise you get problems.
    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.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by cpjust View Post
    I believe the only time you need a forward declaration of a class is when you have two classes that both refer to each other.
    This is probably the only time you need them but it might be a good idea to use them any time you can.

    The idea is to include as few other headers in a header as possible to reduce dependencies between headers. So if a class is only used to declare a pointer or reference in a header, then a forward declaration would be fine.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    The real reason I asked this was because I would like to replace the standard main function:

    Code:
    int main(int *args, char *argv[]) {
            return 0;
    }
    with something like this:

    With a header file:
    Code:
    class Main;
    
    int main(int *args, char *argv[]) {
            Main prog(args, argv);
    
            return prog.Start();
    }
    And the program 'entry point':
    Code:
    class Main {
            public:
                    Main(int *args, char *argv[]);
    
                    int Start() {
                            /*....*/
                            
                            return 0;
                    }
    };
    Where Argument is a class that has already parsed the arguments making them accessible thru a hash table or dictionary.

    The idea was to have the 'real' main function in a header file and the entry point that the programmer would see was instead a class called Main or something like that. Suppose it was a pretty stupid idea to start with anyway.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you're doing is bad, because you're trying to use the class while it's still undefined (because you're only using a forward declaration telling the compiler it exists).
    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.

  13. #13
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    I should have added that the posted code dosn't work either, it was just how I 'visioned' it.

    I settled on something like this instead, skipping the whole class thing:

    Code:
    extern int Main(const Dict<String> &argv);
    
    int main(int args, char *argv[]) {
    	Dict<String> arguments = ParseArgs(args, argv);
    	
    	return Main(arguments);
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you want to do this, though? What's wrong with the initial "main"?
    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.

  15. #15
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    Quote Originally Posted by Elysia View Post
    Why do you want to do this, though? What's wrong with the initial "main"?
    The idea was to 'abstract away' the handling of the arguments, if that makes any sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Prototyping a class
    By Inquirer in forum Game Programming
    Replies: 8
    Last Post: 08-05-2002, 09:59 PM