forward declaration for a type definition

Discussion in 'C++' started by aaragon, Nov 20, 2007.

  1. aaragon

    aaragon Guest

    Hello, I was wondering if it is possible to forward declare a type
    definition. If so, what is the way to do it? I found a couple of
    messages where they say it's not possible but there must be a way to
    do it.
    Thank you.

     
    aaragon, Nov 20, 2007
    #1
    1. Advertising

  2. aaragon

    David Harmon Guest

    On Mon, 19 Nov 2007 18:43:58 -0800 (PST) in comp.lang.c++, aaragon
    <> wrote,
    >Hello, I was wondering if it is possible to forward declare a type
    >definition. If so, what is the way to do it?


    Sure, for example,

    class foobar;

    Of course the type is "incomplete" without the full definition and there
    are limits to what you can do with it; you can create pointers to it but
    not instances of it.
     
    David Harmon, Nov 20, 2007
    #2
    1. Advertising

  3. aaragon

    Kai-Uwe Bux Guest

    aaragon wrote:

    > Hello, I was wondering if it is possible to forward declare a type
    > definition.


    You can forward declare classes.

    You cannot, however, forward declare an identifier as a type name to be
    defined (e.g., by typedef) later.

    > If so, what is the way to do it? I found a couple of
    > messages where they say it's not possible but there must be a way to
    > do it.


    Why?

    If you are in a situation where you write code for a type (not necessarily
    just a class) yet unknown, consider writing a template.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 20, 2007
    #3
  4. aaragon

    aaragon Guest

    On Nov 19, 10:16 pm, Kai-Uwe Bux <> wrote:
    > aaragon wrote:
    > > Hello, I was wondering if it is possible to forward declare a type
    > > definition.

    >
    > You can forward declare classes.
    >
    > You cannot, however, forward declare an identifier as a type name to be
    > defined (e.g., by typedef) later.
    >
    > > If so, what is the way to do it? I found a couple of
    > > messages where they say it's not possible but there must be a way to
    > > do it.

    >
    > Why?
    >
    > If you are in a situation where you write code for a type (not necessarily
    > just a class) yet unknown, consider writing a template.
    >
    > Best
    >
    > Kai-Uwe Bux


    I'm using code from a library so I need to create type definitions for
    specific types. However, the type definition is defined by using a
    templated class that uses it at the same time. Something like this:

    typedef Class<MyOwnClass1, MyOwnClass2> ClassA;

    // Then, in the definition of MyOwnClass
    template <class SomeClass>
    class MyOwnClass {

    // uses type definition ClassA
    someFun(ClassA&);

    };

    // and then the actual instantiations of MyOwnClass
    typedef MyOwnClass<SomeClass1> MyOwnClass1;
    typedef MyOwnClass<SomeClass2> MyOwnClass2;

    so you can see, that the class MyOwnClass depends on types that are
    not yet instantiated.

    The way I solved this is by moving the typedefinition of MyOwnClass1
    and MyOwnClass2 before the first typedef and using a forward
    declaration of the template class MyOwnClass. Very messy eh???? It
    turns out that this cyclic dependence is because of the use of a
    Visitor class that I need to have for MyOwnClass (visitor design
    pattern).

     
    aaragon, Nov 20, 2007
    #4
  5. aaragon wrote:
    > On Nov 19, 10:16 pm, Kai-Uwe Bux <> wrote:
    >> aaragon wrote:
    >>> Hello, I was wondering if it is possible to forward declare a type
    >>> definition.

    >>
    >> You can forward declare classes.
    >>
    >> You cannot, however, forward declare an identifier as a type name to
    >> be defined (e.g., by typedef) later.
    >>
    >>> If so, what is the way to do it? I found a couple of
    >>> messages where they say it's not possible but there must be a way to
    >>> do it.

    >>
    >> Why?
    >>
    >> If you are in a situation where you write code for a type (not
    >> necessarily just a class) yet unknown, consider writing a template.
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > I'm using code from a library so I need to create type definitions for
    > specific types. However, the type definition is defined by using a
    > templated class that uses it at the same time. Something like this:
    >
    > typedef Class<MyOwnClass1, MyOwnClass2> ClassA;
    >
    > // Then, in the definition of MyOwnClass
    > template <class SomeClass>
    > class MyOwnClass {
    >
    > // uses type definition ClassA
    > someFun(ClassA&);


    What's wrong with just declaring 'ClassA' a class instead of making
    it a typedef? To declare a function that accepts a reference to it
    the compiler only needs to know that it's a class (and not an array
    or a function, for example).

    >
    > };
    >
    > // and then the actual instantiations of MyOwnClass
    > typedef MyOwnClass<SomeClass1> MyOwnClass1;
    > typedef MyOwnClass<SomeClass2> MyOwnClass2;
    >
    > so you can see, that the class MyOwnClass depends on types that are
    > not yet instantiated.
    >
    > The way I solved this is by moving the typedefinition of MyOwnClass1
    > and MyOwnClass2 before the first typedef and using a forward
    > declaration of the template class MyOwnClass. Very messy eh???? It
    > turns out that this cyclic dependence is because of the use of a
    > Visitor class that I need to have for MyOwnClass (visitor design
    > pattern).
    >
    > a²


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 20, 2007
    #5
  6. aaragon

    aaragon Guest

    On Nov 20, 11:11 am, "Victor Bazarov" <> wrote:
    > aaragon wrote:
    > > On Nov 19, 10:16 pm, Kai-Uwe Bux <> wrote:
    > >> aaragon wrote:
    > >>> Hello, I was wondering if it is possible to forward declare a type
    > >>> definition.

    >
    > >> You can forward declare classes.

    >
    > >> You cannot, however, forward declare an identifier as a type name to
    > >> be defined (e.g., by typedef) later.

    >
    > >>> If so, what is the way to do it? I found a couple of
    > >>> messages where they say it's not possible but there must be a way to
    > >>> do it.

    >
    > >> Why?

    >
    > >> If you are in a situation where you write code for a type (not
    > >> necessarily just a class) yet unknown, consider writing a template.

    >
    > >> Best

    >
    > >> Kai-Uwe Bux

    >
    > > I'm using code from a library so I need to create type definitions for
    > > specific types. However, the type definition is defined by using a
    > > templated class that uses it at the same time. Something like this:

    >
    > > typedef Class<MyOwnClass1, MyOwnClass2> ClassA;

    >
    > > // Then, in the definition of MyOwnClass
    > > template <class SomeClass>
    > > class MyOwnClass {

    >
    > > // uses type definition ClassA
    > > someFun(ClassA&);

    >
    > What's wrong with just declaring 'ClassA' a class instead of making
    > it a typedef? To declare a function that accepts a reference to it
    > the compiler only needs to know that it's a class (and not an array
    > or a function, for example).
    >
    >
    >
    >
    >
    > > };

    >
    > > // and then the actual instantiations of MyOwnClass
    > > typedef MyOwnClass<SomeClass1> MyOwnClass1;
    > > typedef MyOwnClass<SomeClass2> MyOwnClass2;

    >
    > > so you can see, that the class MyOwnClass depends on types that are
    > > not yet instantiated.

    >
    > > The way I solved this is by moving the typedefinition of MyOwnClass1
    > > and MyOwnClass2 before the first typedef and using a forward
    > > declaration of the template class MyOwnClass. Very messy eh???? It
    > > turns out that this cyclic dependence is because of the use of a
    > > Visitor class that I need to have for MyOwnClass (visitor design
    > > pattern).

    >
    > > a²

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    ClassA is defined by a library. It is actually a class template, so
    the correct type has to be obtained through a type definition.
     
    aaragon, Nov 20, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    604
    Jonathan Turkanis
    Feb 15, 2004
  2. verec
    Replies:
    2
    Views:
    1,047
    verec
    Jun 25, 2005
  3. Pierre Yves
    Replies:
    2
    Views:
    491
    Pierre Yves
    Jan 10, 2008
  4. Sybolt de Boer
    Replies:
    6
    Views:
    738
    Michael Tsang
    Nov 29, 2009
  5. Luca Forlizzi
    Replies:
    4
    Views:
    464
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page