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

operators in derived classes??

CONSIDER:

#include <valarray>

class ivec : public valarray<int>
{
public:
ivec(size_t sz) : valarray<int>(sz){};
void t();
};

int main()
{
ivec w(1);
(-w).t();
}

The return type of -w above is valarray<int> and not the derived type ivec.
This results
in a COMPILATION ERROR.

Is there a neat way of reusing the unary -operator of valarray<int> above?
To make
a type conversion to ivec?
Regards,
Niels Lauritzen ni***@imf.au.dk
Jul 19 '05 #1
1 1519
On Wed, 6 Aug 2003 15:09:27 +0200, "Niels Lauritzen" <ni***@imf.au.dk>
wrote:
CONSIDER:

#include <valarray>

class ivec : public valarray<int>
{
public:
ivec(size_t sz) : valarray<int>(sz){};
void t();
};

int main()
{
ivec w(1);
(-w).t();
}

The return type of -w above is valarray<int> and not the derived type ivec.
This results
in a COMPILATION ERROR.

Is there a neat way of reusing the unary -operator of valarray<int> above?
To make
a type conversion to ivec?


The problem is that valarray is not intended to be used as a base
class (it doesn't have any virtual functions), but as a building block
to be used by other classes - IOW, use composition, not inheritence.
If you are just trying to add additional operations, use non-member
functions. e.g.

void t(valarray<int>& v);

However, you can't then do

t(-w); //error, can't bind temporary to non-const reference

since you pass by non-const ref (and this is no bad thing in this
example). Perhaps you actually wanted a const operation:

void t(valarray<int> const& v);

t(-w); //works fine now

Tom
Jul 19 '05 #2

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

Similar topics

13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
10
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
5
by: Shak | last post by:
Hi all. I was led to believe that static methods were not inherited by their subclasses (and since that makes sense, rightly so). However, a subclass I've written is using it's (abstract)...
3
by: Dean Roddey | last post by:
I just upgraded the compiler version we use in our product (we'd stuck with a very old one for a long time) and it seems that things have moved forward while we were off in the wilderness, and I'm...
6
by: JohnQ | last post by:
I like, non-copyable, non-assignable and, most often, non-default-constructable also, as a starting point for class design: class SomeClass { SomeClass(); // disallow default construction...
3
by: Noodle | last post by:
Hi Guys, not sure if anyone can help me on this one but...... I have a class that I overload the equality operator (=). I also have a class that inherits from this first class which has extra...
19
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary...
4
by: t f | last post by:
hi just a quick question - i have the something like the following public abstract class ClassA { protected int iA; public int Value { get {return iA;} set { iA = value;}}
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...

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.