473,398 Members | 2,120 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,398 software developers and data experts.

How to invoke the operator of the private base class?

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 it is a little bit cumbersome. I'm wondering if there any
way to make ** works?
#include <iostream>
#include <cstring>

class base {
public:
int operator[](int i) {
return i;
}
int get(int i) {
return i;
}
};

class derived : private base {
public:
derived(int i) {
std::cout << "base[i]: " << base[i] << std::endl; //error
**
std::cout << "base[i]: " << (*this)[i] << std::endl; //no error
***
std::cout << "base[i]: " << base::operator[](i) << std::endl;//
no error ****
std::cout << "base.get(i): " << base::get(i) << std::endl;
}
};

int main(int argc, char *argv[])
{
derived a(10);
}

Apr 1 '06 #1
1 1351
Pe*******@gmail.com wrote:
I try to invoke the [] operator of the base class.

** line doesn't work.
The syntax "[blah]" is only applicable to _expressions_, and further
only to those that evaluate to objects of classes that overload the
operator[] or to pointers.

The syntax "[blah]" is not applicable to _types_. "base" is a *type*.
But *** line works.
Of course. "(*this)" is an *object* of type that overloads op[] (by
inheriting the base's one).
However, *** lines becomes
problematic with multiple inheritance is used for "derived".
What do you mean by that? You can always static_cast your 'this' to
avoid ambiguity.
****
works, but it is a little bit cumbersome. I'm wondering if there any
way to make ** works?
No.
#include <iostream>
#include <cstring>

class base {
public:
int operator[](int i) {
return i;
}
int get(int i) {
return i;
}
};

class derived : private base {
public:
derived(int i) {
std::cout << "base[i]: " << base[i] << std::endl; //error
**
std::cout << "base[i]: " << (*this)[i] << std::endl; //no error
***
std::cout << "base[i]: " << base::operator[](i) << std::endl;//
no error ****
std::cout << "base.get(i): " << base::get(i) << std::endl;
}
};

int main(int argc, char *argv[])
{
derived a(10);
}

V
--
Please remove capital As from my address when replying by mail
Apr 1 '06 #2

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

Similar topics

15
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
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"
2
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 {...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
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 {...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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:
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...
0
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...
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...
0
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...

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.