Seeking some help with pointer/array notation wrt class members.

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.
 
T

Thomas J. Gritzan

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: [snip]
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 ?


Operator .* is one of the pointer-to-member operators (.* and ->*)

But even obj.(*(p+i)) would not work, because the right side of "." must
be a name of a member of obj.

You could do: *(obj.p+i)

And you could give std::vector<> and the other containers a try, they
will save you a lot of work.
 
B

brzozo2

You could do: *(obj.p+i)
Bah, I'm dumb heh :) I totally forgot about trying to dereference the
whole thing
intstead of just the member. Thanks!
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top