473,503 Members | 10,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forward declaration and typedefs

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 fooVector:public 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
Jul 22 '05 #1
5 10359
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.
Jul 22 '05 #2

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:41***********************@news.gradwell.net.. .
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 fooVector:public 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

Jul 22 '05 #3
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?

Jul 22 '05 #4
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
Jul 22 '05 #5
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1854
by: Jorge Yáñez | last post by:
Hello, It's possible to do a forward declaration of a typedef? Something similar to typedef TMyType; // typedef forward declaration void f ( TMyType& t )
2
4490
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
23
3819
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
2
508
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
7
5967
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
3
9259
by: Till Crueger | last post by:
Hi, I am trying to implement a tree in C and I have the folowing code: struct inner { struct node *left; struct node *right; }; struct leaf {
28
1942
by: Tom Plunket | last post by:
Hey gang; In C++ I often used forward declarations to allow me to get away with not including additional files in headers, if in fact all I needed was to know that a type existed. e.g. ...
6
8603
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
11
8292
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.