There are 2 abstract base classes:
class IB;
class IA
{
//...
public:
virtual IB* GetB() = 0;
}
class IB
{
//...
public:
virtual IA* GetA() = 0;
}
Then, this is derived classes:
class CB; //#1
class CA : public IA
{
//...
public:
CB* GetB(); //#2
}
class CB : public IB
{
//...
public:
CA* GetA(); //#3
}
The question is, the declaration do not work.
In C++, the overrided function can return a class which is public
derived from the return type of the function in base class.
When the function #2 returns IB*, it is OK. #3 is OK, too.
But, when #2 returns CB*, the compiler says something like "CB is not
derived from IB".
At #1, I can not declare like this:
class CB : public IB;
The syntax is not passed.
So, what can I do?
There is cycle dependence, but I do not care of the dependence: CA and
CB is almost in a single .cpp file. 6 2417
On 2 Jul., 19:46, "kep...@gmail.c om" <kep...@gmail.c omwrote:
There are 2 abstract base classes:
class IB;
class IA
{
//...
public:
virtual IB* GetB() = 0;
}
class IB
{
//...
public:
virtual IA* GetA() = 0;
}
Then, this is derived classes:
class CB; //#1
class CA : public IA
{
//...
public:
CB* GetB(); //#2}
class CB : public IB
{
//...
public:
CA* GetA(); //#3
}
The question is, the declaration do not work.
In C++, the overrided function can return a class which is public
derived from the return type of the function in base class.
When the function #2 returns IB*, it is OK. #3 is OK, too.
But, when #2 returns CB*, the compiler says something like "CB is not
derived from IB".
At #1, I can not declare like this:
class CB : public IB;
The syntax is not passed.
So, what can I do?
There is cycle dependence, but I do not care of the dependence: CA and
CB is almost in a single .cpp file.
you need to give them different names, because you cant overload a
function by just changing it's return-type:
class CB : public IB
{
public:
CA* _GetA() {return static_cast<CA* >(GetA());};
private:
IA* GetA();
};
On 2 Jul, 18:46, "kep...@gmail.c om" <kep...@gmail.c omwrote:
class IA
{
* * //...
public:
* * virtual IB* GetB() = 0;
}
class CA : public IA
{
* * //...
public:
* * CB* GetB(); //#2}
Your problem, as rhavin grobert has also pointed out, is that in one
case GetB returns an IB* and in the other it returns a CB*. He has
suggested you use functions with different names, but that doesn't
seem to me to be quite what you want. I think you need to decide
whether GetB will return a CB* for all classes derived from IA, or
not. If you're always going to return a CB* then change the
declaration in IA. If not, change the declaration in class IA so that
it says GetB returns an IB*. Since CB is derived from IB, a pointer to
an IB can hold any value that a pointer to a CB can, so the values can
be passed quite happily.
If you really need the return type of GetB to be different for
different classes, you may need to have differently-named functions,
or to have another think about your class structure.
Hope that helps.
Paul.
On Jul 2, 1:46 pm, "kep...@gmail.c om" <kep...@gmail.c omwrote:
There are 2 abstract base classes:
class IB;
class IA
{
//...
public:
virtual IB* GetB() = 0;
}
class IB
{
//...
public:
virtual IA* GetA() = 0;
}
Then, this is derived classes:
class CB; //#1
class CA : public IA
{
//...
public:
CB* GetB(); //#2}
class CB : public IB
{
//...
public:
CA* GetA(); //#3
}
The question is, the declaration do not work.
You are missing semicolons at the end of the class definitions. Test
your
code before posting it.
In C++, the overrided function can return a class which is public
derived from the return type of the function in base class.
When the function #2 returns IB*, it is OK. #3 is OK, too.
But, when #2 returns CB*, the compiler says something like "CB is not
derived from IB".
At #1, I can not declare like this:
class CB : public IB;
The syntax is not passed.
As far as I can tell, you cannot use covariance in this case. Either
return a
pointer to the interface or change your design so that you do not have
a
cyclic dependency.
--
Jonathan Mcdougall
On Jul 2, 11:04 am, ".rhavin grobert" <cl...@yahoo.de wrote:
On 2 Jul., 19:46, "kep...@gmail.c om" <kep...@gmail.c omwrote:
There are 2 abstract base classes:
class IB;
class IA
{
//...
public:
virtual IB* GetB() = 0;
}
[...]
class CA : public IA
{
//...
public:
CB* GetB(); //#2}
[...]
you need to give them different names, because you cant overload a
function by just changing it's return-type:
Yes but this is not overloading. Because of that 'virtual' above, CA
is overriding the same function, and C++ does support covariant return
types.
The problem here is that CA and CB cannot both see the definition of
the other at the same time. It must be possible to solve by returning
proxy classes instead of plain IB* and CB*.
Ali
On Jul 2, 10:46*am, "kep...@gmail.c om" <kep...@gmail.c omwrote:
There are 2 abstract base classes:
class IB;
class IA
{
* * //...
public:
* * virtual IB* GetB() = 0;
}
class IB
{
* * //...
public:
* * virtual IA* GetA() = 0;
}
Then, this is derived classes:
class CB; //#1
class CA : public IA
{
* * //...
public:
* * CB* GetB(); //#2}
class CB : public IB
{
* * //...
public:
* * CA* GetA(); //#3
}
The question is, the declaration do not work.
In C++, the overrided function can return a class which is public
derived from the return type of the function in base class.
When the function #2 returns IB*, it is OK. #3 is OK, too.
But, when #2 returns CB*, the compiler says something like "CB is not
derived from IB".
At #1, I can not declare like this:
class CB : public IB;
The syntax is not passed.
So, what can I do?
There is cycle dependence, but I do not care of the dependence: CA and
CB is almost in a single .cpp file.
Try using proxy classes (Holders below) that allow covariance by being
fully defined where used:
class IA;
class IAHolder
{
IA * ia_;
};
class CA;
class CAHolder : public IAHolder
{
CA * ca_;
};
class IB;
class IBHolder
{
IB * ia_;
};
class CB;
class CBHolder : public IBHolder
{
CB * ca_;
};
class IA
{
//...
public:
virtual ~IA()
{}
virtual IBHolder * GetB() = 0;
};
class IB
{
//...
public:
virtual ~IB()
{}
virtual IAHolder * GetA() = 0;
};
class CB; //#1
class CA : public IA
{
//...
public:
CBHolder * GetB()
{
return 0;
}
};
class CB : public IB
{
//...
public:
CAHolder * GetA()
{
return 0;
}
};
int main()
{
CA ca;
CB cb;
}
Ali
On 2 Jul, 20:57, gw7...@aol.com wrote:
On 2 Jul, 18:46, "kep...@gmail.c om" <kep...@gmail.c omwrote:
class IA
{
* * //...
public:
* * virtual IB* GetB() = 0;
}
class CA : public IA
{
* * //...
public:
* * CB* GetB(); //#2}
Your problem, as rhavin grobert has also pointed out, is that in one
case GetB returns an IB* and in the other it returns a CB*. He has
suggested you use functions with different names, but that doesn't
seem to me to be quite what you want. I think you need to decide
whether GetB will return a CB* for all classes derived from IA, or
not. If you're always going to return a CB* then change the
declaration in IA. If not, change the declaration in class IA so that
My mistake - this should say ... change the declaration in class CA
so that ...
it says GetB returns an IB*. Since CB is derived from IB, a pointer to
an IB can hold any value that a pointer to a CB can, so the values can
be passed quite happily.
If you really need the return type of GetB to be different for
different classes, you may need to have differently-named functions,
or to have another think about your class structure.
Hope that helps.
Paul.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Victor Irzak |
last post by:
Hello,
I have an ABC.
it supports:
ostream & operator <<
I also have a derived class that supports this operator.
How can I call operator << of the base class for derived object??? Is it
at all possible?
|
by: Roy Yao |
last post by:
Hello,
I need to pass a pointer to a
callback function to the lower
level modules. But the function
is thought to be a virtual member one.
How can I get the real address of
the virtual member function?
|
by: Josh Lessard |
last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece
of code that works, but I just don't understand why. I'm not actually
this part of the program, but I really want to know how and why it works.
I'll post a simplified version of it below and ask my questions
afterwards:
class Base {
void *function_ptr;
|
by: qazmlp |
last post by:
class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
|
by: qazmlp |
last post by:
Is it a good style to re-forward declare the types, which were already
forward declared in base header file, in derived class header file?
// base.h
class addrClass;
class base
{
public:
virtual void setAddress(addrClass* node);
| |
by: WittyGuy |
last post by:
Hi all,
Though I know the concepts of both abstract class & virtual
function (like derived class pointer pointing to base class...then
calling the function with the pointer...), what is the real
implementation usage of these concepts? Where these concepts will be
used. Please provide some illustration (real-time), so that it can be
easily comprehended.
Thanks,
wittyGuy
|
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 Triangle and
Prism, or Square and Prism respectively
|
by: mijobee |
last post by:
I'm very new to c++ and just writing some code to learn. I've run into
a problem, with a javaish design, and want to know if there is any
possible solution without modifying the design. I've read up on
virtual inheritance and from my understanding this should work fine but
I haven't found any docs that use such a tangled example. The gcc
output containing the errrors:
Example.cpp: In member function 'virtual IObject*...
|
by: Steve |
last post by:
Hi,
I always though to return an instance of a class by value, it had to be
defined - i.e. forward declaration isn't good enough?
Consider the following code snippet:
class RGBA;
class Colour
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |