473,659 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling base class operator==

Consider the following:

#include <string>

class A
{
public:
A( const std::string & full_name
, const std::string & display_name)
: m_full_name(ful l_name)
, m_display_name( display_name) {}
virtual ~A() {}
bool operator==(cons t A& other) const {
return (m_full_name == other.m_full_na me)
&& (m_display_name == other.m_display _name);
}
private:
std::string m_full_name;
std::string m_display_name;
};

class B : public A
{
/* constructors etc. skipped ...*/
public:
bool operator==(cons t B& other) const {
return A::operator==(o ther);
}
private: /* etc. */
};

Is there an alternative syntax which can be used when calling the base
class operator== from the derived class (IOW leaving out the keyword
"operator") ?

Thanks.

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #1
5 11328
You can do this:

class A {
public :
bool operator ==(const A& obj)const { return true; }
};

class B : public A {
public :
bool operator ==(const B& obj)const {
const A* temp1 = this;
const A* temp2 = &obj;
return *temp1 == *temp2;
}
};

But if operator == is unchanged for B, just let it
inherit the one from A, and don't redefine it in B.

Steve

Jul 22 '05 #2
On Sun, 08 Aug 2004 01:10:30 +0200, Bob Hairgrove
<wo************ **@to.know> wrote:
Consider the following:

#include <string>

class A
{
public:
A( const std::string & full_name
, const std::string & display_name)
: m_full_name(ful l_name)
, m_display_name( display_name) {}
virtual ~A() {}
bool operator==(cons t A& other) const {
return (m_full_name == other.m_full_na me)
&& (m_display_name == other.m_display _name);
}
private:
std::string m_full_name;
std::string m_display_name;
};

class B : public A
{
/* constructors etc. skipped ...*/
public:
bool operator==(cons t B& other) const {
return A::operator==(o ther);
}
private: /* etc. */
};

Is there an alternative syntax which can be used when calling the base
class operator== from the derived class (IOW leaving out the keyword
"operator") ?


Only if you cast

public:
bool operator==(cons t B& other) const {
return *static_cast<co nst A*>(this) == other;
}

I prefer your original form.

john
Jul 22 '05 #3
Bob Hairgrove wrote:
Consider the following:

#include <string>

class A
{
public:
A( const std::string & full_name
, const std::string & display_name)
: m_full_name(ful l_name)
, m_display_name( display_name) {}
virtual ~A() {}
bool operator==(cons t A& other) const {
return (m_full_name == other.m_full_na me)
&& (m_display_name == other.m_display _name);
}
private:
std::string m_full_name;
std::string m_display_name;
};

class B : public A
{
/* constructors etc. skipped ...*/
public:
bool operator==(cons t B& other) const {
return A::operator==(o ther);
}
private: /* etc. */
};

Is there an alternative syntax which can be used when calling the base
class operator== from the derived class (IOW leaving out the keyword
"operator") ?

Thanks.

--
Bob Hairgrove
No**********@Ho me.com


Having wasted two weeks of debugging time due to
this issue, I have devised the following:
class Base
{
protected:
bool equal_to(const Base& b) const;
};

class Derived
: public Base
{
public:
bool operator==(cons t Derived& d) const
{
bool result = Base::equal_to( d);
// ...
return result;
}
};

The above idiom allows the compiler to check for
instances where two variables of type Base are
compared, or when two pointers to two different
children are compared via parent pointers.
class Monkey
: public Base
{
public:
bool operator==(cons t Monkey& m) const
{
bool result = Base::equal_to( d);
// ...
return result;
}
};

// Code fragment:
Base * p_monkey = new Monkey;
Base * p_derived = new Derived;
bool result;

result = *p_monkey == p_derived;
// End of code fragment.

The assignment above is allowed with virtual
operator==() methods in the base class. In
my idiom, the compiler will flag the assignment
because operator== is not defined in the base
class.

