B
brzozo2
Hello, anyone knows if there is a way to use pointer notation instead
of
array when it comes to copying one object to another via a pointer of
array to objects?
Here is a very simple code to show what I mean:
/* 2 classes, one holds the int number, other is a table which
uses pointer to array of objects */
#include <iostream>
using namespace std;
class Number {
public:
int number;
Number& operator =(Number const& obj) { //assignment copy
if(this == &obj) return *this;
number = obj.number;
return *this;
}
};
class Table {
Number* p;
public:
Table(int n =0) {
p = new Number[5]; //p is array pointer to 5 Number objects
for(int i=0; i<5; i++)
p.number = n+i; //some values
}
// ******************************************************* //
Table(Table const& obj) { //copy constructor
p = new Number[5];
for(int i=0; i < 5; i++) {
//*(p+i) = obj.p;
//p = obj.p;
//Above 2 statments work, however when I try to use the
//pointer notation for the right operand:
*(p+i) = obj.*(p+i); //then this generates an error.
}
}
// ***************************************** //
~Table() { delete[] p; }
};
int main () {
//do some stuff here..
}
The error generated is:
'.*' : illegal, right operand has type 'Number *'
but isn't obj.*(p+i) equivalent to obj.p ? Just like *(p+i) <==>
p ?
I've looked over few books but no luck so far, and this has been
pestering me for a few days now.
Thanks.
of
array when it comes to copying one object to another via a pointer of
array to objects?
Here is a very simple code to show what I mean:
/* 2 classes, one holds the int number, other is a table which
uses pointer to array of objects */
#include <iostream>
using namespace std;
class Number {
public:
int number;
Number& operator =(Number const& obj) { //assignment copy
if(this == &obj) return *this;
number = obj.number;
return *this;
}
};
class Table {
Number* p;
public:
Table(int n =0) {
p = new Number[5]; //p is array pointer to 5 Number objects
for(int i=0; i<5; i++)
p.number = n+i; //some values
}
// ******************************************************* //
Table(Table const& obj) { //copy constructor
p = new Number[5];
for(int i=0; i < 5; i++) {
//*(p+i) = obj.p;
//p = obj.p;
//Above 2 statments work, however when I try to use the
//pointer notation for the right operand:
*(p+i) = obj.*(p+i); //then this generates an error.
}
}
// ***************************************** //
~Table() { delete[] p; }
};
int main () {
//do some stuff here..
}
The error generated is:
'.*' : illegal, right operand has type 'Number *'
but isn't obj.*(p+i) equivalent to obj.p ? Just like *(p+i) <==>
p ?
I've looked over few books but no luck so far, and this has been
pestering me for a few days now.
Thanks.