473,396 Members | 1,987 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,396 software developers and data experts.

Abstract base class + Functor

Hi all,

I'm struggling a bit with Functors generated for an ABC.

This is the functor code:

class Functor{
public:
virtual double operator(double x)=0
}

template <class T> class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
}
public:
SpecFunctor(T* _obj, double(T::*_fpt)(double x))
{
obj = _obj; fpt = _fpt;
}

virtual operator()(double x)
{
(*obj.*fpt)(x);
}

Now, this works fine for 'ordinary' classes T. However, I would like to have
it working for an ABC. When I use an ABC for T, I get warnings from g++
that obj_ will get initialzed after... I more or less understand why the
warnings appear (you cannot create an object for an ABC).

It's merely warnings, but I would like to clean it up. Is there a way to do
this without giving up the Functor or ABC?

Thanks for any tips,

gert

Jul 19 '05 #1
6 5908
On Tue, 23 Sep 2003 10:12:41 +0200, Gert Van den Eynde
<gv******@hotmail.com> wrote:
Hi all,

I'm struggling a bit with Functors generated for an ABC.

This is the functor code:

class Functor{
public:
virtual double operator(double x)=0
}

template <class T> class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
}
public:
SpecFunctor(T* _obj, double(T::*_fpt)(double x))
{
obj = _obj; fpt = _fpt;
}

virtual operator()(double x)
{
(*obj.*fpt)(x);
}

Now, this works fine for 'ordinary' classes T. However, I would like to have
it working for an ABC. When I use an ABC for T, I get warnings from g++
that obj_ will get initialzed after... I more or less understand why the
warnings appear (you cannot create an object for an ABC).
You don't show any code that tries to create an object of abstract
type.

It's merely warnings, but I would like to clean it up. Is there a way to do
this without giving up the Functor or ABC?


Once I'd fixed the syntax errors, added virtual destructors, etc., it
worked fine:

class Functor{
public:
virtual ~Functor(){}
virtual double operator()(double x) = 0;
};

template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt)(double x))
:obj(obj), fpt(fpt)
{
}

virtual double operator()(double x)
{
return (*obj.*fpt)(x);
}
};

struct ABClass
{
~ABClass(){}
virtual double f(double d) = 0;
};

struct Derived: ABClass
{
virtual double f(double d)
{
return d * d;
}
};

#include <iostream>

int main()
{
Derived d;
Functor* f = new SpecFunctor<ABClass>(&d, &ABClass::f);
std::cout << (*f)(10) << '\n';
}

Or was that not what you meant?

Tom
Jul 19 '05 #2
>
Or was that not what you meant?


Yes, indeed. But compiling your code with g++ -Wall gives the same warnings:
test.cc:26: warning: `struct ABClass' has virtual functions but non-virtual
destructor
test.cc:32: warning: `struct Derived' has virtual functions but non-virtual
destructor
test.cc: In constructor `SpecFunctor<T>::SpecFunctor(T*, double
(T::*)(double))
[with T = ABClass]':
test.cc:44: instantiated from here
test.cc:12: warning: `SpecFunctor<ABClass>::obj' will be initialized after
test.cc:11: warning: `double (ABClass::*SpecFunctor<ABClass>::fpt)(double

The code works, as did mine, I just want to get rid of the warnings... I
suppose that when the compiler generates them, there must be a cleaner
way,no?

gert

Jul 19 '05 #3
Gert Van den Eynde wrote:
Or was that not what you meant?


Yes, indeed. But compiling your code with g++ -Wall gives the same
warnings:
test.cc:26: warning: `struct ABClass' has virtual functions but
non-virtual destructor


struct ABClass
{
virtual ~ABClass(){}
virtual double f(double d) = 0;
};

TADA :-)

--
Attila aka WW
Jul 19 '05 #4
> struct ABClass
{
virtual ~ABClass(){}
virtual double f(double d) = 0;
};

TADA :-)


That one I figured out myself ;-) shouldn't have copy/pasted it... It's the
'will be initialized after' that I wonder about...

gert
Jul 19 '05 #5
"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:bk**********@naxos.belnet.be...
[...]
That one I figured out myself ;-) shouldn't have copy/pasted it... It's
the 'will be initialized after' that I wonder about...


template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt)(double x))
:obj(obj), fpt(fpt)
{
}
// ...
};

The order of initialization is determined by the order of the data
members within the class, not the order of initializers in the c'tor.
Therefore, if you give a different order in the c'tor, the compiler
is warning you that it will not respect that ordering. To eliminate
the warning, make the initializer list match the data member
ordering.

Dave
Jul 19 '05 #6
On Tue, 23 Sep 2003 11:25:07 +0200, Gert Van den Eynde
<gv******@hotmail.com> wrote:
struct ABClass
{
virtual ~ABClass(){}
virtual double f(double d) = 0;
};

TADA :-)


That one I figured out myself ;-) shouldn't have copy/pasted it... It's the
'will be initialized after' that I wonder about...


That's a good example of GCC's nanny warnings, where it annoys you by
complaining about non-errors. Change the functor to:
template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(double x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt)(double x))
:fpt(fpt), obj(obj) //re-ordered these
//to match the declaration order.
{
}

virtual double operator()(double x)
{
return (*obj.*fpt)(x);
}
};

Tom
Jul 19 '05 #7

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

Similar topics

8
by: Pete C | last post by:
In this section of the FAQ: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10 an abstract destructor is given a definition with the cryptic comment "It's faster this...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
3
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
0
by: ma740988 | last post by:
Consider # include <iostream> # include <vector> # include <typeinfo> # include <string> class BaseMsg { friend std::ostream& operator<<( std::ostream&, const BaseMsg& ); std::string name...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...

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.