This is just something that I use, your mileage
may vary.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
On Mon, 09 Aug 2004 15:25:45 GMT, Thomas Matthews
<Th************ *************** *@sbcglobal.net > wrote:
Having wasted two weeks of debugging time due to
this issue, I have devised the following:
class Base
{
protected:
bool equal_to(const Base& b) const;
};

class Derived
: public Base
{
public:
bool operator==(cons t Derived& d) const
{
bool result = Base::equal_to( d);
// ...
return result;
}
};

The above idiom allows the compiler to check for
instances where two variables of type Base are
compared, or when two pointers to two different
children are compared via parent pointers.
class Monkey
: public Base
{
public:
bool operator==(cons t Monkey& m) const
{
bool result = Base::equal_to( d);
// ...
return result;
}
};

// Code fragment:
Base * p_monkey = new Monkey;
Base * p_derived = new Derived;
bool result;

result = *p_monkey == p_derived;
// End of code fragment.

The assignment above is allowed with virtual
operator==() methods in the base class. In
my idiom, the compiler will flag the assignment
because operator== is not defined in the base
class.

This is just something that I use, your mileage
may vary.


It looks good!

I was just looking at this and another thread concerning virtual
operator=() (assignment op) and inheritance. In my example, operator==
is not virtual, and the derived operator== takes a different type,
therefore the base class operator== is not overridden ... merely
overloaded. Also, my base class is not abstract, so I would like to
have a "real" operator== in the base class.

I suppose all classes could call the protected "equal_to" function,
including the base class' operator==(). That would keep all
implementation code in one place. Do you see any drawbacks to this
method?

Thanks for your time and help!

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #5
Bob Hairgrove wrote:

It looks good!

I was just looking at this and another thread concerning virtual
operator=() (assignment op) and inheritance. In my example, operator==
is not virtual, and the derived operator== takes a different type,
therefore the base class operator== is not overridden ... merely
overloaded. Also, my base class is not abstract, so I would like to
have a "real" operator== in the base class.

I suppose all classes could call the protected "equal_to" function,
including the base class' operator==(). That would keep all
implementation code in one place. Do you see any drawbacks to this
method?

Thanks for your time and help!

--
Bob Hairgrove
No**********@Ho me.com


So far I haven't encountered any drawbacks to this method.
I just get compiler error messages stating that operator==()
is not implemented; which is the whole purpose: to let
you know that you shouldn't be comparing base classes!

The basis for my method is "double dispatch". (Search the
web and this newsgroup.) If I have Rectangle and Cone
derived from Shape and Shape has an equality member,
there was nothing preventing a comparison between
Rectangle and Cone by dereferencing pointers.
struct Shape
{
bool operator==(cons t Shape&) const;
};
class Square : public Shape {};
class Circle : public Shape {};

Shape * shape_1(new Square);
Shape * shape_2(new Circle);

if (*shape_1 == *shape_2)
{
}

The fundamental logic to my method is to remove the
operator==() method in a base class from public access.
The renaming to "equal_base " is more explicit than
operator==(). The operator==() may imply that it
extends to child objects. This is what causes problems.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #6

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

Similar topics

10
1891
by: Shayne Wissler | last post by:
I've overloaded the global new operator, so that I can, detect when I've run out of memory: extern void* operator new(size_t s) { void *r = malloc(s); if (!r && s) { fprintf(stderr, "Error: No more memory."); exit(1); } return r;
0
1432
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The solution seems to work though. Please let me know if this is okay or if I need to modify something here. Thanks !!!! Here is what I have: #include <new> #include <iostream>
9
4792
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
0
1090
by: tiwy | last post by:
On Andreas Lagemann <andreas.lagemann@freenet.de> wrote: > > Class Base > { > public: > Base() {} > > Base(A* a, B* b); >
7
1519
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: // Assignment operators. virtual base<T>& operator=(const base<T>& rhs) = 0;
6
2102
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
5
3414
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
19
2226
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
15
3518
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
0
8427
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8746
jinu1996
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...
0
8627
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
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 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...
0
4175
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...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.