?
What will be the output of the following C++ code?
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int n = 0) : x(n) {};
int& operator[](int n)
{
cout << "0" ;
return x;
}
int operator[](int n) const
{
cout << "1" ;
return x;
}
};
void foo(const A& a)
{
int z = a[2];
}
int main()
{
A a(7);
a[3] = 8;
int z = a[2];
foo(a);
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int n = 0) : x(n) {};
int& operator[](int n)
{
cout << "0" ;
return x;
}
int operator[](int n) const
{
cout << "1" ;
return x;
}
};
void foo(const A& a)
{
int z = a[2];
}
int main()
{
A a(7);
a[3] = 8;
int z = a[2];
foo(a);
return 0;
}
Join the Discussion
Login to post a comment or share your explanation.
Login