473,903 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator commonalities

Hi!

I have a class C with one type template parameter T and a member of
type T. Class C has two assignment operators that take C's
instantiated with the same and with another T, respectively. The
implementations of these differ only in that the latter does not
assign the T member. How do I best get to write the common part only
once?

Here's the situation for those who read code better than English. Of
course, my real class and operators are bigger than that:

template<class T>
class C {
public:
C<T>& operator=(const C<T>& rhs)
{ t_ = rhs.t_; data_ = rhs.data_; return *this; }

template<RhsT>
C<T>& operator=(const C<RhsT>& rhs)
{ data_ = rhs.data_; return *this; }

private:
int data_;
T t_;
};
Thank you,
Martin
Jul 22 '05 #1
5 1277
Martin Eisenberg wrote:
Hi!

I have a class C with one type template parameter T and a member of
type T.
You mean you have a class template called C.
Class C has two assignment operators that take C's
instantiated with the same and with another T, respectively. The
implementations of these differ only in that the latter does not
assign the T member. How do I best get to write the common part only
once?


Why would you want to?

Jul 22 '05 #2
Rolf Magnus wrote:
Martin Eisenberg wrote:
Hi!

I have a class C with one type template parameter T and a
member of type T.


You mean you have a class template called C.
Class C has two assignment operators that take C's
instantiated with the same and with another T, respectively.
The implementations of these differ only in that the latter
does not assign the T member. How do I best get to write the
common part only once?


Why would you want to?


Come to think of it, why would anyone actually want to avoid
duplication? Seriously, I don't get your question.
Jul 22 '05 #3
"Martin Eisenberg" <ma************ *****@PAMudo.ed u> wrote...
Rolf Magnus wrote:
Martin Eisenberg wrote:
Hi!

I have a class C with one type template parameter T and a
member of type T.


You mean you have a class template called C.
Class C has two assignment operators that take C's
instantiated with the same and with another T, respectively.
The implementations of these differ only in that the latter
does not assign the T member. How do I best get to write the
common part only once?


Why would you want to?


Come to think of it, why would anyone actually want to avoid
duplication? Seriously, I don't get your question.


template<class T> class C {
T t;
void commonCode() { /*whatever*/ }
public:
C(T t) : t(t) {}
C& operator =(C const& otherC) {
commonCode();
t = otherC.t;
return *this;
}

template<class U> C& operator =(C<U> const& otherCU) {
commonCode();
return *this;
}
};

int main() {
C<int> ci(42);
C<double> cd(3.1415926);
ci = cd;
}

Perhaps I don't understand your problem. Do you think you could
illustrate with C++?

Victor
Jul 22 '05 #4
"Martin Eisenberg" <ma************ *****@PAMudo.ed u> wrote:
The implementations of these differ only in that the latter does not
assign the T member. How do I best get to write the common part only
once? template<class T>
class C {
public: template <> // <--------- added C<T>& operator=(const C<T>& rhs)
{ t_ = rhs.t_; data_ = rhs.data_; return *this; }

template<typena me RhsT> // <-------- added "typename"
C<T>& operator=(const C<RhsT>& rhs)
{ data_ = rhs.data_; return *this; }

private:
int data_;
T t_;
};


(Needed to add "template <>" to the first declaration, at least under Visual
C++).

This won't quite work anyway ... "data_ = rhs.data_" is illegal, since in
the context of C<T>, C<RhsT>::_data is private

David F
Jul 22 '05 #5
Victor Bazarov wrote:
Perhaps I don't understand your problem. Do you think you could
illustrate with C++?


Duh... You've understood my "problem" perfectly well. Sometimes I
think terminal blindness is but weeks away. Thanks anyway,
Martin
Jul 22 '05 #6

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

Similar topics

7
8038
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
1
3885
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include "BitString.h"
5
3815
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
0
1840
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should be disabled in a release build, and to do that efficiently, I suppose I need to overload the << operator. My current implementation of the stream in release mode is posted below. Everything works fine for POD type like bool and int, but the...
3
2964
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD FUNCTION!!
14
2349
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> using namespace std;
6
4436
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
5
2301
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
3
3294
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
0
10006
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
11294
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10990
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,...
0
9692
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
8057
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
5899
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
6100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4734
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
4316
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.