Thread: Hiding / avoiding / postponing type?

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Hiding / avoiding / postponing type?

    So I have a simple class that I want to take a functor:
    Code:
    template<typename T>
    class CMyMFCEditBrowseCtrl: public CMFCEditBrowseCtrl
    {
    public:
    	CMyMFCEditBrowseCtrl(T OnAfterUpdateFunc): m_OnAfterUpdateFunc(OnAfterUpdateFunc) {}
    	virtual void OnAfterUpdate() { m_OnAfterUpdateFunc(); }
    
    protected:
    	T m_OnAfterUpdateFunc;
    };
    Now, since this is a derived class that merely overrides a virtual function, it must be in my header:

    Code:
    class CVideoEncoderforN8Dlg : public CDialogEx
    {
    	...
    	CMyMFCEditBrowseCtrl<?> m_VideoCtrl;
    	...
    };
    But there is the problem. I need to put in a functor, so I don't know the type. And since I cannot define the functor inside the header, I cannot deduce the type.
    So I need to hide or post-pone the type in some way. So this is what I wanted to ask. What is good solution for this kind of situation? What are some good techniques?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not sure where exactly is the problem, but

    Code:
    class CMyMFCEditBrowseCtrl: public CMFCEditBrowseCtrl
    {
            typedef std::function<void()> AfterUpdateFun;
    public:
    	CMyMFCEditBrowseCtrl(AfterUpdateFun OnAfterUpdateFunc): m_OnAfterUpdateFunc(OnAfterUpdateFunc) {}
    	virtual void OnAfterUpdate() { m_OnAfterUpdateFunc(); }
    
    protected:
    	AfterUpdateFun m_OnAfterUpdateFunc;
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is a problem because it means I cannot do
    Code:
    CMyMFCEditBrowseCtrl b(&A::test);
    Hence the templated type T, which gives me the opportunity to pass in a function pointer, functor or member function (though via a functor).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A std::function accepts all of those too?

    Code:
    CMyMFCEditBrowseCtrl b(std::function<void()>(std::bind(&A::test, some_a)));
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, so it does. Long time last since I used boost::bind.
    It seems like an acceptable solution, for now, at least.

    Although, still, this seems like a compromise. What if I have to do this with a generic class taking one or more functors?
    I would either have to rewrite them or use some other solution.
    Last edited by Elysia; 12-08-2010 at 05:25 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Unfortunately the only other way I know is to make class CVideoEncoderforN8Dlg a template too. But I think you knew that because you said:

    ... And since I cannot define the functor inside the header, I cannot deduce the type.
    About that I do not understand. Functors are types, so you can separate the implementation from design like normal. If the functors themselves are templates, then you can define them in a separate header and include them later. So there would have to be some other reason why you can't deduce the type. I do not think the inheritance precludes this.
    Last edited by whiteflags; 12-08-2010 at 09:37 PM. Reason: mnntioned the wrong type

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM