473,804 Members | 3,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Member and static functions while assignment

I have the following problem.

class Base
{
public:
void (*pfunc) (int) = 0;
};

class Derived : public Base
{
public:
void (*pfunc) (int a)
{
// Stuff
}
};
class Foo
{
private:
void (*pf) (int);
public:
Foo (Base* ptr)
{
pf = ptr->pfunc; // Error, because pfunc is a member function,
not static
}
};

Is there any way to do something like "pf = ptr->pfunc"?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 19 '05 #1
13 1681
Alex Vinokur wrote:
I have the following problem.

class Base
{
public:
void (*pfunc) (int) = 0;
This line won't compile. It's neither a static integral constant nor a
pure virtual function, but you seem to have mixed the syntax for the
two.
};

class Derived : public Base
{
public:
void (*pfunc) (int a)
You can't define a body for a pointer to a function. Please see the FAQ
for this group on how to post code:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
{
// Stuff
}
};
class Foo
{
private:
void (*pf) (int);
public:
Foo (Base* ptr)
{
pf = ptr->pfunc; // Error, because pfunc is a member function,
not static
You have a lot bigger problems than that!
}
};

Is there any way to do something like "pf = ptr->pfunc"?


Yes. See the FAQs:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M

Oct 19 '05 #2
Alex Vinokur wrote:
I have the following problem.

class Base
{
public:
void (*pfunc) (int) = 0;
A pointer to a function taking an int and returning void.
};

class Derived : public Base
{
public:
void (*pfunc) (int a)
Huh? Another pointer to a function...
{
// Stuff
Are you trying to define a body after declaring a pointer to a function?
}
};
class Foo
{
private:
void (*pf) (int);
OK, a pointer to a function.
public:
Foo (Base* ptr)
{
pf = ptr->pfunc; // Error, because pfunc is a member function,
not static
}
};

Is there any way to do something like "pf = ptr->pfunc"?


What are you trying to accomplish here? Perhaps read about the template
'std::binder_1s t', you can do something like

pf = std::bind1st(&B ase::pfunc, ptr);

although it is a bit convoluted, but should get you what you want, IIUYC.

Next time try to show what you intend to do with 'pf'.

V
Oct 19 '05 #3

"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Alex Vinokur wrote:
I have the following problem.

class Base
{
public:
void (*pfunc) (int) = 0;


This line won't compile. It's neither a static integral constant nor a
pure virtual function, but you seem to have mixed the syntax for the
two.

[snip]

My first message was unlucky. Please ignore it.

Here is problematic program.
====== foo1.cpp ======
struct Base
{
virtual void func (int) = 0;
};

struct Derived : public Base
{
void func (int)
{
// Stuff
}
};

struct Foo
{
void (*pfunc) (int);
Base* p_;

Foo (Base* p) : p_ (p) {}

void doit ()
{

// Here is what I want to reach
pfunc = p_->func;

// ------------------------
// g++ 4.0.1 produces the following error message:
// ............... .........
// error: argument of type 'void (Base::)(int)'
// does not match 'void (*)(int)
// ............... .........
// because func() is _member_ (not static) function
// ------------------------
}
};
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;

return 0;
}
=============== =======
Here is a _partial_ solution of the problem.
====== foo2.cpp ======
struct Base
{
virtual void func (int) = 0;
};

struct Derived : public Base
{
void func (int)
{
// Stuff
}
};

struct Foo
{
void (*pfunc) (int);
static Base* p_s;

Foo (Base* p) { p_s = p; }

static void wrapper(int a)
{
p_s->func (a);
}

void doit ()
{
pfunc = wrapper;
}
};
Base* Foo::p_s = 0;
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;

return 0;
}

=============== =======
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 19 '05 #4
Alex Vinokur wrote:
"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Alex Vinokur wrote:
I have the following problem.

class Base
{
public:
void (*pfunc) (int) = 0;
This line won't compile. It's neither a static integral constant nor a
pure virtual function, but you seem to have mixed the syntax for the
two.


[snip]

My first message was unlucky. Please ignore it.

Here is problematic program.
====== foo1.cpp ======
struct Base
{
virtual void func (int) = 0;


Add:

virtual ~Base() {}
};

struct Derived : public Base
{
void func (int)
{
// Stuff
}
};

struct Foo
{
void (*pfunc) (int);
Base* p_;

Foo (Base* p) : p_ (p) {}

void doit ()
{

// Here is what I want to reach
pfunc = p_->func;
And then what? How do you intend to use 'pfunc'?

// ------------------------
// g++ 4.0.1 produces the following error message:
// ............... .........
// error: argument of type 'void (Base::)(int)'
// does not match 'void (*)(int)
// ............... .........
// because func() is _member_ (not static) function
// ------------------------
}
};
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;

return 0;
}
=============== =======
Here is a _partial_ solution of the problem.
Which problem?
====== foo2.cpp ======
struct Base
{
virtual void func (int) = 0;
};

struct Derived : public Base
{
void func (int)
{
// Stuff
}
};

struct Foo
{
void (*pfunc) (int);
static Base* p_s;

Foo (Base* p) { p_s = p; }

static void wrapper(int a)
{
p_s->func (a);
}

void doit ()
{
pfunc = wrapper;
}
};
Base* Foo::p_s = 0;
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;

return 0;
}

=============== =======


Study this:

////////////////////////////////////
struct Base
{
virtual void func (int) = 0;
virtual ~Base() {}
};

struct Derived : public Base
{
void func(int)
{
// Stuff
}
};

