Hello,
class A {
public:
A& operator+=( const A& a ) = 0;
};
class B1:public A {
public:
A& operator+=( const A& a ) { ... }
};
class B2:public A {
public:
A& operator+=( const A& a ) { ... }
};
class C {
public:
void Move(double t) { (*_a1) += (*_a2)*t; }
private:
A* _a1;
A* _a2;
};
For Move(), I have to define the operator +=() and *().
Unlike operator+=(), operator*() must be defined as friend:
For example, for B1,
friend B1 operator*( const B1&, double);
friend B1 operator*( double, const B1&);
But class C cannot access the above operator.
Is there any way to access the operator* of B1 and B2 from C?
Thanks,
Joseph. 5 1339
Joseph Y. Oh wrote:
class A {
public:
A& operator+=( const A& a ) = 0;
};
class B1:public A {
public:
A& operator+=( const A& a ) { ... }
};
class B2:public A {
public:
A& operator+=( const A& a ) { ... }
};
class C {
public:
void Move(double t) { (*_a1) += (*_a2)*t; }
private:
A* _a1;
A* _a2;
};
For Move(), I have to define the operator +=() and *().
Unlike operator+=(), operator*() must be defined as friend:
For example, for B1,
friend B1 operator*( const B1&, double);
friend B1 operator*( double, const B1&);
But class C cannot access the above operator.
Is there any way to access the operator* of B1 and B2 from C?
If I understand correctly, you need a "virtual friend" function. While
that mechanism is not directly supported by the language, you can easily
simulate it. Give A a friend operator* that calls a private virtual
function on an A argument. B1 and B2 override the private virtual function.
The general technique is described here: http://www.parashift.com/c++-faq-lit...html#faq-15.11
However, in this case I think you should consider not overloading
operator* and use an ordinary member function instead. The signature of
operator* should be made so that it returns an A const. But that's not
possibile because A is an abstract class, so you cannot create instances
of it. You would have to use a return type other than what the user of
the class probably expects (for example, an A*), thereby making your
class harder to use, defeating the purpose of operator overloading.
--
Christian Hackl
Thanks for your answer.
Sounds like what I want is not possible...
"Joseph Y. Oh" <jo***********@ yahoo.comwrote:
Thanks for your answer.
Sounds like what I want is not possible...
It's not that it is not possible, it's not logical.
Let's look at the code again:
class A {
public:
virtual ~A() = 0;
};
A::~A() { }
const ? operator*( const A& left, const B& right );
Note the question mark above, what type could possibly be placed there
and have it make any sense at all?
Maybe my explanation was not clear enough.
Let me describe again what I want to do.
There are 4 classes, P, A, B1 and B2.
class P;
class A;
class B1;
class B2;
P "has a" A pointer.
B1 and B2 "is a" A. (i.e. B1 and B2 are the derived classes from A)
class P
{
public:
P( bool b );
void update(double t);
private:
A* _a1P;
A* _a2P;
};
Here it is assumed that (*_a1P) and (*_a2P) are the same class objects
e.g. if _a1P is the pointer of B1, then _a2P too.
P::P( bool b )
{
if( b )
{
_a1P = new B1();
_a2P = new B1();
}
else
{
_a1P = new B2();
_a2P = new B2();
}
}
void P::update(doubl e t)
{
A& a1 = *_a1P;
A& a2 = *_a2P;
a1 += a2 * t;
}
How can you do "a1 += a2 * t" by operator overloading ?
operator+=() is easy because the returned value is not object
but reference. However, operator*() is not because it requires
copied object.
That's the reason why I posted this question here.
Do you think that this still doesn't make sense?
On Sun, 18 May 2008 10:20:51 -0400, Daniel T. wrote:
"Joseph Y. Oh" <jo***********@ yahoo.comwrote:
>Thanks for your answer. Sounds like what I want is not possible...
It's not that it is not possible, it's not logical.
Let's look at the code again:
class A {
public:
virtual ~A() = 0;
};
A::~A() { }
const ? operator*( const A& left, const B& right );
Note the question mark above, what type could possibly be placed there
and have it make any sense at all?
"Joseph Y. Oh" <jo***********@ yahoo.comwrote:
Maybe my explanation was not clear enough.
Let me describe again what I want to do.
There are 4 classes, P, A, B1 and B2.
class P;
class A;
class B1;
class B2;
P "has a" A pointer.
B1 and B2 "is a" A. (i.e. B1 and B2 are the derived classes from A)
class P
{
public:
P( bool b );
void update(double t);
private:
A* _a1P;
A* _a2P;
};
Here it is assumed that (*_a1P) and (*_a2P) are the same class objects
e.g. if _a1P is the pointer of B1, then _a2P too.
Here is your problem. Don't assume, express it in the code directly.
template < typename T >
class P
{
public:
P(); // no bool argument necessary
void update( double t );
private:
T* _a1P;
T* _a2P;
};
With the above, and properly defined functions, you can do the multiply. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Don Vaillancourt |
last post by:
Hello all,
Over the years as I design more database schemas the more I come up with
patterns in database design. The more patterns I recognize the more I
want to try to design some kind of generic design patterns that can be
used and shared amongst many sub-schemas.
For example, the grouping of entities. I may have the following tables:
employee, product and client. These tables have no direct relationship
with each other. But...
|
by: sk |
last post by:
I have an applicaton in which I collect data for different parameters
for a set of devices. The data are entered into a single table, each
set of name, value pairs time-stamped and associated with a device.
The definition of the table is as follows:
CREATE TABLE devicedata
(
device_id int NOT NULL REFERENCES devices(id), -- id in the device
|
by: Test User |
last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people
as possible)
I am somewhat familiar with Access 2000, but my latest project has me
stumped. So, I defer to you experts.
I've been asked to create a Daily Log sheet to be distributed to some of our
clerks.
For each day, the clerk is to log tasks worked on for the day, (i.e worked
on the johnson account).
|
by: rodchar |
last post by:
Hey all,
I'm trying to understand Master/Detail concepts in VB.NET. If I do a data
adapter fill for both customer and orders from Northwind where should that
dataset live?
What client is responsible for instantiating the orders class? Would it be
the ui layer or the master class in the business layer?
thanks,
|
by: tshad |
last post by:
Many (if not most) have said that code-behind is best if working in teams -
which does seem logical.
How do you deal with the flow of the work?
I have someone who is good at designing, but know nothing about ASP. He can
build the design of the pages in HTML with tables, labels, textboxes etc.
But then I would need to change them to ASP.net objects and write the code
to make the page work (normally I do this as I go - can't do this...
| |
by: roN |
last post by:
Hi,
I'm creating a Website with divs and i do have some troubles, to make it
looking the same way in Firefox and IE (tested with IE7). I checked it with
the e3c validator and it says: "
This Page Is Valid XHTML 1.0 Transitional!" but it still wouldn't look the
same.
It is on http://www.dvdnowkiosks.com/new/theproduct.php scroll down and
recognize the black bottom bar when you go ewith firefox(2.0) which isn't
there with IE7. Why does...
|
by: JoeC |
last post by:
I have a question about designing objects and programming. What is the
best way to design objects? Create objects debug them and later if you
need some new features just use inhereitance. Often times when I
program, I will create objects for a specific purpose for a program and
if I need to add to it I just add the code.
|
by: |
last post by:
I have a question about spawning and displaying subordinate list controls
within a list control. I'm also interested in feedback about the design of
my search application. Lots of code is at the end of this message, but I
will start with an overview of the problem.
I've made a content management solution for my work with a decently
structured relational database system. The CMS stores articles. The CMS also
stores related items --...
|
by: neelsmail |
last post by:
Hi,
I have been working on C++ for some time now, and I think I have a
flair for design (which just might be only my imagination over-
stretched.. :) ). So, I tried to find a design certification, possibly
that involves C++, but, if not, C++ and UML. All I could find was Java
+ UML design certifications (one such is detailed on
http://www.objectsbydesign.com/tools/certification.html). Although UML
is expected to be language independent,...
|
by: indrawati.yahya |
last post by:
In a recent job interview, the interviewer asked me how I'd design
classes for the following problem: let's consider a hypothetical
firewall, which filters network packets by either IP address, port
number, or both. How should we design the classes to represent these
filters?
My answer was:
class FilterRule
{
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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: 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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |