Thread: templated functions and derivation

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    templated functions and derivation

    Hello

    I have a derived and base class.
    From derived class I call templated functions in base class.

    For instance:

    Code:
    class base {
    public:
    	base() { }
    	
    	template <typename T>
    	void somefunction(T t) {
    		callback(t);
    	}
    	
    	template <typename T> 	// I want to make it templated to call derived back with templated argument
    	virtual void callback(T t) = 0;
    
    }
    
    class derived : public base {
    public:
    	derived() { }
    	
    	void callsomefunction() {
    		sometype someinst;
    		somefunction(someinst);
    	}
    	
    	void callback(sometype t) {	
    	}
    
    }
    Now I want to callsomefunction() from derived class.
    How could I callback (virtual function) from base class with templated argument as you can see in the code?
    How could this be possible?
    Do I have to make templated base class?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have never seen a virtual function template before... but really, what is the bigger picture of what you are trying to solve?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Would making that function a friend help?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    I call function to serialize data (any object type)..
    When data arrives back, base class calls derived class with the same type of data..

    Until now I just called virtual function without arguments.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Member function templates cannot be virtual. Sorry. It's just not implementable with reasonable effort.

    For example, in the most common way of implementing virtual functions, the vtable, you'd need a vtable entry for each template instantiation. How does the compiler know how these are ordered, though? What if one compilation unit contains different instantiations than another? With normal functions, the linker can just fold them together. With virtual functions, that's not possible.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    How could I solve this problem then?
    Should I make templated class and make typedef inside it that would be used as an argument?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Describe your problem in more detail, maybe we can find another solution.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    I call templated function in my base class with with smart pointer.
    This function reads data from socket and deserializes it to smart pointer.

    When deserialization is done, I call virtual function without arguments.

    Now I want to call this function with the same smart pointer argument (where data was serialized to).

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, what you are describing is an attempted solution to your problem. State what is the problem that you are trying to solve.

    E.g., instead of saying "I want to create a temporary, assign x to the temporary, assign y to x, then assign the temporary to y", say "I want to swap x and y".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    template <typename Derived>
    class base
    {
            protected:
     
            Derived const &
            derived(void) const
            {
                return static_cast<Derived const&>(*this);
            }
     
            Derived &
            derived(void)
            {
                return static_cast<Derived&>(*this);
            }
     
            public:
     
            template <typename Type>
            void
            put(Type const & data) const
            {
                    return derived().put(data);
            }
    };
     
    #include <iostream>
     
    class test : public base<test>
    {
            public:
            template <typename Type>
            void
            put(Type const & data) const
            {
                    std::cout << data << std::endl;
            }
    };
     
    template <typename Derived, typename Type>
    void
    put(base <Derived> const & rhs, Type const & data)
    {
            rhs.put(data);
    }
     
    int
    main(void)
    {
            put(test(), "foo");
            put(test(), 1024);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed