Virtual member function overriding

Is there any way to do something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Base {
    public virtual void Get() = 0;
    virtual ~Base();
};

class Derived1 : Base {
    private bool value;
    public bool Get() {
        return value;
    }
};

class Derived2 : Base {
    private int value;
    public int Get() {
        return value;
    }
};

//and then have something like
Base* base = new Derived2();
int temp = base->Get();


I know C++ supports covariant return types in virtual overloaded methods, but is there any way to do this with native types?
Hey there. I have been away from C++ for a while now but I tried your code and unfortunately it turned out to be pretty much pseudocode.

I messed around with it and got it to compile but I had to change it a lot. What you want only seems to work if you place every overloaded method in the vase class and if one of them is a pure virtual method than all overloaded methods in that base class must also be pure virtual. At least this is what I came up with after experimenting. Also, note that the return value is not enough to overload a method. Only the parameters and name of the method make it's signature unique.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	class Base {
	public:
		virtual void Get() = 0;
		virtual bool Get(int i) = 0;
		virtual int Get(float x) = 0;
		virtual ~Base() { }
	};

	class Derived1 : public Base {
			bool value;
	public:
			 void Get() { }
			 bool Get(int i) { }
			 int Get(float x) { }	
	};

	class Derived2 : public Base {
			int value;
	public:
			 void Get() { }
			 bool Get(int i) { }
			 int Get(float x) { }
	};


	int main()
	{
	
	Base* base = new Derived2;
	
	//int temp = base->Get();  // I commented this out because you are trying to set a void method to an int
	
		return 0;	
	}
You can't do that. C++ is a statically typed language. In the last lines of your example, how is the compiler supposed to know what type Get returns?
The important thing to notice is that you even if this was possible (it kind of is, with a bit of work), such thing would be entirely useless.
As per my knowledge for the function overriding the signature of base class and derived class should be same in fact there should not be any difference in their return type
Yes, C++ virtual methods support covariant return type, which means an override method must return etither the return type of the overrided one or a child type of it (it must also be a pointer or reference).
Here neither bool or int is a child type of void so this can't work. An example of covariant methods:

1
2
3
4
5
6
7
8
9
10
class A
{
public:
virtual A* clone() = 0;
}

class B : public A
{
B* clone() override;
}


Here B is a child type of A so it's ok
Hi,

C++ is a meta programming language, you can use template to solve your problem a possible solution is like following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
template<class Type>
class Base {
public :
	typedef Type value_type;
	
	Base(){}
	virtual ~Base(){}

	virtual value_type Get() = 0;

};

template<class Type>
class Derived1 : public Base<Type> {
public :

	typedef Type value_type;
	
	virtual ~Derived1(){}
	value_type Get() {
        return value;
    }

private :
	value_type value;

};
template<class Type>
class Derived2 : public Base<Type> {
public :

	typedef Type value_type;
	
	virtual ~Derived2(){}

	value_type Get() {
        return value;
    }

private :
	value_type value;
};


int main () {

//and then have something like
Base<int>* base = new Derived2<int>();
int temp = base->Get();
	cout<<temp<<endl;
	delete base;
  return EXIT_SUCCESS ;
}


hope it helps
Last edited on
another solution in C may be like following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Base {
    public:
 virtual int Get() = 0;
    virtual ~Base();
};

class Derived1 : public Base {
    private int value;
    public int Get() {
        return value > 0 ;
    }
};

class Derived2 : public  Base {
    private int value;
    public int Get() {
        return value;
    }
};

//and then have something like
Base* base = new Derived2();
int temp = base->Get();
Last edited on
Topic archived. No new replies allowed.