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

If operator=() is private in base.....

If base declares operator=() as private, is it possible for the
derived class to declare operator=() as public and allow assignment
access for it?
If no, why is it restricted so?
Jul 22 '05 #1
3 1356

"qazmlp" <qa********@rediffmail.com> skrev i en meddelelse
news:db**************************@posting.google.c om...
If base declares operator=() as private, is it possible for the
derived class to declare operator=() as public and allow assignment
access for it? Yes If no, why is it restricted so?

It is not, but you might have problems declaring a sensible operator=
considering the base class does not define this operation.

/Peter
Jul 22 '05 #2
qazmlp wrote:
If base declares operator=() as private, is it possible for the
derived class to declare operator=() as public and allow assignment
access for it?


Yes. You can do that with any member function or operator.

Jul 22 '05 #3

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
If base declares operator=() as private, is it possible for the
derived class to declare operator=() as public and allow assignment
access for it?
If no, why is it restricted so?


Think it this way. Each derived object has a base part.
You want to keep assignment operator in base to be private.
That means you can't invoke bases' assignment operator from the derived class.
Which in turn means that when you assign objects you will keep
base part of the old object and derived part of the object you are assigning
from.

Consider this example code -

#include <iostream>
class A{
public:
int i;
A(int i): i(i){}
private:
A& operator=(const A& a)
{
this->i = a.i;
return *this;
}
};

class B: public A{
public:
int j;
B(int i, int j): A(i), j(j) {}
B& operator=(const B&b)
{
// Can't invoke base assignment operator.
// A::operator=(b);
this->j= b.j;
return *this;
}
};

int main(){
B b1(5, 2), b2(6, 4);
b1=b2;
std::cout << b1.i << " " << b1.j;
}

Output - 5 4
See the difference?

-Sharad
Jul 22 '05 #4

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

Similar topics

5
by: Bob Hairgrove | last post by:
Consider the following: #include <string> class A { public: A( const std::string & full_name , const std::string & display_name) : m_full_name(full_name)
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
1
by: PengYu.UT | last post by:
I try to invoke the operator of the base class. ** line doesn't work. But *** line works. However, *** lines becomes problematic with multiple inheritance is used for "derived". **** works, but...
2
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...
7
by: Jim Langston | last post by:
This is something someone was asking in irc. I really don't need to do this right now, but may have to in the future. The following code is in error (marked). #include <iostream> #include...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
7
by: Daniel Kraft | last post by:
Hi all! I'm getting some for me "unexpected behaviour" from g++ 4.1.2 when compiling this simple test program: #include <cstdlib> #include <cstdio> using namespace std; template<typename...
17
by: Corey Cooper | last post by:
I have a class for which I needed to create an operator= function. I then use that as a base class for another class, which also needs an operator=. What is the syntax to use in the derived class...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.