templated functions and derivation

This is a discussion on templated functions and derivation within the C++ Programming forums, part of the General Programming Boards category; Hello I have a derived and base class. From derived class I call templated functions in base class. For instance: ...

  1. #1
    l2u
    l2u is offline
    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
    22,315
    I have never seen a virtual function template before... but really, what is the bigger picture of what you are trying to solve?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    l2u
    l2u is offline
    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,893
    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
    l2u
    l2u is offline
    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,893
    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
    l2u
    l2u is offline
    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
    22,315
    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".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21