Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Forward declaration and typedefs

Reply
Thread Tools

Forward declaration and typedefs

 
 
Simon Elliott
Guest
Posts: n/a
 
      01-07-2005
For some time I've been using typedefs for STL containers for reasons
outlined in:

http://www.gotw.ca/gotw/046.htm

However a major downside to this is that you can't forward declare a
typedef of a STL container:

#include <vector>
struct foo
{
int f1;
};
typedef std::vector<foo> fooVector;
// Can't forward declare fooVector

However, by deriving a class from the container, it becomes possible to
forward declare it:

class fooVectorublic std::vector<foo>{};

This appears to allow me to treat fooVector as a std::vector<foo> and
to forward declare it:

class fooVector; // OK

In general it's not recommended to inherit from STL containers because
they don't have virtual destructors. Am I correct in thinking that the
above usage is safe because I'm not dealing with the fooVector class
polymorphically?

Is there any way to improve on the above so that other developers don't
inadvertantly use fooVector in a dangerous manner?


--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      01-07-2005
Simon Elliott wrote:
...
>
> In general it's not recommended to inherit from STL containers because
> they don't have virtual destructors.


Nonsense. It's perfectly safe to inherit from objects that don't have
virtual destructors.

Am I correct in thinking that the
> above usage is safe because I'm not dealing with the fooVector class
> polymorphically?


see above.

>
> Is there any way to improve on the above so that other developers don't
> inadvertantly use fooVector in a dangerous manner?


Get better developers.
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      01-07-2005

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:41debb25$0$23051$.. .
> For some time I've been using typedefs for STL containers for reasons
> outlined in:
>
> http://www.gotw.ca/gotw/046.htm
>
> However a major downside to this is that you can't forward declare a
> typedef of a STL container:
>
> #include <vector>
> struct foo
> {
> int f1;
> };
> typedef std::vector<foo> fooVector;
> // Can't forward declare fooVector
>
> However, by deriving a class from the container, it becomes possible to
> forward declare it:
>
> class fooVectorublic std::vector<foo>{};
>
> This appears to allow me to treat fooVector as a std::vector<foo> and
> to forward declare it:
>
> class fooVector; // OK
>
> In general it's not recommended to inherit from STL containers because
> they don't have virtual destructors. Am I correct in thinking that the
> above usage is safe because I'm not dealing with the fooVector class
> polymorphically?
>
> Is there any way to improve on the above so that other developers don't
> inadvertantly use fooVector in a dangerous manner?
>


I'm not sure why you would want to forward declare that in the first
place...? A forward declaration is needed when you have a dependency on an
external class (or between classes in the same header), such as having a
pointer to another class inside your class. But if you're including the
<vector> header in your header, and you've successfully typedef'd it there,
what's to prevent you from including in instance of (or pointer to) that new
type it in your class? Why do you need to forward declare it? Like this:

// .h file
#ifndef MYVEC_H
#define MYVEC_H

#include <vector>

typedef std::vector<int>MyVector;

class VectorHolder
{
MyVector m_vector;
};

#endif
// end .h file

That works for me. What problem are you having that requires a forward
declaration?

-Howard



-Howard



 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-07-2005

> pointer to another class inside your class. But if you're including the
> <vector> header in your header, and you've successfully typedef'd it
> there, what's to prevent you from including in instance of (or pointer to)
> that new type it in your class? Why do you need to forward declare it?
> Like this:
>


Should read:

> ...But if you're including the <vector> header in your header, and you've
> successfully typedef'd it there, what's to prevent you from including an
> instance of (or pointer to) that new type in your class?



 
Reply With Quote
 
Simon Elliott
Guest
Posts: n/a
 
      01-07-2005
On 07/01/2005, Howard wrote:

> // .h file
> #ifndef MYVEC_H
> #define MYVEC_H
>
> #include <vector>
>
> typedef std::vector<int>MyVector;
>
> class VectorHolder
> {
> MyVector m_vector;
> };
>
> #endif
> // end .h file


Let's call this header myvec.hpp.

> That works for me. What problem are you having that requires a
> forward declaration?


Two reasons:

1/ Elimination of headers. Suppose I want to have a pointer or
reference to MyVector in another header, but I don't want to include
myvec.hpp in that header. This reduces compile time dependencies and is
considered a Good Thing.

2/ Circular references. The classic example is when you have two
classes which need to contain a pointer to each other:

class bar; // Need this forward declaration to get this to compile

class foo
{
bar* bar_ptr_;
};

class bar
{
foo* foo_ptr_;
};

--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
Simon Elliott
Guest
Posts: n/a
 
      01-07-2005
On 07/01/2005, Gianni Mariani wrote:
> > In general it's not recommended to inherit from STL containers
> > because they don't have virtual destructors.

>
> Nonsense. It's perfectly safe to inherit from objects that don't
> have virtual destructors.


It's only safe if you can be sure that no-one is going to try to use
the objects polymorphically.

--
Simon Elliott http://www.ctsn.co.uk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forward decleration of enumerations and typedefs in a NameSpace kasiyil C++ 3 08-21-2006 08:33 PM
Forward declarations, templates, and typedefs Noah Roberts C++ 7 05-02-2006 11:02 PM
template & typedefs declaration problem krema2ren@gmail.com C++ 5 12-08-2005 01:05 PM
Forward typedefs Jorge Yáñez C++ 2 12-17-2004 05:37 PM
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57