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)(doubl e x);
T* obj;
}
public:
SpecFunctor(T* _obj, double(T::*_fpt )(double x))
{
obj = _obj; fpt = _fpt;
}
virtual operator()(doub le 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 6 5922
On Tue, 23 Sep 2003 10:12:41 +0200, Gert Van den Eynde
<gv******@hotma il.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)(doubl e x); T* obj; } public: SpecFunctor( T* _obj, double(T::*_fpt )(double x)) { obj = _obj; fpt = _fpt; }
virtual operator()(doub le 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()(doub le x) = 0;
};
template <class T>
class SpecFunctor: public Functor
{
private:
double (T::*fpt)(doubl e x);
T* obj;
public:
SpecFunctor(T* obj, double(T::*fpt) (double x))
:obj(obj), fpt(fpt)
{
}
virtual double operator()(doub le 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<ABC lass>(&d, &ABClass::f) ;
std::cout << (*f)(10) << '\n';
}
Or was that not what you meant?
Tom
> 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<AB Class>::obj' will be initialized after
test.cc:11: warning: `double (ABClass::*Spec Functor<ABClass >::fpt)(doubl e
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
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
> 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
"Gert Van den Eynde" <gv******@hotma il.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)(doubl e 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
On Tue, 23 Sep 2003 11:25:07 +0200, Gert Van den Eynde
<gv******@hotma il.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)(doubl e 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()(doub le x)
{
return (*obj.*fpt)(x);
}
};
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 way, trust me". Why?
Is it faster to type? I notice that the derived classes in that example
seem mysteriously not to require a destructor.
|
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
define the behavior for most, but not all of its members. The derived
classes will define the behavior for the remaining members (the undefined
members).
I'd like to force the derived classes to implement the undefined members in
the base class. I...
|
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 the need to have abstract
class types in C#?
Derived classes from abstract base classes must overrided the abstract
method defininition anyway, so why not just provide a complete definition
for the abstract method when defining the containing...
|
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
|
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 looked for one on the net (I did browse the
boost library though). It doesn't matter right now, anyway.
To put it simply, Abstract Iterator is mainly a wrapper class. It helps
| |
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 magic, I
wrote the following module. It is mainly useful as a light weight tool
to help programmers catch mistakes at definition time (e.g., forgetting
to implement a method required by the given interface). This is handy
when unit tests or...
|
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 iteration, which
forces me to have certain interface on the Iterator class.
struct Element {
virtual ~Element() = 0;
};
|
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 right?
And, why not use "new" instead of using "virtual"?
And the last question, what is the differences between a abstract method and
a interface?
|
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 ;
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |