473,473 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor Filter Through... ?


You know the whole "Linked List" concept, where each object knows where the
the next one is. Well, in one of its simplest forms I have:

template<class T>
struct LinkedObject : virtual public T
{
LinkedObject* p_next;
};
The only problem here is in calling the constructor:
#include <string>

int main()
{
LinkedObject<std::string> blah("Monkey!");

blah.p_next = new LinkedObject<std::string>("Ape!");
delete blah.p_next;
}
Is there any way to achieve something akin to the above, whereby the call to
the derived class's constructor "filters through" to the base class's
constructor... ?

What I'm trying to achieve is that an object of type "LinkedObject
<std::string>" will behave exactly as would an object of type
"std::string" - the only difference being that you can do:

blah.p_next = 0;
As such, I want the call to the constructor of "LinkedObject<T>" to fall
through to the constructor of the base class.
-JKop
Jul 22 '05 #1
3 1382

"JKop" <NU**@NULL.NULL> wrote in message
news:rf*******************@news.indigo.ie...

You know the whole "Linked List" concept, where each object knows where the the next one is. Well, in one of its simplest forms I have:

template<class T>
struct LinkedObject : virtual public T
{
LinkedObject* p_next;
};
The only problem here is in calling the constructor:
#include <string>

int main()
{
LinkedObject<std::string> blah("Monkey!");

blah.p_next = new LinkedObject<std::string>("Ape!");
delete blah.p_next;
}
Is there any way to achieve something akin to the above, whereby the call to the derived class's constructor "filters through" to the base class's
constructor... ?

What I'm trying to achieve is that an object of type "LinkedObject
<std::string>" will behave exactly as would an object of type
"std::string" - the only difference being that you can do:

blah.p_next = 0;
As such, I want the call to the constructor of "LinkedObject<T>" to fall
through to the constructor of the base class.
-JKop


You can add generic templated constructors to LinkedObject, like this

template<class T>
struct LinkedObject : virtual public T
{
LinkedObject() {}
template <class A> LinkedObject(const A& a) : T(a) {}
template <class A, class B> LinkedObject(const A& a, const B& b) : T(a,
b) {}
template <class A, class B, class C> LinkedObject(const A& a, const B&
b, const C& c) : T(a, b, c) {}
// etc etc, go as high as you like
LinkedObject* p_next;
};

This works because the constructor will only be instantiated if it is used.
Therefore its not a problem if T doesn't have all the constructors that
LinkedObject is assuming. Does need a default constructor however.

john
Jul 22 '05 #2
John Harrison posted:
You can add generic templated constructors to LinkedObject, like this

template<class T>
struct LinkedObject : virtual public T
{
LinkedObject() {}
template <class A> LinkedObject(const A& a) : T(a) {}
template <class A, class B> LinkedObject(const A& a, const B& b) :
T(a,
b) {}
template <class A, class B, class C> LinkedObject(const A& a,
const B&
b, const C& c) : T(a, b, c) {}
// etc etc, go as high as you like
LinkedObject* p_next;
};

This works because the constructor will only be instantiated if it is
used. Therefore its not a problem if T doesn't have all the
constructors that LinkedObject is assuming. Does need a default
constructor however.

john

Thanks.

But shouldn't the function arguments be *non*-const?, as in:
template <class A> LinkedObject(A& a) : T(a) {}

template <class A, class B> LinkedObject(A& a, B& b) : T(a,b) {}

template <class A, class B, class C> LinkedObject(A& a, B& b, C& c) : T(a,
b, c) {}
template <class A, class B, class C, class D> LinkedObject(A& a, B& b, C& c,
D& d) : T(a,b,c,d){}

This way, if the expressions supplied as arguments to the constructor are
actually const, then the argument types will automagically become const too.
Is there any potential pitfalls in this... I don't see any at the moment.
-JKop
Jul 22 '05 #3

"JKop" <NU**@NULL.NULL> wrote in message
news:HB*******************@news.indigo.ie...
John Harrison posted:
You can add generic templated constructors to LinkedObject, like this

template<class T>
struct LinkedObject : virtual public T
{
LinkedObject() {}
template <class A> LinkedObject(const A& a) : T(a) {}
template <class A, class B> LinkedObject(const A& a, const B& b) :
T(a,
b) {}
template <class A, class B, class C> LinkedObject(const A& a,
const B&
b, const C& c) : T(a, b, c) {}
// etc etc, go as high as you like
LinkedObject* p_next;
};

This works because the constructor will only be instantiated if it is
used. Therefore its not a problem if T doesn't have all the
constructors that LinkedObject is assuming. Does need a default
constructor however.

john

Thanks.

But shouldn't the function arguments be *non*-const?, as in:
template <class A> LinkedObject(A& a) : T(a) {}

template <class A, class B> LinkedObject(A& a, B& b) : T(a,b) {}

template <class A, class B, class C> LinkedObject(A& a, B& b, C& c) : T(a,
b, c) {}
template <class A, class B, class C, class D> LinkedObject(A& a, B& b, C&

c, D& d) : T(a,b,c,d){}

This way, if the expressions supplied as arguments to the constructor are
actually const, then the argument types will automagically become const too.

Is there any potential pitfalls in this... I don't see any at the moment.


But then you won't be able to call any of your constructors with a
temporary.

I was looking at some code that did this just the other day. I don't have
access to it right now but I think they used const references, but said
something like 'if you want non-const references use something like the
boost ref library'.

John
Jul 22 '05 #4

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

Similar topics

1
by: jj23 | last post by:
Hi all, I am really new to XML and have been asked to do a simple task o filtering out records from an XML source. The source XML however, i hard to understand and I can't quite figure out how...
1
by: Robert Neville | last post by:
I would like to add filter functionality to my database whether through the Main form or the subform. This question may be rudimentary, yet I have not less experience with filtering data outside...
4
by: Nhmiller | last post by:
This is directly from Access' Help: "About designing a query When you open a query in Design view, or open a form, report, or datasheet and show the Advanced Filter/Sort window (Advanced...
9
by: Thomas Scheiderich | last post by:
I am curious as to 2 things. One is the order that the constructors are handled and the other is why the debugger doesn't show the static objects constructor (unless you set a breakpoint. For...
5
by: Paul de Goede | last post by:
I set the Response.Filter in my aspnet application but I have noticed that if you do a Server.Transfer that the filter doesn't get called. And in actual fact the response is mostly empty. It seems...
12
by: Dave A | last post by:
I have a class that does not have a default constructor. The nature of the class inherently excludes it from having one and to put one in will blatantly misrepresent the object that it is modelling....
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
3
by: franc sutherland | last post by:
Hello, I have a report which I filter using the me.filter command in the OnOpen event. Me.Filter = "OrderID=" & Forms!variable_form_name! Me.FilterOn = True I want to be able to open that...
0
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
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
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
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
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
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
muto222
php
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.