Solved

subscript op and function call op

Posted on 1998-04-20
2
160 Views
Last Modified: 2010-04-10
I cannot get this to compile: problems with the operators

class FOO {

         public:
             int operator () (void) {
                  return(1);
             }
             int operator [] (unsigned index) {
                  return(array[index]);
             }            

       private:
             int array[10];
}

FOO *foo;

int main(void)  {
     
     foo = new FOO();
    int result = (some boolean expresion) ? foo[3] : 0;   // compiler does not foo[3] in this line
    int result2 = (some boolean expression) ? foo(3) : 0;   // compiler does not like foo(3) either

    delete foo;
    return 0;

}

Thanks.
I tried this in both watcom c/c++ 10.0b and borland 5.02 c++
0
Comment
Question by:migue
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
ID: 1162507
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1162508
Your Foo[3] is close, the only problem is that Foo is not a FOO object, it is a pointer to a FOO object.  If you declared it as a FOO, like

FOO Foo;

Then

int i = Foo[3];

would work.

You can fix this by declaring Foo as a FOO as shown above or by making it a reference, like

FOO &Foo = new FOO;

(Kind of unusual, but works.)  Or you can continue to keep Foo a pointer to FOO and change your syntax with the [] operator, like

FOO *Foo;
int i = (*Foo)[3];

Obviously, your attempt to use parenthesis (that is, Foo(3)) has the same problem as well as the fact that the parenthesis operator does not accept an integer parameter.  Thus you could change Foo to an object or a reference to an object and then you could do Foo().  Or you can keep it a pointer and do (*Foo)().
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now