473,811 Members | 3,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigment operator and container of base type


Below is an example of the problem I am having. I don't understand how
I can get the compiler to see deriv &operator=(cons t T &rhs).

I am sure this is a common problem - any suggestions?
#include <iostream>
#include <vector>

struct base
{
virtual ~base()=0 {};
};

template<class T>
struct deriv : public base
{
deriv(): data_(0) {};
deriv(const T &data): data_(data) {};
deriv &operator=(cons t T &rhs) { data_=rhs; return (*this); }
deriv &operator=(cons t deriv &rhs)
{ data_=rhs.data_ ; return (*this); }
T data_;
};

struct container
{
base &operator[](int i) { return *(data_[i]); };
std::vector<bas e *data_;
};

void fill_cont(conta iner &data);

int main(int argc, char *argv[])
{
container data;
fill_cont(data) ;

std::cout << "data[0]=" << typeid(data[0]).name() << std::endl;
// output: data[0]=struct deriv<int>
data[0]=12445;

std::cout << "data[1]=" << typeid(data[1]).name() << std::endl;
// output: data[1]=struct deriv<double>
data[1]=4.5667;

std::cout << "data[2]=" << typeid(data[2]).name() << std::endl;
// output: data[2]=struct deriv<char *>
data[2]="test";

return 0;
}

void fill_cont(conta iner &data)
{
data.data_.push _back(new deriv<int>());
data.data_.push _back(new deriv<double>() );
data.data_.push _back(new deriv<char *>());
}

--

Adrian

Think you know a language? Post to comp.lang... and find out!
Dec 4 '06
10 1792
Adrian wrote:
Ondra Holub wrote:
>In container::oper ator[] you are returning reference to base. But there
is no assignment operator in class 'base', so compiler cannot see it.

Should be. The compiler should generate one by default.
Surely. base& base::operator= (const base&) is declared by default. Remarks:

1. It does not match the signature of deriv<T>& deriv<T>::opera tor=(const
T&)
2. It is not virtual.

1 Means that when the comiler searches base (static type of lhs in
assignment) for operator=, it cannot see any operator= taking a T, even
though the dynamic type of lhs has one.

2 Means that even if the signatures had matched, the compiler would
statically link to base::operator= . Example of latter:

#include <iostream>

using namespace std;

struct foo {
virtual foo& operator =(const foo&) {
cout << "foo\n";
return *this;
}
};

struct bar : foo {
bar& operator=(const foo&) {
cout << "bar\n";
return *this;
}
};

int main() {
foo f;
bar b;

foo & fb = b;

f = f;
b = b;
fb = b;
}

Delete the virtual kw in foo, and see how output changes.

--
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
- Roger H. Lincoln
Dec 4 '06 #11

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

Similar topics

4
1790
by: Dean Mitchell | last post by:
Hi, I have a simple STL map container that maps a stl string to a structure as follows. typedef std::map<std::string,TASKLIST> PCLIST; In a class I have a member variable that is defined as a reference to a PCLIST; PCLIST &m_List;
4
1393
by: al | last post by:
string s = "original string"; s = "new string"; s.assign("another new string"); Is there any difference using above two way to assign a new string to s? class Derived : public Base {
5
1889
by: Steve | last post by:
Hi, I have a class called cList as so: template<class T> class cList { // base class for Lists private: protected: vector<T> tListOf; // field list container public: void Add(const T& t) {tListOf.push_back(t);} // add new object to list unsigned int Count() { return tListOf.size(); } // number of list items
2
1564
by: yopwojtek | last post by:
Hello All, i know from some tutorials, that copy constructor and assigment operator is not inherited from base to derived class. i think it make sense but i wrote a little example: class Base { protected: int* a_; public: Base& operator=(const Base& _toCopy) {
15
1911
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
2
25709
by: Mr Dyl | last post by:
I'm having a few issues porting Windows code to Linux (VS.Net 2003 to GCC 4.0.1). So this probably has something to do with VS not catching non-compliant code. I'd like to simplify this problem, but I'm not really sure what the issue is. I appologize in advance, as this doesn't give you much to go on. Basically, I want to compare my own iterators with stl vector iterators. I've written the operator== and operator!= functions to do...
2
2921
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class ("base"). I want to be able to add: 1) any derived array holding class to any other derived array holding class 2) any derived array holding class to a literal value (e.g int, double, etc) for which addition is defined for the type in the...
13
3981
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help is much appreciated. JD
19
3533
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class called MyString) and an integer i.e. std::map<MyString, int>. Whenever I insert the elements in the map using the subscript operator, I noticed that the copy constructor for MyString is invoked more number of times than if I do it using the insert()...
0
9731
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
10651
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...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
tracyyun
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...
0
9208
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
7671
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
6893
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();...
0
5556
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...
3
3020
bsmnconsultancy
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.