473,395 Members | 1,639 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,395 software developers and data experts.

ptr to member

im trying to index through an array that is member of a class using ptr
to member

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntPtr = &testclass7::arr;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::* '
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
how do i define a ptr to member arr?

class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(arr) / sizeof(*arr);i++)
{
arr[i] = arg+i;
}
}
};
Jul 22 '05 #1
2 1516
* trying_to_learn:
im trying to index through an array that is member of a class using ptr
to member
Instead provide a []-operator.

Pointer to member is very seldom necessary, and pointer to data member
is absolutely EVIL.

Don't use it.

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntPtr = &testclass7::arr;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::* '
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
No, it's not const, and it's not a pointer: it's an array.

how do i define a ptr to member arr?

class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(arr) / sizeof(*arr);i++)
{
arr[i] = arg+i;
}
}
};


Like this:

int main()
{
testclass7 obj;
int (testclass7::* p)[10] = &testclass7::arr;

for( int i = 0; i < 10; ++i )
{
std::cout << (obj.*p)[i] << std::endl;
}
}

But don't use it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
trying_to_learn wrote:
im trying to index through an array that is member of a class using ptr
to member

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntPtr = &testclass7::arr;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::*
' Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
Uh... a compiler suggesting a reinterpret_cast and a C style cast... evil.
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
No. arr is an array of 10 ints. Look at the error message. What you'd need
is a member pointer to an array of int, not a member pointer to a pointer.
Anyway, why do you think you need a member pointer at all?
how do i define a ptr to member arr?
Uhm, look at the error message. It tells you.
class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(arr) / sizeof(*arr);i++)
{
arr[i] = arg+i;
}
}
};


Jul 22 '05 #3

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

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
40
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; ...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.