Thread: Forward declaration of classes

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You don't need the definition of a class to declare a function with that class as the return type. This little example compiles without problems:
    Code:
    class B;
    
    class A
    {
    public:
            B getB();
    };
    
    class B : public A
    {
    };
    
    B A::getB() { return B(); }
    
    int main()
    {
            A a;
            B b(a.getB());
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    this was not a requirement of my software design parameters. I was just wondering if there was a way it could be done.

    This class hierarchy has a class called Object as its base, and a class called String derives publicly from Object. I wanted Object to have a virtual ToString() function that would return a String type. it's beginning to look like this is not possible. it's not really a setback though. since I was planning a ToString() member function in each derived class anyway, It doesn't really matter that it's not a virtual function.

    All of your advice and suggestions have been wonderful, and I appreciate all of you taking your time to help with my question.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    You don't need the definition of a class to declare a function with that class as the return type. This little example compiles without problems:
    Code:
    class B;
    
    class A
    {
    public:
            B getB();
    };
    
    class B : public A
    {
    };
    
    B A::getB() { return B(); }
    
    int main()
    {
            A a;
            B b(a.getB());
    }
    That one kind of surprised me for a second; but then I realized it's only if you want a B member variable in A that you need to make it a pointer...

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it's beginning to look like this is not possible.
    CornedBee showed an example that compiles and does exactly what you describe. Can you try compiling his example?

    >> This little example compiles without problems
    But that's not guaranteed in the standard, is it?

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    >> This little example compiles without problems
    But that's not guaranteed in the standard, is it?
    Why not?

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure and I don't remember, but there was mention of a reason on here a few weeks (months?) ago. Something about how some compilers might require the size of that object to set up the stack properly or something.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Only at call time and function definition time. Not for the declaration.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Forward Declaration
    By BigDaddyDrew in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 12:15 PM