how is it possible to overload operators for a class array if there is only one integer not pointer to an integer ? can anyone demonstrate it with a little piece of code ? i know how to do it with pointer to integer but don't know how to with a single integer :(
Overloading Operators for Array Class
Started by ahmed, Apr 25 2009 05:51 AM
12 replies to this topic
#1
Posted 25 April 2009 - 05:51 AM
|
|
|
#2
Posted 25 April 2009 - 06:57 AM
We cant without your source code help you;)
#3
Posted 25 April 2009 - 07:08 AM
Here is a bit i made how to overload it ? i can't user pointer to array in the class so please help me out :(
class myInt{
int a;
public:
myInt(int p=0){
a=p; }
int get(){ return a; }
myInt operator + (myInt aOb[]){
myInt temp;
for(int i=0;i<size;i++){
temp=a[i]+aOb[i].a; }
return temp; }
};
#4
Posted 25 April 2009 - 07:14 AM
this is c or c# ,
sorry , i cant programming in that language
sorry , i cant programming in that language
#5
Posted 25 April 2009 - 07:14 AM
it's C++ =/
#6
Posted 25 April 2009 - 07:20 AM
ow ^^ :P
Then i see maybe the fold :
it can be stupid because i am a beginner ;)
if it doesnt help, i cant help you anymore
Then i see maybe the fold :
class myInt{
int a;
public:
[B] myInt(int p= "0"){
a = "p"; }
[/B]
int get(){ return a; }
myInt operator + (myInt aOb[]){
myInt temp;
for(int i=0;i<size;i++){
temp=a[i]+aOb[i].a; }
return temp; }
};
it can be stupid because i am a beginner ;)
if it doesnt help, i cant help you anymore
#7
Posted 25 April 2009 - 07:24 AM
hehe i think you don't know C++ so its ok :)
#8
Posted 25 April 2009 - 07:26 AM
i know c++ , but work style is different than that from me ;)
#9
Posted 25 April 2009 - 08:29 AM
I'm really not clear on what you have in mind. It looks like you want the following code to do something:
myInt daint; int a[10]; for (int i=0;i<10;i++) a[i]=i; daint = daint + a;Is that correct?
#10
Posted 25 April 2009 - 08:31 AM
that's why I said in my first post that I want a sample code in which we just take a single integer and then we have to make an array of objects in main and perform operations on it , i.e the operators which will be overloaded for it so can you please post some sample ? I can't find any with a simple int :(
#11
Posted 25 April 2009 - 08:48 AM
You would do better to use a vector of ints instead of an array of ints. With an array, you have no way of knowing how big it is inside method operator+. A vector has a size attribute, however, that will allow you to know how many items it has.
#12
Posted 25 April 2009 - 08:50 AM
Haven't studied vectors in university so far :( , maybe i should do it with an pointer to an int then


Sign In
Create Account


Back to top









