473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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[i].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[i];
//p[i] = obj.p[i];

//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[i] ? Just like *(p+i) <==>
p[i] ?
I've looked over few books but no luck so far, and this has been
pestering me for a few days now.
Thanks.

Jul 29 '06 #1
2 1624
br*****@uwindsor.ca schrieb:
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[i];
//p[i] = obj.p[i];

//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[i] ? Just like *(p+i) <==>
p[i] ?
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.

--
Thomas
Jul 29 '06 #2
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!

Jul 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
2
by: Foobarius Frobinium | last post by:
http://thelinuxlink.net/~fingolfin/pointer-guide/ I've made a lot of corrections, and put in more new stuff. I have malloc and pointers to struct's and maybe a few more things and I'm done. ...
8
by: Simon Morgan | last post by:
Can somebody please explain why the following piece of code generates an error unless I change q to *q: typedef int Queue; int top = 9; void insert(Queue *q, int n) { q = n; }
3
by: ash | last post by:
Hey I am new, but I don't have time to intruduce myself yet. I am intro to C++ and this is a programme I have to write. all the direction are here, It will be very nice of someone to figure this...
6
by: David Lozzi | last post by:
Howdy, I'm new to classes. Below is my class User. (is this a reserved namespace or class?) It works great, kind of. If I specify the username and password, the correct firstname and lastname...
7
by: Sam Kong | last post by:
Hello! My question would not be very practical but just out of curiosity. In JavaScript, Array is a subclass of Object. Well maybe not exactly... but sort of... For normal objects, you can...
2
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.