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

Home Posts Topics Members FAQ

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 1564
yopwojtek wrote in news:1122137091 .534825.212870
@g44g2000cwa.go oglegroups.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
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;
1
1435
by: JKop | last post by:
First attempt: class Blah : public int { }; So I began working on the next best thing:
5
2599
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 derived iterator. I need the derived iterator in order to overload the operator++. But I want to use also std::vector<PBaseItem>::begin() in order to get the begin of the vector.
3
14104
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 functions over those of DataTime type. My first thougt was : "Inherit from DateTime and implement those functions". Then I found out that DateTime is a struct, not a class, so it can`t be done. For example, The new type should implement an...
6
2024
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 could do like: class cStorableStr : std::string { private: int mDatabase;
3
2219
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 : std::unary_function<A*, bool> { public:
21
5727
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 classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
19
2453
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; //every class may have this statement self.hello = function() {
10
1792
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? #include <iostream> #include <vector> struct base
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
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
1
10405
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
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...
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();...
1
4342
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
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.