0

I have class Signal and Image and both classes have method with same name but different input and output parameters. Is it allowed?

  template <class T> class Signal {
    Signal<T> zeroPadding(Signal<T>);
    }

    template <class T> class Image:public Signal<T>
    {
    public:
    Image(void);
        ~Image(void);
        Image(int,int);
        Image(int,int,double);
        Image(int,int,double,double);
            Image<T> zeroPadding(Image<T>);

    template <class T> Image<T>::Image(int width,int height):Signal(width,height) {}

    template <class T> Image<T>::Image(int width,int height,double dt):Signal(width,height,dt) {}

    template <class T> Image<T>::Image(int width,int height,double dt,double t0 ):Signal(width,height,dt,t0) {}


    template <class T> Image<T> Image<T>::zeroPadding(Image<T> im2){
        Image<T> i1 =(*this);
        Image<T> i2 =im2;

        if (i1.getHeight()==im2.getHeight()){
            i2.zeroPadding(i1); /* I want to call zeroPadding function from class Signal<T>. How I can convert i1 and i2 to class Signal<T> without initialization?*/
        }

}
user3094708
  • 1
  • 1
  • 3
  • Can you fix your formatting to make this readable? – Chad Dec 16 '13 at 21:48
  • 1
    By qualifying the name: `i2.Signal::zeroPadding(i1);` The qualified name tells where to look up for the function. `i1` will automaticaly get converted to `Shape` by [slicing](http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c). Your code is weird to say the least. Why are you making a copy of `this` in the function? – jrok Dec 16 '13 at 21:49
  • I believe you could `static_cast&>(i2).zeroPadding(i1);` also. See: http://ideone.com/ej1C1T – Chad Dec 16 '13 at 21:51
  • I have copy of this because I need width of this before and after I change its width. So i1.getWidth() will return width of this before change and (*this).getWidth() will return width of this after change – user3094708 Dec 16 '13 at 22:04

1 Answers1

0

It's certainly allowed for a base class and a descendant to have methods of the same name with differing argument and return types.

To call the base class's function from a method of the descendant, though, the base class's method will have to be visible. In the question's example code, Signal<T>::zeroPadding is private, so only code within methods belonging directly to Signal<T> (and its friends, but not necessarily descendants) can call that function.

To call Signal<T>::zeroPadding from Image<T>::zeroPadding, the former would need to have at least protected visibility.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I didn't put here but in code Signal::zeroPadding is public. – user3094708 Dec 17 '13 at 09:25
  • I got error Error 3 error LNK2019: unresolved external symbol "public: __thiscall Signal::~Signal(void)" (??1?$Signal@H@@QAE@XZ) referenced in function "public: __thiscall Image::~Image(void)" (??1?$Image@H@@QAE@XZ) – user3094708 Dec 17 '13 at 09:32
  • I have this error in line if (getHeight()==im2.getHeight()) because getHeight() is function from class Signal and im2 is object of class Image so conversion doesn't work. But in main I don't have error.Why? int main() { Image *a=new Image(2,3); Image *b=new Image(1,3); (*a).getWidth(); //here I got 2 without error (*a).getHeight();//here I got 3 without error (*a).zeroPadding(*b); } – user3094708 Dec 17 '13 at 12:35
  • Comments are not the place to post new code. Please edit your question to include updated information. Please also ensure that the code you post accurately represents the problem you're having. If you can't copy and paste the code from the question into a new file and observe the same error as the one you claim to see, then you haven't sufficiently described the problem. That's why I've voted to close your question as off-topic: You haven't provided real code to demonstrate the problem, and you're struggling to describe it well using just English. – Rob Kennedy Dec 17 '13 at 14:40