473,657 Members | 2,609 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 10365
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
1862
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
4506
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
3841
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
5985
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
9268
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
1964
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
8612
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
8310
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
8397
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
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
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();...
0
4158
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
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
1957
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.