473,379 Members | 1,317 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,379 software developers and data experts.

accessing/overloading parent overloaded operators

I want to use the * operator of the parent class in the def'n of the *
operator for my child class. Is there an elegant way to do this e.g.

template<typename object>
class foo : public pFoo<object>
{
// yadayadayada
object& operator*()
{
WhateverDereferencedParentReturns bar =
HowDoIGetTheReturnOfParentOperator*;
return SomeOperation(bar);
}
}

I think I can do a static_cast on the 'this' pointer to get 'bar'

e.g.

WhateverDereferencedParentReturns bar = *static_cast<pFoo>(*this);

but this seems clunky to me...is there a more elegant solution?

regards,
L.
Jul 22 '05 #1
2 1263

"NKOBAYE027" <NK********@Rogers.Com> wrote in message
news:%K**********************@twister01.bloor.is.n et.cable.rogers.com...
I want to use the * operator of the parent class in the def'n of the *
operator for my child class. Is there an elegant way to do this e.g.

template<typename object>
class foo : public pFoo<object>
{
// yadayadayada
object& operator*()
{
WhateverDereferencedParentReturns bar =
HowDoIGetTheReturnOfParentOperator*;
return SomeOperation(bar);
}
}

I think I can do a static_cast on the 'this' pointer to get 'bar'

e.g.

WhateverDereferencedParentReturns bar = *static_cast<pFoo>(*this);
WhateverDereferencedParentReturns bar = *static_cast<pFoo<object>& >(*this);

or this

WhateverDereferencedParentReturns bar = **static_cast<pFoo<object>* >(this);

Personally I would use the second, I never entirely trust casting with
references. Your solution (when corrected for syntax) involves the creation
of a temporary pFoo<object> object.

but this seems clunky to me...is there a more elegant solution?


Perhaps this

class foo : public pFoo<object>
{
typedef pFoo<object> base_type;
object& operator*()
{
WhateverDereferencedParentReturns bar = base_type::operator*(*this);
return SomeOperation(bar);
}

I think that is the clearest.

John
Jul 22 '05 #2
Thanks again, John. It was 311 am so I had to give up abusing my brain (or
the mush that it has undoubtedly been replaced with over the past years) and
get some sleep. :o) I feel much better now....

I like your sol'n to the casting issue better...I think my objections to it
were the same as yours...I'd rather cast pointers than objects or references
to them.

regards,
L.
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g************@uni-berlin.de...

"NKOBAYE027" <NK********@Rogers.Com> wrote in message
news:%K**********************@twister01.bloor.is.n et.cable.rogers.com...
I want to use the * operator of the parent class in the def'n of the *
operator for my child class. Is there an elegant way to do this e.g.

template<typename object>
class foo : public pFoo<object>
{
// yadayadayada
object& operator*()
{
WhateverDereferencedParentReturns bar =
HowDoIGetTheReturnOfParentOperator*;
return SomeOperation(bar);
}
}

I think I can do a static_cast on the 'this' pointer to get 'bar'

e.g.

WhateverDereferencedParentReturns bar = *static_cast<pFoo>(*this);
WhateverDereferencedParentReturns bar = *static_cast<pFoo<object>&
(*this);

or this

WhateverDereferencedParentReturns bar = **static_cast<pFoo<object>*
(this);

Personally I would use the second, I never entirely trust casting with
references. Your solution (when corrected for syntax) involves the

creation of a temporary pFoo<object> object.

but this seems clunky to me...is there a more elegant solution?
Perhaps this

class foo : public pFoo<object>
{
typedef pFoo<object> base_type;
object& operator*()
{
WhateverDereferencedParentReturns bar =

base_type::operator*(*this); return SomeOperation(bar);
}

I think that is the clearest.

John

Jul 22 '05 #3

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

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
18
by: uday | last post by:
Does C supports overloading. I am thinking no. But some people are saying yes. If yes how. Please answer with examples. Also in c++ size of empty class is one. why. and what are the default...
0
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
2
by: Chris | last post by:
Hi, I try Public Shared Operator +(ByVal Left As Point, ByVal Right As Size) As Point Return New Point(Left.X + Right.Width, Left.Y + Right.Height) End Operator but I get compiler errors....
2
by: Armond VanHeusen | last post by:
A quick question if I may (this relates to .NET1.1 by the way): If I have a C# class defined in my project that does operator overloading, can I use this in my VB code? For example, I have a...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
35
by: josh | last post by:
Hi, I coded the following but It does not return what I expect, why? #include <iostream> using namespace std; class Other { public: int i;
3
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.