Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointers to member functions and virtual inheritance

Reply
Thread Tools

pointers to member functions and virtual inheritance

 
 
alex.mizrahi@gmail.com
Guest
Posts: n/a
 
      09-12-2005
helo

i'm going to use pointers to member function with virtual inheritance
in following way:

struct base {
public:
typedef void (base::*fun_t)(void);
int i;
void call(fun_t f) {
(this->*f)();
}
};

class derived : virtual public base {
public:
int j;
};

class derived2 : public derived {
public:
void fun() {
std::cout<<"1";
}
void callcall() {
call(reinterpret_cast<fun_t>(fun));
}

};

it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
but works.
is it ok? (i mean it will work under MSVC7.1, and maybe later, not
abstract portability and stadard conformance).
are there situations when it will fail?

is there some better _lightweight_ approach? (not involving heavy stuff
like boost::function -- it's definitely nice, but it's too much..)

(i've read article http://www.codeproject.com/cpp/FastDelegate.asp ,
but i didn't understand details enough to understand if it will really
work always).

with best regards, Alex 'killer_storm' Mizrahi.

 
Reply With Quote
 
 
 
 
Oliver \(Nospam\)
Guest
Posts: n/a
 
      09-12-2005
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> i'm going to use pointers to member function with virtual inheritance
> in following way:
> struct base {
> public:
> typedef void (base::*fun_t)(void);
> int i;
> void call(fun_t f) {
> (this->*f)();
> }
> };
> class derived : virtual public base {
> public:
> int j;
> };
> class derived2 : public derived {
> public:
> void fun() {
> std::cout<<"1";
> }
> void callcall() {
> call(reinterpret_cast<fun_t>(fun));
> }
> };
> it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
> but works.
> is it ok? (i mean it will work under MSVC7.1, and maybe later, not
> abstract portability and stadard conformance).
> are there situations when it will fail?


Look, I do not actually know, what you are doing. Where do you need any
descendant class?
reinterpret_cast is a dangerous way to do anything, but be safe.

> is there some better _lightweight_ approach? (not involving heavy stuff
> like boost::function -- it's definitely nice, but it's too much..)


What about STL mem_fun?

Best regards // oliver


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      09-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> i'm going to use pointers to member function with virtual inheritance
> in following way:

[snip]
> it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
> but works.
> is it ok? (i mean it will work under MSVC7.1, and maybe later, not
> abstract portability and stadard conformance).
> are there situations when it will fail?

[snip]

At the very least this approach seems quite confusing, if not
unportable or flat out wrong. I'd probably prefer redesigning if that's
an option. Those who look at your code later would thank you. If that's
not possible, what constraints force you into this approach? Perhaps we
can suggest an alternate path that will also meet your constraints.

Cheers! --M

 
Reply With Quote
 
alex.mizrahi@gmail.com
Guest
Posts: n/a
 
      09-12-2005
> At the very least this approach seems quite confusing, if not
> unportable or flat out wrong. I'd probably prefer redesigning if that's
> an option.


well, i found odd things happen when i enable
pointers_to_members(full_generality), so i removed virtual inheritance,
and it became working without reinterpret_cast. instead inheriting from
class with virtual base i'm going to use it just as member, that's less
confusing and messy, i think..

as i understand, it's still is not perfectly correct to cast
pointer-to-member-of-derived to pointer-to-member-of-base, but it's
quite simple and should work always well.

 
Reply With Quote
 
Oliver \(Nospam\)
Guest
Posts: n/a
 
      09-12-2005
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> well, i found odd things happen when i enable
> pointers_to_members(full_generality), so i removed virtual inheritance,
> and it became working without reinterpret_cast. instead inheriting from
> class with virtual base i'm going to use it just as member, that's less
> confusing and messy, i think..


You do not know, what a virtual base class means.
"Virtual base classes offer a way to save space and avoid ambiguities in
class hierarchies that use multiple inheritance."

Where do you use multiple inheritance?

> as i understand, it's still is not perfectly correct to cast
> pointer-to-member-of-derived to pointer-to-member-of-base, but it's
> quite simple and should work always well.


This is not a serious opinion, Sir.

Best regards // oliver



 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-12-2005
* (E-Mail Removed):
>
> i'm going to use pointers to member function


There's almost _never_ any need for that.


> with virtual inheritance


Why?


> in following way:
>
> struct base {
> public:
> typedef void (base::*fun_t)(void);
> int i;
> void call(fun_t f) {
> (this->*f)();
> }
> };


There are no functions in 'base' that 'base::call' could call.


> class derived : virtual public base {
> public:
> int j;
> };


Huh.


> class derived2 : public derived {
> public:
> void fun() {
> std::cout<<"1";
> }
> void callcall() {
> call(reinterpret_cast<fun_t>(fun));
> }
>
> };


Undefined behavior.



> it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
> but works.
> is it ok?


No.


> (i mean it will work under MSVC7.1, and maybe later, not
> abstract portability and stadard conformance).


You mean -- will it seemingly "work" for some hypothetical non-conforming
compiler? That's impossible to say for us who don't have it. Try.


> are there situations when it will fail?


Yes, it's undefined behavior and can fail in any situation.



> is there some better _lightweight_ approach? (not involving heavy stuff
> like boost::function -- it's definitely nice, but it's too much..)


Depends what you're trying to do. Have you considered:

class Base
{
private:
virtual void foo() = 0;
public:
void call() { foo(); }
};

class Derived2: public Base
{
private:
virtual void foo() { std::cout<<"1"; }
public:
void callcall() { call(); }
};


> (i've read article http://www.codeproject.com/cpp/FastDelegate.asp ,
> but i didn't understand details enough to understand if it will really
> work always).


Are you trying to implement some kind of delegate functor?

--
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?
 
Reply With Quote
 
alex.mizrahi@gmail.com
Guest
Posts: n/a
 
      09-13-2005
> You do not know, what a virtual base class means.
> "Virtual base classes offer a way to save space and avoid ambiguities in
> class hierarchies that use multiple inheritance."


> Where do you use multiple inheritance?


do you want to see full code repository?
if i didn't show code that use multiple inheritance, it doesn't mean
it's not used. i know what's it

 
Reply With Quote
 
alex.mizrahi@gmail.com
Guest
Posts: n/a
 
      09-13-2005
> > is there some better _lightweight_ approach? (not involving heavy stuff
> > like boost::function -- it's definitely nice, but it's too much..)


> Depends what you're trying to do. Have you considered:
>
> class Base
> {
> private:
> virtual void foo() = 0;
> public:
> void call() { foo(); }
> };
>
> class Derived2: public Base
> {
> private:
> virtual void foo() { std::cout<<"1"; }
> public:
> void callcall() { call(); }
> };


there might be more than one function in Derived2 that should be passed
to call -- call(&Derived2::foo), call(&Derived2::bar). this will not
work with simple virtual functions..


> > (i've read article http://www.codeproject.com/cpp/FastDelegate.asp ,
> > but i didn't understand details enough to understand if it will really
> > work always).


> Are you trying to implement some kind of delegate functor?


yes. very simple degenerate case -- i do no have to pass "this" since
it's called in same class instance.
btw, how is boost::function and boost::bind implemented physically? is
there an article somewhere?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static member functions vs. member function pointers paul C++ 8 04-30-2009 11:03 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Member function pointers to member functions with default arguments Hamish C++ 3 01-25-2008 06:46 AM
Overloading and Inheritance with virtual member functions jois.de.vivre@gmail.com C++ 3 02-07-2007 05:36 PM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM



Advertisments