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! 10 1789
Adrian napsal:
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 *>());
}
The code is quite long, but for me is strange for example pure virtual
destructor in base class.
Adrian napsal:
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!
In container::oper ator[] you are returning reference to base. But there
is no assignment operator in class 'base', so compiler cannot see it.
Adrian wrote:
>
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).
The problem is that this function doesn't exist in base, which is the static
type. The compiler will only search the object's static type.
>
I am sure this is a common problem - any suggestions?
Surely. You can cast to whatever type you really have:
dynamic_cast<de riv<int>&>(data[0]) = 12445;
Another "solution" follows.
#include <iostream>
#include <vector>
struct base
{
template <class Tbase& operator=(const T&);
virtual ~base()=0 {};
of course this don't work, but your compiler will tell you that.
};
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_;
};
template <class T>
base& base::operator= (const T& rhs)
{
return dynamic_cast<de riv<T>&>(*this) = rhs;
}
>
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";
Of course "test" has type const char[5], not char*, so this will not work.
>
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 *>());
}
--
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
– Roger H. Lincoln
On 2006-12-04 21:07, Adrian wrote:
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 *>());
}
I'm quite sure you can't do that. Besides from what Ondra Holub has said
what you are trying to do is to create a heterogeneous container, which
I'm quite sure you can't do. At least not like that, you might succeed
if you first find out what kind of deriv it is and cast it to that type.
But then there would not be much of a point, would it?
--
Erik Wikström
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.
--
Adrian
Think you know a language? Post to comp.lang... and find out!
Ondra Holub wrote:
The code is quite long, but for me is strange for example pure virtual
destructor in base class.
This is a useful trick for insuring that the class is abstract.
--
Adrian
Think you know a language? Post to comp.lang... and find out!
Robert Bauck Hamar wrote:
Adrian wrote:
The problem is that this function doesn't exist in base, which is
the static
type. The compiler will only search the object's static type.
>I am sure this is a common problem - any suggestions?
Surely. You can cast to whatever type you really have:
dynamic_cast<de riv<int>&>(data[0]) = 12445;
Another "solution" follows.
>#include <iostream> #include <vector>
struct base {
template <class Tbase& operator=(const T&);
> virtual ~base()=0 {};
of course this don't work, but your compiler will tell you that.
What wont work. Nothing wrong with a virtual destructor?
template <class T>
base& base::operator= (const T& rhs)
{
return dynamic_cast<de riv<T>&>(*this) = rhs;
}
Perfect. I didnt try that. I assumed the compiler would never be able
to deduce T.
Thanks for that.
>
Of course "test" has type const char[5], not char*, so this will not work.
Was just throwing things into the example :-)
--
Adrian
Think you know a language? Post to comp.lang... and find out!
Adrian wrote:
Robert Bauck Hamar wrote:
>Adrian wrote:
The problem is that this function doesn't exist in base, which is
the static
>type. The compiler will only search the object's static type.
>>I am sure this is a common problem - any suggestions?
Surely. You can cast to whatever type you really have:
dynamic_cast<d eriv<int>&>(dat a[0]) = 12445;
Another "solution" follows.
>>#include <iostream> #include <vector>
struct base {
template <class Tbase& operator=(const T&);
>> virtual ~base()=0 {};
of course this don't work, but your compiler will tell you that.
What wont work. Nothing wrong with a virtual destructor?
The dtor _should_, of course, be virtual. But the above line is a syntax
error. You _can_ do:
class foo {
virtual ~foo() = 0;
};
foo::~foo() {}
but not
class foo {
virtual ~foo() = 0 {}
};
--
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
- Roger H. Lincoln
Robert Bauck Hamar wrote:
The dtor _should_, of course, be virtual. But the above line is a syntax
error. You _can_ do:
class foo {
virtual ~foo() = 0;
};
foo::~foo() {}
but not
class foo {
virtual ~foo() = 0 {}
};
Gotya. Actually the compiler allows it. I hate it when they dont
follow standards, makes porting a nightmare.
Just found it in the standard. 10.4.2. Makes sense
--
Adrian
Think you know a language? Post to comp.lang... and find out! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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
{
|
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
|
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) {
|
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
| |
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...
|
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...
|
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
|
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()...
|
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: 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...
|
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: 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: 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
|
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...
| |