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

Override virtual function w/ Object (not Reference) return?

Hello,

I'm trying to implement the design below. I'd prefer to use commented
operator+() in class Number instead of the associated, uncommented
operator+(), but as I understand Covariant Return Types, one is not
allowed to return non-references (eg, references or pointers) in
virtual-functions (which seems to make sense given the seemingly
polymorphic nature).

So...I use the hack below and make my Object-return function be
non-virtual.

2 questions:

1) Is this "hack" appropriate so long as I manually make sure I
override all the non-virtual functions in my derived classes?

2) Can anyone suggest other ways to solve my problem?

I'll readily admit that I do not yet properly understand some of these
advanced C++ topics.

More details below.
Thanks for any help,
-Matt
A little background: I need two object/class types that share the
same methods, one of which is numerically "larger" then the other such
that the larger number can handle arithmetic overflows (without losing
the resulting larger number) of the smaller number. ie, if SmallNum
is one byte big, and BigNum is two bytes big, then:

(SmallNum) 0xFF + (SmallNum) 0x01 = (BigNum) 0x0100
class Number
{
public:
virtual Number& operator = (const Number &rval) = 0;
//virtual Number operator + (const Number &rval) = 0;
Number operator + (const Number &rval);
};

class BigNum : public Number
{
public:
BigNum() { };
BigNum& operator = (const Number &rval) { }
BigNum operator + (const Number &rval) { }
};

class SmallNum : public Number
{
public:
SmallNum() { };
SmallNum& operator = (const Number &rval) { }
BigNum operator + (const Number &rval) { }
};

int main() { BigNum bn; SmallNum sn; return 0; }

--
Remove the "downwithspammers-" text to email me.
Jul 23 '05 #1
3 1673
Matt wrote:
I'm trying to implement the design below. I'd prefer to use commented
operator+() in class Number instead of the associated, uncommented
operator+(), but as I understand Covariant Return Types, one is not
allowed to return non-references (eg, references or pointers) in
virtual-functions (which seems to make sense given the seemingly
polymorphic nature).

So...I use the hack below and make my Object-return function be
non-virtual.

2 questions:

1) Is this "hack" appropriate so long as I manually make sure I
override all the non-virtual functions in my derived classes?
Appropriate for what? Any time you add two 'SmallNum', you are going to
get a BigNum. Is that your intention, even if the result *can* fit into
a SmallNum? Then, yes, it's appropriate. Otherwise, no, you need some
other solution. Perhaps adding a conversion from BigNum to SmallNum would
help.
2) Can anyone suggest other ways to solve my problem?
You could return a proxy object that would contain a union and proper
conversions to all involved classes. Of course it should throw if you
try to convert stored BigNum to SmallNum. Or truncate. Depending on
your design, of course.
I'll readily admit that I do not yet properly understand some of these
advanced C++ topics.
Which topics are those? Perhaps you should get a better book or ask
more specific questions.
More details below.
Thanks for any help,
-Matt
A little background: I need two object/class types that share the
same methods, one of which is numerically "larger" then the other such
that the larger number can handle arithmetic overflows (without losing
the resulting larger number) of the smaller number. ie, if SmallNum
is one byte big, and BigNum is two bytes big, then:

(SmallNum) 0xFF + (SmallNum) 0x01 = (BigNum) 0x0100
So, what happens to

(SmallNum) 0x01 + (SmallNum) 0x02

? (BigNum) 0x0003 or (SmallNum) 0x03 ?


class Number
{
public:
virtual Number& operator = (const Number &rval) = 0;
//virtual Number operator + (const Number &rval) = 0;
Number operator + (const Number &rval);
};

class BigNum : public Number
{
public:
BigNum() { };
BigNum& operator = (const Number &rval) { }
BigNum operator + (const Number &rval) { }
};

class SmallNum : public Number
{
public:
SmallNum() { };
SmallNum& operator = (const Number &rval) { }
BigNum operator + (const Number &rval) { }
};

int main() { BigNum bn; SmallNum sn; return 0; }


Victor
Jul 23 '05 #2
On Tue, 01 Feb 2005 10:46:06 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:
1) Is this "hack" appropriate so long as I manually make sure I
override all the non-virtual functions in my derived classes?
Appropriate for what? Any time you add two 'SmallNum', you are going to
get a BigNum. Is that your intention, even if the result *can* fit into
a SmallNum?


Yes.
Then, yes, it's appropriate.
Thanks.
So, what happens to

(SmallNum) 0x01 + (SmallNum) 0x02

? (BigNum) 0x0003 or (SmallNum) 0x03 ?
(BigNum) 0x0003
2) Can anyone suggest other ways to solve my problem?


You could return a proxy object that would contain a union and proper
conversions to all involved classes. Of course it should throw if you
try to convert stored BigNum to SmallNum. Or truncate. Depending on
your design, of course.


I like this option.

fyi, Previously-unmentioned design constraint/requirement:

The "Numbers" will be written to files. I need to minimize the
storage space used to contain the millions/billions of "Numbers" (that
will be much larger then the sizes we describe here). I was first
attempting to make a direct/exact correlation between the memory size
of the Number objects and the serialized, "storage" size.

However, I can possibly do the union in memory and store the
"SmallNum" and "BigNum" portions in storage where appropriate.
I'll readily admit that I do not yet properly understand some of these
advanced C++ topics.


Which topics are those? Perhaps you should get a better book


Care to offer any suggestions?
or ask
more specific questions.


I'll attempt to be as thorough as possible in the future.

Thanks very much for you continued, timely, and informative responses,
Victor.

-Matt
--
Remove the "downwithspammers-" text to email me.
Jul 23 '05 #3
Matt wrote:
I'll readily admit that I do not yet properly understand some of these
advanced C++ topics.


Which topics are those? Perhaps you should get a better book

Care to offer any suggestions?


When I say "a better book", I expect it to depend on what you've already
gone through. If you already have "TC++PL", then you should probably
spend more time with it. Otherwise, do get a copy. Do you have the
"Effective" series? Do you have "Exceptional" series? At some point
"Advanced C++" by Coplien helped me to straighten out some misconceptions,
but it's rather old, so take it with a grain of salt and if you can find
it in a library, no need to get your own copy, I guess. "C++ Templates",
"The C++ Standard Library" are a must. "Modern C++ Design" and
"Multi-Paradigm DESIGN for C++" are also very good and deserve a place on
your shelf.

Cross-over books are useful too. OOD and domain-specific ones can contain
often very helpful information (or misinformation, if you can recognize it
as such). Sometimes gems are buried under a pile of unrelated stuff and
it would be your task to see them as gems. Of course, one man's gem is
another man's pebble in his shoe (I am unsure myself what I mean here :-)

V
Jul 23 '05 #4

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

Similar topics

22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
12
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding a virtual function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A...
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
3
by: Tom Jones | last post by:
I do not understand what is meant when someone states that a given method is "hidden" verses overriden. Would someone please provide a short example of both cases and why you might want to...
2
by: Adriano Coser | last post by:
Hello. After I converted my .net code to the new VC2005 syntax I started to get C4490 on my ExpandableObjectConverter subclass overrides. The GetProperties method is no longer called by the...
3
by: methodios | last post by:
Hi Can someone help me straighten out my train of thought... I want to know what actually goes on behind the scene..step by step. Here is the code: ----------------- using System; ...
13
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
12
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.