473,893 Members | 1,588 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:publi c 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 10375
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:publi c 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
1877
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
4531
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 to gather various different classes into a common namespace using typedefs: class QWidget {}; class MyListview {}; namespace gui
23
3880
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
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
6004
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
9289
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
1997
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. foo.h: struct Bar;
6
8627
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 myfile.h template<typename T,typename R> class some_class;
11
8349
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
9982
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11243
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10837
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10469
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9645
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8021
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7173
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4684
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 we have to send another system
2
4280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.