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

inherit assigment operator

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) {
*a_ = 5;
return *this;
}
Base() : a_(new int(6)) {}
~Base() {delete a_;}

int GetAValue() {return *a_;}
};

class Derive : public Base {
private:
double b_;
public:
Derive() : Base(), b_(1.0) {*a_ = 7;}
};

int _tmain() {
Derive der1;
Derive der2;
der1 = der2;
cout << der1.GetAValue() << endl; // - here got 5 on screen

return 0;
}

and i.m confused, becouse my assigment operator works for object from
derive class:( why?? I thought that der1 = der2; will use default
assigment operator generated by compiller. but is used the one from
Base class becouse i see that der1,GetAValue() return 5.
I hope You will able to explain me why it is so. The compiller is
VC++7.0.
thanks for any answers.
yopwojtek
czyli tworze zupelnie bzdurny operator przypisania z kopiowaniem w
klasie Base (w zasadzie nie kopiuje tylko ustawia *a_ na 5), ale nie
tworze go w klasie Derive. natomiast jesli teraz zastosuje operator
przypisania dla obiektów klasy pochodnej, to widac, ze jednak
zostanie zastosowany nie wygenerowany przez kompilator operator
domyslny tylko wlasnie ten który zdefiniowalem w klasie Base,
czyli jednak podczas dziedziczenia zostal przekazany do klasy Derive.
nie rozumiem dlaczego tak sie dzieje, bardzo prosze o razjasnienie
mi problemu?? kompilator to VC++7.0
pozdrawiam
woyt

Jul 23 '05 #1
2 1551
yopwojtek wrote in news:1122137091.534825.212870
@g44g2000cwa.googlegroups.com in comp.lang.c++:
and i.m confused, becouse my assigment operator works for object from
derive class:( why?? I thought that der1 = der2; will use default
assigment operator generated by compiller. but is used the one from
Base class becouse i see that der1,GetAValue() return 5.
I hope You will able to explain me why it is so. The compiller is
VC++7.0.


The compiler generated assignment operator, just calls operator = ()
for each sub-objects, this means base classes and member variables.

So the compiler generated operator = () in derived is calling your
user defined operator = ().

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '05 #2
thanks a lot.
woyt

Jul 23 '05 #3

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

Similar topics

4
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...
1
by: JKop | last post by:
First attempt: class Blah : public int { }; So I began working on the next best thing:
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
3
by: Boaz Ben-Porat | last post by:
I Know, ... a struct can not be inherited. ** Is there any tricky way to extend a struct ? ** I have to implement a component that represent a date (without time) that has a small number of...
6
by: Raf256 | last post by:
Hi, I would extend class only a bit. In example: I want to write my own class, that works all like std::string, only it also have member int mDatabase; and it have method SaveToDatabase(); I...
3
by: Allerdyce.John | last post by:
I would like to know what is the advantage of inherit from unary_function template? For example, I have this in my code: What is the advantage of doing this: class Same :...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
10
by: Adrian | last post by:
Below is an example of the problem I am having. I don't understand how I can get the compiler to see deriv &operator=(const T &rhs). I am sure this is a common problem - any suggestions? ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.