problem in using [ ] operator

B

Bangalore

Bangalorewrote:
[quote:c9ec9cc6ef]I am still not sure what all of this should
eventually lead to.
So may I ask: What is your real problem?
Don't show us what you think the solution to your problem is and what
problems you have with that solution. What is your first hand problem
that you try to solve with that code?

Actually I am facing this problem in my project, Here if at all ,I
include those array elements in my Base class ,size of the base class
will increase,this leads to change in architecture of that object , If
I change that object, it has a huge impact on my code.

With rererence vaibles , there is code snippet
--------- Before slipt: -----------
class B
{
public:
B(){};
A a;
}

++++++++ After split +++

#include<iostream.h>

class A
{
public:
A():i(9){};
int i; //I want to declare int i[10] as private, for data
hiding purpose
};

class B
{
public:
B():a(*(new A())){};
A& a; //Desperately I want to use array of two pointers
};

main()
{
B b;
cout << b.a.i;
}
)

{

B b;

cout << b.a.i;

}



And yes: I think Peter is still right: Whatever your problem might
be, std::vector most likely would be a solution to it.

I don't want to use vectors here.
 
J

John Harrison

Bangalore said:
Bangalorewrote:

[quote:c9ec9cc6ef]I am still not sure what all of this should
eventually lead to.
So may I ask: What is your real problem?
Don't show us what you think the solution to your problem is and
what

problems you have with that solution. What is your first hand
problem

that you try to solve with that code?


Actually I am facing this problem in my project, Here if at all ,I
include those array elements in my Base class ,size of the base class
will increase,this leads to change in architecture of that object , If
I change that object, it has a huge impact on my code.

With rererence vaibles , there is code snippet
--------- Before slipt: -----------
class B
{
public:
B(){};
A a;
}

++++++++ After split +++

#include<iostream.h>

class A
{
public:
A():i(9){};
int i; //I want to declare int i[10] as private, for data
hiding purpose
};

class B
{
public:
B():a(*(new A())){};
A& a; //Desperately I want to use array of two pointers

B(): { a[0] = new A(); a[1] = new A(); }
A* a[2];

What is the problem?
};

main()
{
B b;
cout << b.a.i;
}
)

{

B b;

cout << b.a.i;

}







I don't want to use vectors here.

You've got three choices.

1) Use an array, you already said you don't want to do that.

2) Use a vector, you've just said you don't want to do that (why?).

3) Use pointers.

I have to say I don't think there is any problem here. You seem to be
seeing problems that don't exist.

john
 
B

Bangalore

B(): { a[0] = new A(); a[1] = new A(); }
A* a[2];

What is the problem?

I have problem in accsessing array elements using overloaded []
operator.I
have slightly modied code for array of pointers.

class A
{
int a[1000]; // declaring int a[10] as private, for data hiding
purpose
public:
A():{};
int i;

//overload [] operator
int& operator [] (int index)
{
//some range checking can be done
return a;
}
};

};
class B
{
A* a[2]; //declaring in private part for data hiding.
public:
B():
{
a[0]=new A();
a[1]=new A();
}

//I need to overload [] operator here so that
//in main function I can directly use objects of
//this class
};
int main()
{

B base;
// Here I wanted to fill the values for array in class A(i.e int
a[100]);
//How can I do it here?????
}

You've got three choices.

1) Use an array, you already said you don't want to do that.

2) Use a vector, you've just said you don't want to do that (why?).

3) Use pointers.
Here I am using array of pointers
I have to say I don't think there is any problem here. You seem to be
seeing problems that don't exist.
I am finding problem in implementing overloaded operator [] in class B
and
Filling array elements in main()

Thanks
Bangalore]
 
K

Karl Heinz Buchegger

Bangalore said:
B(): { a[0] = new A(); a[1] = new A(); }
A* a[2];

What is the problem?

I have problem in accsessing array elements using overloaded []
operator.I
have slightly modied code for array of pointers.

class A
{
int a[1000]; // declaring int a[10] as private, for data hiding
purpose
public:
A():{};
int i;

//overload [] operator
int& operator [] (int index)
{
//some range checking can be done
return a;

You don't want to return a here. a is an array.
You want to return a reference to one specific array
element.

return a[index];
}
};

};
class B
{
A* a[2]; //declaring in private part for data hiding.
public:
B():
{
a[0]=new A();
a[1]=new A();
}

//I need to overload [] operator here so that
//in main function I can directly use objects of
//this class
};
int main()
{

B base;
// Here I wanted to fill the values for array in class A(i.e int
a[100]);
//How can I do it here?????

Well.
Think of some syntax you want to use.
How would you have it look like?
I am finding problem in implementing overloaded operator [] in class B
and
Filling array elements in main()

What should that operator do?
eg. map all index values from 0 to 999 to *a[0]
and all index values from 1000 to 1999 to *a[1]

hint: the above already contains a hint in how the
syntax should look like. a is a poitner. Dereferencing
that pointer lets you get your hands at the underlying
object. If in doubt, a few '(' ')' are most often a good
idea to clear things up.

int& B::eek:perator[] (int index)
{
if( index < 1000 )
return (*(a[0]))[index];
else
return (*(a[1]))[index];
}

a the array of pointers
a[0] one element of that array -> one pointer
*(a[0]) dereference the pointer -> the underlaing class A object
(*(a[0])) [index] use operator[] from that class A object
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,698
Members
45,488
Latest member
MohammedHa

Latest Threads

Top