struct Foo
{
void (Base::*pfunc) (int);

Foo (void (Base::*pf) (int)) : pfunc(pf) {}

void doit(Base* p_)
{
(p_->*pfunc)(42);
}
};
int main ()
{
Base* p = new Derived;
Foo foo(&Base::func );

foo.doit(p);

delete p;
}
/////////////////////////////////////////

V
Oct 19 '05 #5
Alex Vinokur wrote:
My first message was unlucky. Please ignore it.

Here is problematic program.
====== foo1.cpp ======
struct Base
{
virtual void func (int) = 0;
};

struct Derived : public Base
{
void func (int)
{
// Stuff
}
};

struct Foo
{
void (*pfunc) (int);
Base* p_;

Foo (Base* p) : p_ (p) {}

void doit ()
{

// Here is what I want to reach
pfunc = p_->func;

// ------------------------
// g++ 4.0.1 produces the following error message:
// ............... .........
// error: argument of type 'void (Base::)(int)'
// does not match 'void (*)(int)
// ............... .........
// because func() is _member_ (not static) function
// ------------------------
}
};

Ok, now you've posed the problem correctly, and in order to find the
solution, all you need to do is read the FAQs to which I previously
referred you:

http://www.parashift.com/c++-faq-lit...o-members.html

[snip] Here is a _partial_ solution of the problem.
====== foo2.cpp ======
struct Base
{
virtual void func (int) = 0;
};

struct Derived : public Base
{
void func (int)
{
// Stuff
}
};

struct Foo
{
void (*pfunc) (int);
static Base* p_s;

Foo (Base* p) { p_s = p; }

static void wrapper(int a)
{
p_s->func (a);
}

void doit ()
{
pfunc = wrapper;
}
};
Base* Foo::p_s = 0;
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;

return 0;
}


This is not a good solution. It is not necessary to resort to static
members and methods. See the FAQs.

Cheers! --M

Oct 19 '05 #6

Victor Bazarov wrote:
[snip]
Study this:

////////////////////////////////////
struct Base
{
virtual void func (int) = 0;
virtual ~Base() {}
};

struct Derived : public Base
{
void func(int)
{
// Stuff
}
};

struct Foo
{ --------------------------------- void (Base::*pfunc) (int); But here I have to use
void (*pfunc) (int);
---------------------------------
Foo (void (Base::*pf) (int)) : pfunc(pf) {}

void doit(Base* p_)
{
(p_->*pfunc)(42);
}
};
int main ()
{
Base* p = new Derived;
Foo foo(&Base::func );

foo.doit(p);

delete p;
}
/////////////////////////////////////////

V

Alex Vinokur

Oct 23 '05 #7
* Alex Vinokur:

I have to use
void (*pfunc) (int);

[to call a member function]


If you have any leeway at all, consider using a functor object instead. That
can encapsulate an ordinary static function, or a member function bound to an
object (sometimes called a _closure_ or a _delegate_), or whatever. See the
FAQ items at the end of the FAQ page that "mlimber" pointed to.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 23 '05 #8

Alf P. Steinbach wrote:
* Alex Vinokur:

I have to use
void (*pfunc) (int);

[to call a member function]


If you have any leeway at all, consider using a functor object instead. That
can encapsulate an ordinary static function, or a member function bound to an
object (sometimes called a _closure_ or a _delegate_), or whatever. See the
FAQ items at the end of the FAQ page that "mlimber" pointed to.

[snip]

Thanks.

I saw the FAQ.

But it seems that it doesn't solve my problem.
Here my situation.

struct BaseBar
{
// Stuff: Data members
virtual void callback (int) = 0;
virtual ~BaseBar() {}
};

struct Bar1 : public BaseBar
{
// Stuff: Data members
void callback (int)
{
// Stuff
}
};
struct Bar2 : public BaseBar
{
// Stuff: Data members
void callback (int)
{
// Stuff
}
};

struct Config
{
// Stuff: Data members
void (*the_callback) (int); // We can't change this prototype

};

struct Foo
{
// Stuff: Data members
Config config_;
Foo (BaseBar* ptr_i)
{
// Stuff
// Here I have to do what is impossible to do:
// -----------------------
// config_.the_cal lback = ptr_i->callback;
// -----------------------
}
};

int main ()
{
Bar1 bar1;
Bar2 bar2;

Foo foo1 (&bar1);
Foo foo2 (&bar2);
Foo foo3 (&bar1);

return 0;
}
The approach from
http://www.parashift.com/c++-faq-lit....html#faq-33.2
doesn't help here because we have several Bar-ojects created.
Alex Vinokur

Oct 23 '05 #9
* Alex Vinokur:
void (*the_callback) (int); // We can't change this prototype
The approach from
http://www.parashift.com/c++-faq-lit....html#faq-33.2
doesn't help here because we have several Bar-ojects created.


If the callback is only called during initialization I don't see why not.

Otherwise, if you can specify the int argument to the callback, then use a
std::map as the global, not just a single pointer, and the int argument as key
to the map.

Otherwise, if you can't specify the argument then it seems the API you're
using supports only a single callback which can be called at any time, and you
then have to decide whether you want just a single object to receive the call
or whether to distribute it to all objects, or what.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 23 '05 #10

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

Similar topics

2
2846
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the dependency/independency on the lifecycle of the object, what is the significant differences between public member functions and public static member functions?
5
4837
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
30
3501
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
4627
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
8
4597
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
3
1711
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
7
12462
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have blindly searched for the answer but I still not thoroughly
5
1356
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float Re ( float re_ ) { return ( re = re_ ); }
2
17392
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
22
2784
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
0
9707
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
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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
10323
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
9161
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, 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...
1
7622
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 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...
0
6856
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2997
bsmnconsultancy
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...

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.