473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.h pp" ///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 2117
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.h pp" ///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.nowrot e:
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.h pp" ///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* BombedMazeFacto ry::MakeWall () const;
BombedRoom* BombedMazeFacto ry::MakeRoom(in t n) const;
instead of,
Wall* BombedMazeFacto ry::MakeWall () const
Room* BombedMazeFacto ry::MakeRoom(in t 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.nowrot e:
>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.h pp" ///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* BombedMazeFacto ry::MakeWall () const;
BombedRoom* BombedMazeFacto ry::MakeRoom(in t n) const;
instead of,
Wall* BombedMazeFacto ry::MakeWall () const
Room* BombedMazeFacto ry::MakeRoom(in t 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* BombedMazeFacto ry::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
2142
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
1990
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
4242
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 abstract new System.Data.IDbConnection Connection }
16
3184
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
2543
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 inherit a class. The best workaround and one that I use is the new keyword and cast the base class reference, but in my opinion this is a bad practice...
8
2170
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 virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
3
4541
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from...
2
10314
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 Type from the spezialized class. After this, the error invalid covariant return type occoured. the following minimal sample shows the problem: ...
9
1868
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
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.