pointers to member functions and virtual inheritance

Discussion in 'C++' started by alex.mizrahi@gmail.com, Sep 12, 2005.

  1. Guest

    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.
     
    , Sep 12, 2005
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Oliver \(Nospam\), Sep 12, 2005
    #2
    1. Advertising

  3. mlimber Guest

    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
     
    mlimber, Sep 12, 2005
    #3
  4. Guest

    > 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.
     
    , Sep 12, 2005
    #4
  5. <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Oliver \(Nospam\), Sep 12, 2005
    #5
  6. * :
    >
    > 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?
     
    Alf P. Steinbach, Sep 12, 2005
    #6
  7. Guest

    > 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 :)
     
    , Sep 13, 2005
    #7
  8. Guest

    > > 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?
     
    , Sep 13, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    10
    Views:
    757
    Marcus Kwok
    Dec 8, 2006
  2. Replies:
    3
    Views:
    355
  3. Hamish
    Replies:
    3
    Views:
    580
    Alf P. Steinbach
    Jan 25, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. paul
    Replies:
    8
    Views:
    715
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page