473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

g++ typedef inheritance

Hi there,

I'm having a struggle with GNU g++ (3.3.5) and inheritance of typedefs in
STL containers.
I'm trying to port some old code I wrote with MS Visual C++ and it looks
like there is a discrepancy in the STL implementation or at least
interpretation of the standard (I trust g++ most on the latter).
To the case:

While this compiles fine:

-------------------
class B {
public:
typedef int foo;
};

class C : public B {
public:
void bar() {
foo a;
}
};
-------------

This doesn't work:
------------------
template <typename T, template <typename ELEM> class CONT = std::vector>
class pointainer : public CONT<T *>
{
public:
void foo() {
CONT<T *>::iterator it;
}
};
------------
tpltest.cc: In member function `void pointainer<T, CONT>::foo()':
tpltest.cc:9: error: parse error before `;' token
The vector iterator is defined in include/g++-v3/bits/stl_vector.h like
this:
typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;

Another thing is that I'm not sure that I'm on the right track when I
try to overwrite some functionalities of STL containers by inheritance.
I have been using a pointainer by Yonat Sharon in visual c++ for a long time
that was implemented in a similar way, but this pointainer doesn't compile
with g++.
The purpose of the pointainer is to automatically call the dtor of it's
objects when they are removed from the pointainer (or when the pointainer
is destructed). Yonat implemented that by overwriting many of the key
methods of the stl container. Since these methods are non-virtual, it would
give problems if the pointainer is used in a mother STL class context.

Currently I only see the solution of using composition instead of
inheritance which should clean any semantic problems, but will throw out my
free lunch. However I'm still curious why g++ wouldn't accept the stl
iterator usage.
---
Søren Holstebroe - http://www.holstebroe.dk
Jul 23 '05 #1
2 2673
Søren Holstebroe wrote:
Hi there,

I'm having a struggle with GNU g++ (3.3.5) and inheritance of typedefs in
STL containers.
I'm trying to port some old code I wrote with MS Visual C++ and it looks
like there is a discrepancy in the STL implementation or at least
interpretation of the standard (I trust g++ most on the latter).
To the case:

While this compiles fine:

-------------------
class B {
public:
typedef int foo;
};

class C : public B {
public:
void bar() {
foo a;
}
};
-------------

This doesn't work:
------------------
template <typename T, template <typename ELEM> class CONT = std::vector>
class pointainer : public CONT<T *>
{
public:
void foo() {
CONT<T *>::iterator it;
}
};
It isn't supposed to. Try:

typename CONT<T *>::iterator it;

------------
tpltest.cc: In member function `void pointainer<T, CONT>::foo()':
tpltest.cc:9: error: parse error before `;' token
The vector iterator is defined in include/g++-v3/bits/stl_vector.h like
this:
typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;

This is valid.
Another thing is that I'm not sure that I'm on the right track when I
try to overwrite some functionalities of STL containers by inheritance.
I have been using a pointainer by Yonat Sharon in visual c++ for a long
time that was implemented in a similar way, but this pointainer doesn't
compile with g++.
The purpose of the pointainer is to automatically call the dtor of it's
objects when they are removed from the pointainer (or when the pointainer
is destructed). Yonat implemented that by overwriting many of the key
methods of the stl container. Since these methods are non-virtual, it
would give problems if the pointainer is used in a mother STL class
context.

Currently I only see the solution of using composition instead of
inheritance which should clean any semantic problems, but will throw out
my free lunch.


What would you gain by that? You still couldn't use it in a context where
the base class is needed. If you just want to prevent anyone from trying
this, you could inherit privately and get the member functions you want to
keep into your derived class with using declarations.

Jul 23 '05 #2
Rolf Magnus wrote:
Søren Holstebroe wrote:
This doesn't work:
------------------
template <typename T, template <typename ELEM> class CONT = std::vector>
class pointainer : public CONT<T *>
{
public:
void foo() {
CONT<T *>::iterator it;
}
};


It isn't supposed to. Try:

typename CONT<T *>::iterator it;

This works. Thank you.
Currently I only see the solution of using composition instead of
inheritance which should clean any semantic problems, but will throw out
my free lunch.


What would you gain by that? You still couldn't use it in a context where
the base class is needed. If you just want to prevent anyone from trying
this, you could inherit privately and get the member functions you want to
keep into your derived class with using declarations.


My purpose was to semantically prevent base usage so private inheritance is
a solution. I just could get my using statements right and was in doubt
that this was the right track to follow anyhow. My overall purpose is to
make hide all the ugly memory management while still being able to use
containers of base class pointers with simple semantics. Ie. I want my
programs to be as readable, semantically simple and maintainable as my Java
programs.

The following naïve pointainer works with g++:

----------
#include <vector>
#include <iostream>

template <typename T, template <typename ELEM> class CONT = std::vector>
class pointainer : CONT<T *>
{
public:
using CONT<T *>::iterator;
using CONT<T *>::const_iterator;
using CONT<T *>::push_back;
using CONT<T *>::begin;
using CONT<T *>::end;

~pointainer() {
for (typename CONT<T *>::const_iterator it=begin();it!=end();it++) {
delete *it;
}
}

};

using namespace std;

class A {
public:
int v;
A(int _v) : v(_v) {cout << "A::ctor"<<endl;}
~A() {cout << "A::~dtor"<<endl;}
};

int main() {
pointainer<A> p;
p.push_back(new A(5));
p.push_back(new A(42));

for (pointainer<A>::const_iterator it=p.begin();it!=p.end();it++) {
cout << (*it)->v << endl;
}

return 0;
}
----------
Output is as expected:

A::ctor
A::ctor
5
42
A::~dtor
A::~dtor

--------------

Søren Holstebroe - http://www.holstebroe.dk
Jul 23 '05 #3

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Thomas Matthews | last post by:
Hi, I have a hierarchy of classes and would like to create an array of pointers to member functions that could also contain pointers to the parent member functions as well. What is the syntax...
6
by: Keith H Duggar | last post by:
Often I've felt that the ability to alias an existing type where the alias would be treated as a different type thus allowing overloading, etc would be very useful. typedef : alias an existing...
7
by: Jürgen Kaminski | last post by:
Hello all, I got some legacy code where a template implements a non-templated interface. These classes are really HUGE, but it boils down to the following structure: class BaseClass { public:...
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
4
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector...
8
by: Ben Pope | last post by:
Hi everybody, I know this is not usually the place for design questions, but I was wondering what some of you more experienced people would do. Say I have an application that has a C++ & STL...
3
by: Doug Eleveld | last post by:
Hi Everyone, I have been playing aroung with 'object-oriented' C for a while now and I have come up with an interesting way to simulate C++ inheritance and non-member functions in C in a 100%...
8
by: Carmen Sei | last post by:
I found in C++ a type is typedef several times and alot of time, I need to dig further to see the original type. like LPDWORD - it's a strange type, but when i dig further, i see it's a (DWORD...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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
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,...
0
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...

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.