473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

covariant return type

Hi,
is there any way forward declare a covariant return type (and is it
a language feature or extension only)?

I have class,
class CompModel;
class CompWorker{
public:
virtual CompModel& model(); /// forward declaration works as
usual.
};
#include "KeyModel.hpp" ///here i need to include it.
class KeyWorker : public CompWorker{
public:
virtual KeyModel& model();
}
where,
class KeyModel : public CompModel{
};
No way i can express a forward decl which inherit from another class.
I am not sure if really any include is needed apart from knowing
inheritance, as KeyModel memory model is not needef for KeyWorker.

Thanks
abir
Jul 14 '08 #1
3 2104
abir wrote:
Hi,
is there any way forward declare a covariant return type (and is it
a language feature or extension only)?

I have class,
class CompModel;
class CompWorker{
public:
virtual CompModel& model(); /// forward declaration works as
usual.
};
#include "KeyModel.hpp" ///here i need to include it.
class KeyWorker : public CompWorker{
public:
virtual KeyModel& model();
}
where,
class KeyModel : public CompModel{
};
No way i can express a forward decl which inherit from another class.
I am not sure if really any include is needed apart from knowing
inheritance, as KeyModel memory model is not needef for KeyWorker.
Looks like a bad design decision. Anyway, would something like this work
for you?

template < class T >
class CompWorker
{
public:
virtual T& model();
};

class KeyWorker : public CompWorker< KeyWorker >
{
};
Might need to add some things (like for example virtual destructor).

Jul 14 '08 #2
On Jul 14, 2:38 pm, anon <a...@no.nowrote:
abir wrote:
Hi,
is there any way forward declare a covariant return type (and is it
a language feature or extension only)?
I have class,
class CompModel;
class CompWorker{
public:
virtual CompModel& model(); /// forward declaration works as
usual.
};
#include "KeyModel.hpp" ///here i need to include it.
class KeyWorker : public CompWorker{
public:
virtual KeyModel& model();
}
where,
class KeyModel : public CompModel{
};
No way i can express a forward decl which inherit from another class.
I am not sure if really any include is needed apart from knowing
inheritance, as KeyModel memory model is not needef for KeyWorker.

Looks like a bad design decision. Anyway, would something like this work
for you?
Why bad design decision? This is a common design and covariant return
type is just for these kind of evolved objects.
consider GoF AbstructFactory, and if someone has
BombedWall* BombedMazeFactory::MakeWall () const;
BombedRoom* BombedMazeFactory::MakeRoom(int n) const;
instead of,
Wall* BombedMazeFactory::MakeWall () const
Room* BombedMazeFactory::MakeRoom(int n) const
I don't see anything bad in this, do you?
Forward declaration is a C++ related issue, which i was testing.
BTW, i don't have any cyclic dependency, so i can include it well, in
my case. But any change in KeyModel.hpp needs a recompilation of
KeyWorker,
which is not actually needed if i can express the relationship between
KeyModel & CompModel properly.
template < class T >
class CompWorker
{
public:
virtual T& model();

};

class KeyWorker : public CompWorker< KeyWorker >
{

};

Might need to add some things (like for example virtual destructor).
Yes, it works. but ... If i have a bunch of evolving objects ,
then i need to have a few template parameter for CompWorker.
and if there is N such cov return types, out of N^2 templates only N
are useful.
It's a solution, but intent is not a clear one.
Where if i can declare relation among the class as forward declaration
then it is clear.
like class CompWorker;
class KeyWorker : public CompWorker;

Thnaks
abir
Jul 14 '08 #3
abir wrote:
On Jul 14, 2:38 pm, anon <a...@no.nowrote:
>abir wrote:
>>Hi,
is there any way forward declare a covariant return type (and is it
a language feature or extension only)?
I have class,
class CompModel;
class CompWorker{
public:
virtual CompModel& model(); /// forward declaration works as
usual.
};
#include "KeyModel.hpp" ///here i need to include it.
class KeyWorker : public CompWorker{
public:
virtual KeyModel& model();
}
where,
class KeyModel : public CompModel{
};
No way i can express a forward decl which inherit from another class.
I am not sure if really any include is needed apart from knowing
inheritance, as KeyModel memory model is not needef for KeyWorker.
Looks like a bad design decision. Anyway, would something like this work
for you?
Why bad design decision? This is a common design and covariant return
type is just for these kind of evolved objects.
consider GoF AbstructFactory, and if someone has
BombedWall* BombedMazeFactory::MakeWall () const;
BombedRoom* BombedMazeFactory::MakeRoom(int n) const;
instead of,
Wall* BombedMazeFactory::MakeWall () const
Room* BombedMazeFactory::MakeRoom(int n) const
I don't see anything bad in this, do you?
IMO might be better if BomberWall and Wall inherit from WallBase, then
you could do:
WallBase* BombedMazeFactory::MakeWall () const
Forward declaration is a C++ related issue, which i was testing.
BTW, i don't have any cyclic dependency, so i can include it well, in
my case. But any change in KeyModel.hpp needs a recompilation of
KeyWorker,
which is not actually needed if i can express the relationship between
KeyModel & CompModel properly.
>template < class T >
class CompWorker
{
public:
virtual T& model();

};

class KeyWorker : public CompWorker< KeyWorker >
{

};

Might need to add some things (like for example virtual destructor).
Yes, it works. but ... If i have a bunch of evolving objects ,
then i need to have a few template parameter for CompWorker.
and if there is N such cov return types, out of N^2 templates only N
are useful.
It's a solution, but intent is not a clear one.
Where if i can declare relation among the class as forward declaration
then it is clear.
like class CompWorker;
class KeyWorker : public CompWorker;
Would it be possible to make a non-template base class for the template
class I suggested?
Maybe this would solve your doubts:
http://www.gamedev.net/community/for...opic_id=400392
Jul 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
14
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
13
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
6
by: miked | last post by:
Why are there still no covariant return types? All searches reveal no workarounds accept for using an interface which is a real pain. I wind up missing this capability almost every time I...
8
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
2
by: Sebastian Schucht | last post by:
Hi, I have a templateclass with virtual member-functions, so i can't create instances ... but for returning the result i would like use one. So i replaced the A<TTypefrom the baseclass with the...
9
by: Rahul | last post by:
Hi Everyone, I was trying to implement covariant return types and i get a compilation error, class BB : public AA { }; class A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.