473,881 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator () overloading, wrong overload gets called

Hi all,

Im having a problem with my code. Im programming a vector class,
and am trying to overload the () operator in 2 different situations.
The first situation is to assign values, e.g. Y = X(1,2), the elements
1 and 2 of X gets assigned to Y. In this case, the operator ()
overload should create a copy that is unmodifiable. In the 2nd case, I
want do assign values to elements 1 and 2, e.g. X(1,2) = Y. Then in
this case, the values must be updated. I update by instantiating a
custom iterator class which has an overloaded = operator. Below is my
code. Sorry its a little long.

The issue now is that when I do Y = X(1,2), I always end up calling
the overload that is meant for the 2nd situation, rather than the
first. I hope I can get some advice.

Thanks in advance.

//*************** *************** *************** ***********
// Main Vector Class
template <typename TYPE>
class Vector {
//*************** *************** *************** ***********
// Selectors
const TYPE& operator[](const int& k) const { return v[k]; } //default
[] indexing
const TYPE& operator()(cons t int& k) const {return v[k];}
int size() const {return v.size();} //returns the size

//*************** *************** *************** ***********
// VectorItr Class
// this is a custom iterator class for Vector
// it wraps the standard vector iterator class
class VectorItr : public
std::iterator<s td::random_acce ss_iterator_tag , TYPE{
typename vector<TYPE>::i terator vitr;
int n; int *ptr;

VectorItr& operator= (const Vector<TYPE>& rhs) {
//using the iterator, populate the Vector<TYPE>
if (ptr == NULL) //for range
rhs.assign(vitr ,n,rhs);
else {
for (int k=0; k<n; k++)
*(vitr+ptr[k]) = rhs[k];
return *this; //should i return this?

//*************** *************** *************** ***********
// Constructors and Destructor
VectorItr() { };
~VectorItr(){ };

VectorItr( Vector<TYPE>& val, const int& i, const int& j) {
//this is used for range assignment
//this constructor is used to initialize the beginning of the
//iterator, and the number of elements that need to be assigned
vitr = val.begin()+i; n = j-i+1; ptr = NULL;

VectorItr( Vector<TYPE>& val, int ind[], const int n2) {
//this is used for index assignment
//this constructor is used to initialize the beginning of the
//iterator, and the number of elements that need to be assigned
vitr = val.begin(); n = n2; ptr = &ind[0];


const Vector<TYPEoper ator()(const int& i, const int& j) const
return Vector<TYPE>(*t his, i, j);

VectorItr operator()(cons t int& i, const int& j)
return VectorItr (*this, i, j);

//*************** *************** *************** ***********
// Constructors and Destructor
Vector() { };
~Vector(){ };

//*************** *************** *************** ***********
// Copiers
//copy constructors
Vector( const Vector<TYPE>& val) { //copy constructor for
intermediate results (like x + y)
for (int k = 0; k < val.size(); k++)
Vector( const Vector<TYPE>& val, const int& i, const int& j) {
//this constructor is used to create copy when indexing a range
for (int k = i; k <=j; k++){
if (k<val.size())
else v.push_back(0);
//can i make an initialization that does something like v = {1, 2 3};

Vector<TYPE>& operator=(const Vector<TYPE>& rhs) {
for (int k = 0; k < rhs.size(); k++)
return *this;

//*************** *************** *************** ***********
// vector container functions
void push_back(TYPE val) {v.push_back(va l);}
typename vector<TYPE>::i terator begin() { return v.begin();}
typename vector<TYPE>::i terator end() { return v.end();}
void assign(typename vector<TYPE>::i terator itr, const int& n, const
Vector<TYPE>& val) const {
//Maybe I should group this under copiers
for (int k=0; k<n; k++){
*itr = val[k]; itr++;
//return *this; //should i return this?

template <size_t N//trick to get array size in automatically!
Vector<TYPE>& assign(int (&val)[N]) {
v.assign(val, val+N);
return *this;
Sep 10 '08 #1
1 1759
On Sep 10, 11:43 am, fabian....@gmai l.com wrote:
I am doing the proxy thing, that is done by the VectorItr
Apparently not correctly.
Essentially my code works only in the following situation:

template <typename TYPE>
void ModifyValues(co nst Vector<TYPE>& x){
//put in some values into y.. lets say {3,4}
y = x(0,1);
int main (){
//put in some values into x.. lets say { 0, 1, 2}
//try to index 2 values of x and change y
return 0;
In this situation, Vector x is passed into the function
ModifyValues() as a const, thus the const overloaded ()
operator will kick in.
However, in the following situation, it will not work:
int main (){
//put in some values into x.. lets say { 0, 1, 2}
//put in some values into y.. lets say {3,4}
//try to index 2 values of x and change y
y = x(0,1); <---- doesnt work, will call the non-const version
of the operator() overload
Which is correct. Overload resolution is determined uniquely by
the types, and since the type of x is not const, you get the
non-const operator().

This is where the proxy kicks in. The non const version has
returned a proxy, not a Vector. Supposing that the assignment
operator of Vector requires a Vector, you need a conversion. So
you define a user defined conversion to Vector in the proxy
class, which returns whatever the const operator() would have
x(0,1) = y; <------ however this works.
return 0;
If I do not pass Vector x into a function, and specify it to
be const, the const overload never kicks in.
Which it shouldn't. It's up to you to make the proxy work
correctly in rvalue contexts. (Look at the example code I
posted. Or better yet, read up on proxies---if memory serves me
right, Scott Meyers discusses them in one of his books.)

James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 10 '08 #2

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

Similar topics

by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but I think my problem has to do with my lack of understandign of some issues in C++. There is a class in QWT called QwtPicker which allows one to make rubberbands and select part of the drawing canvas, very useful in zooming etc. Function...
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 ;
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
by: pauldepstein | last post by:
The following code was written by a colleague -- to preserve confidentiality, the name of the class is changed: HisClass operator+ (const HisClass & h1, const HisClass & h2) { // some code here} HisClass HisClass::operator+ (const HisClass& h2) {// more code here}
by: rn5a | last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is something called method overloading in VB.NET (like in C#) which is one of the features in OOP (polymorphism) but does VB.NET also include operator overloading like C# does? Ron
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
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...
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...
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,...
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...
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...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.